Skip to content

Commit

Permalink
add a navigation manager. closes #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Cooke committed Nov 11, 2013
1 parent b6785c0 commit 63b337a
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This document outlines key changes which are introduced in each version. The ful

* Add an admin-side order form for adding new orders and editing existing orders

* Adds `Shoppe::NavigationManager` which is used for managing sets of navigation within
the Shoppe interface. This allows module developers to add their own when appropriate
without needing to hack it into the view.

## v0.0.19

* Only check that a delivery service is suitable if one has actually been selected. Ensures that
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/shoppe/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Shoppe
module ApplicationHelper

def navigation_manager_link(item)
link_to item.description, item.url, item.link_options.merge(:class => item.active?(self) ? 'active' : 'inactive')
end

def status_tag(status)
content_tag :span, status, :class => "status-tag #{status}"
Expand Down
23 changes: 13 additions & 10 deletions app/views/layouts/shoppe/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
%p.logged
Logged in as #{current_user.full_name}
%ul
%li
%span.pending= Shoppe::Order.pending.count
= link_to "Orders", [:orders], :class => @active_nav == :orders ? 'active' : ''
%li= link_to "Products", [:products], :class => @active_nav == :products ? 'active' : ''
%li= link_to "Product Categories", :product_categories, :class => @active_nav == :product_categories ? 'active' : ''
%li= link_to "Delivery Services", [:delivery_services], :class => @active_nav == :delivery_services ? 'active' : ''
%li= link_to "Tax Rates", :tax_rates, :class => @active_nav == :tax_rates ? 'active' : ''
%li= link_to "Users", [:users], :class => @active_nav == :users ? 'active' : ''
%li= link_to "Countries", :countries, :class => @active_nav == :countries ? 'active' : ''
%li= link_to "Settings", :settings, :class => @active_nav == :settings ? 'active' : ''
- for item in Shoppe::NavigationManager.find(:admin_primary).items
%li= navigation_manager_link item

-#%li
-# %span.pending= Shoppe::Order.pending.count
-# = link_to "Orders", [:orders], :class => @active_nav == :orders ? 'active' : ''
-#%li= link_to "Products", [:products], :class => @active_nav == :products ? 'active' : ''
-#%li= link_to "Product Categories", :product_categories, :class => @active_nav == :product_categories ? 'active' : ''
-#%li= link_to "Delivery Services", [:delivery_services], :class => @active_nav == :delivery_services ? 'active' : ''
-#%li= link_to "Tax Rates", :tax_rates, :class => @active_nav == :tax_rates ? 'active' : ''
-#%li= link_to "Users", [:users], :class => @active_nav == :users ? 'active' : ''
-#%li= link_to "Countries", :countries, :class => @active_nav == :countries ? 'active' : ''
-#%li= link_to "Settings", :settings, :class => @active_nav == :settings ? 'active' : ''
%li= link_to "Logout", [:logout], :method => :delete

%header.main
Expand Down
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
en:
shoppe:

navigation:
admin_primary:
orders: Orders
products: Products
product_categories: Product Categories
delivery_services: Delivery Services
tax_rates: Tax Rates
users: Users
countries: Countries

settings:

types:
Expand Down
19 changes: 19 additions & 0 deletions lib/shoppe/default_navigation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'shoppe/navigation_manager'

# This file defines all the default navigation managers used in Shoppe. Of course,
# modules can make changes to these by removing them or adding their own. This
# file is loaded on application initialization so if you make changes, you'll need
# to restart the webserver.

#
# This is the default navigation manager for the admin interface.
#
Shoppe::NavigationManager.build(:admin_primary) do
add_item :orders
add_item :products
add_item :product_categories
add_item :delivery_services
add_item :tax_rates
add_item :users
add_item :countries
end
6 changes: 5 additions & 1 deletion lib/shoppe/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ class Engine < ::Rails::Engine
config.paths["db/migrate"].expanded.each do |expanded_path|
app.config.paths["db/migrate"] << expanded_path
end
end
end

# Load view helpers for the base application
ActiveSupport.on_load(:action_view) do
require 'shoppe/view_helpers'
ActionView::Base.send :include, Shoppe::ViewHelpers
end

# Load default navigation
require 'shoppe/default_navigation'
end

generators do
Expand All @@ -43,3 +46,4 @@ class Engine < ::Rails::Engine

end
end

79 changes: 79 additions & 0 deletions lib/shoppe/navigation_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module Shoppe
class NavigationManager

def self.managers
@managers ||= []
end

def self.create(identifier)
managers << self.new(identifier.to_s)
end

def self.build(identifier, &block)
manager = self.new(identifier.to_s)
manager.instance_eval(&block) if block_given?
managers << manager
end

def self.find(identifier)
managers.select { |m| m.identifier == identifier.to_s }.first
end

def self.render(identifier)
find(identifier).try(:to_html)
end

attr_reader :identifier

def initialize(identifier)
@identifier = identifier
end

def items
@items ||= []
end

def add_item(identifier, options = {}, &block)
item = NavigationItem.new
item.manager = self
item.identifier = identifier.to_s
item.url = options[:url] if options[:url]
item.link_options = options[:link_options] if options[:link_options]
item.active_if = block if block_given?
items << item
end

def remove_item(identifier)
items.remote_if { |i| i.identifier.to_s == identifier.to_s }
end

class NavigationItem
attr_accessor :manager
attr_accessor :identifier
attr_accessor :url
attr_accessor :link_options
attr_accessor :active_if

def description
I18n.translate("shoppe.navigation.#{manager.identifier}.#{identifier}")
end

def url
@url || Shoppe::Engine.routes.url_helpers.send("#{identifier}_path")
end

def active?(request)
if active_if.is_a?(Proc)
active_if.call(request) == true
elsif active_nav_var = request.instance_variable_get('@active_nav')
active_nav_var.to_s == identifier
end
end

def link_options
@link_options ||= {}
end
end

end
end

0 comments on commit 63b337a

Please sign in to comment.