Skip to content

Commit 6068a32

Browse files
committed
Merge branch 'lawitschka/conditional-menus'
2 parents dd95163 + fa0d13f commit 6068a32

File tree

8 files changed

+113
-3
lines changed

8 files changed

+113
-3
lines changed

lib/active_admin/menu_item.rb

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def <=>(other)
6262
result = name <=> other.name if result == 0
6363
result
6464
end
65+
66+
# Returns the display if block. If the block was not explicitly defined
67+
# a default block always returning true will be returned.
68+
def display_if_block
69+
@display_if_block || lambda { |_| true }
70+
end
6571

6672
end
6773
end

lib/active_admin/namespace.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def register_with_menu(config)
190190
# Update the url if it's already been created
191191
add_to[config.menu_item_name].url = config.route_collection_path
192192
else
193-
add_to.add(config.menu_item_name, config.route_collection_path)
193+
add_to.add(config.menu_item_name, config.route_collection_path, config.menu_item_priority, { :if => config.menu_item_display_if })
194194
end
195195
end
196196
end

lib/active_admin/resource.rb

+10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ def parent_menu_item_name
136136
def menu_item_name
137137
@menu_options[:label] || plural_resource_name
138138
end
139+
140+
# Returns the items priority for altering the default sort order
141+
def menu_item_priority
142+
@menu_options[:priority] || 10
143+
end
144+
145+
# Returns a proc for deciding whether to display the menu item or not in the view
146+
def menu_item_display_if
147+
@menu_options[:if] || proc { true }
148+
end
139149

140150
# Should this resource be added to the menu system?
141151
def include_in_menu?

lib/active_admin/views/tabs_renderer.rb

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def render_menu(menu)
2222
end
2323

2424
def render_item(item)
25+
if !call_method_or_proc_on(self, item.display_if_block)
26+
return
27+
elsif (!item.url or item.url == '#') and item.children.any? and (item.children.detect {|child| call_method_or_proc_on(self, child.display_if_block)}).nil?
28+
return
29+
end
30+
2531
content_tag :li, :id => item.dom_id, :class => [("current" if current?(item)), ("has_nested" unless item.children.blank?)].compact.join(" ") do
2632
unless item.children.blank?
2733
link_to(item.name, item.url || "#") + render_nested_menu(item)

spec/unit/menu_item_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ module ActiveAdmin
2727
item = MenuItem.new("Dashboard", "/admin", 10, :if => block )
2828
item.display_if_block.should == block
2929
end
30+
31+
it "should have a default display if block always returning true" do
32+
item = MenuItem.new("Dashboard", "/admin")
33+
item.display_if_block.should be_instance_of(Proc)
34+
item.display_if_block.call(self).should == true
35+
end
3036

3137
describe "url generation and caching" do
3238
it "should generate a url if it is a symbol" do

spec/unit/namespace_spec.rb

+34-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,40 @@ module ::Mock; class Resource; def self.has_many(arg1, arg2); end; end; end
118118
end
119119

120120
describe "disabling the menu" do
121-
# TODO
122-
it "should not create a menu item"
121+
before do
122+
namespace.register Category do
123+
menu false
124+
end
125+
namespace.load_menu!
126+
end
127+
it "should not create a menu item" do
128+
namespace.menu["Categories"].should be_nil
129+
end
130+
end
131+
132+
describe "setting menu priority" do
133+
before do
134+
namespace.register Category do
135+
menu :priority => 2
136+
end
137+
namespace.load_menu!
138+
end
139+
it "should have a custom priority of 2" do
140+
namespace.menu["Categories"].priority.should == 2
141+
end
142+
end
143+
144+
describe "setting a condition for displaying" do
145+
before do
146+
namespace.register Category do
147+
menu :if => proc { false }
148+
end
149+
namespace.load_menu!
150+
end
151+
it "should have a proc returning false" do
152+
namespace.menu["Categories"].display_if_block.should be_instance_of(Proc)
153+
namespace.menu["Categories"].display_if_block.call.should == false
154+
end
123155
end
124156

125157
describe "adding as a belongs to" do

spec/unit/resource_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,27 @@ module ::Mock; class Resource; end; end
121121
end.parent_menu_item_name.should == "Blog"
122122
end
123123
end
124+
125+
describe "menu item priority" do
126+
it "should be 10 when not set" do
127+
config.menu_item_priority.should == 10
128+
end
129+
it "should be settable" do
130+
config.menu :priority => 2
131+
config.menu_item_priority.should == 2
132+
end
133+
end
134+
135+
describe "menu item display if" do
136+
it "should be a proc always returning true if not set" do
137+
config.menu_item_display_if.should be_instance_of(Proc)
138+
config.menu_item_display_if.call.should == true
139+
end
140+
it "should be settable" do
141+
config.menu :if => proc { false }
142+
config.menu_item_display_if.call.should == false
143+
end
144+
end
124145

125146
describe "route names" do
126147
let(:config){ ActiveAdmin.register Category }

spec/unit/tabs_renderer_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
reports = menu["Reports"]
1414
reports.add "A Sub Reports", "/admin/a-sub-reports"
1515
reports.add "B Sub Reports", "/admin/b-sub-reports"
16+
menu.add "Administration", "/admin/administration"
17+
administration = menu["Administration"]
18+
administration.add "User administration", '/admin/user-administration', 10, :if => proc { false }
19+
menu.add "Management", "#"
20+
management = menu["Management"]
21+
management.add "Order management", '/admin/order-management', 10, :if => proc { false }
22+
management.add "Bill management", '/admin/bill-management', 10, :if => :admin_logged_in?
23+
24+
renderer.stub!(:admin_logged_in?).and_return(false)
1625
end
1726

1827
it "should generate a ul" do
@@ -39,6 +48,26 @@
3948
html.should have_tag("li", :parent => { :tag => "ul" }, :attributes => {:id => "a_sub_reports"})
4049
html.should have_tag("li", :parent => { :tag => "ul" }, :attributes => {:id => "b_sub_reports"})
4150
end
51+
52+
it "should not generate a link for user administration" do
53+
html.should_not have_tag("a", "User administration", :attributes => { :href => '/admin/user-administration' })
54+
end
55+
56+
it "should generate the administration parent menu" do
57+
html.should have_tag("a", "Administration", :attributes => { :href => '/admin/administration' })
58+
end
59+
60+
it "should not generate a link for order management" do
61+
html.should_not have_tag("a", "Order management", :attributes => { :href => '/admin/order-management' })
62+
end
63+
64+
it "should not generate a link for bill management" do
65+
html.should_not have_tag("a", "Bill management", :attributes => { :href => '/admin/bill-management' })
66+
end
67+
68+
it "should not generate the management parent menu" do
69+
html.should_not have_tag("a", "Management", :attributes => { :href => '#' })
70+
end
4271

4372
describe "marking current item" do
4473
it "should add the 'current' class to the li" do

0 commit comments

Comments
 (0)