-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a navigation manager. closes #59
- Loading branch information
Adam Cooke
committed
Nov 11, 2013
1 parent
b6785c0
commit 63b337a
Showing
7 changed files
with
134 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |