simple-navigation 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ *1.2.0
2
+ * added capability to add conditions to navigation-items (primary.item key, name, url, :if => Proc.new {current_user.admin?})
3
+
1
4
  *1.1.2
2
5
 
3
6
  * Bugfix: config now gets evaluated on every render_navigation call. Credits to Joël Azémar.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 1
4
- :patch: 2
3
+ :minor: 2
4
+ :patch: 0
@@ -13,16 +13,22 @@ SimpleNavigation::Configuration.run do |navigation|
13
13
  # key - a symbol which uniquely defines your navigation item in the scope of the primary_navigation
14
14
  # name - will be displayed in the rendered navigation. This can also be a call to your I18n-framework.
15
15
  # url - the address that the generated item links to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
16
- # html_options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
16
+ # options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
17
17
  #
18
- primary.item :key_1, 'name', url, html_options
18
+ primary.item :key_1, 'name', url, options
19
19
 
20
20
  # Add an item which has a sub navigation (same params, but with block)
21
- primary.item :key_2, 'name', url, html_options do |sub_nav|
21
+ primary.item :key_2, 'name', url, options do |sub_nav|
22
22
  # Add an item to the sub navigation (same params again)
23
- sub_nav.item :key_2_1, 'name', url, html_options
23
+ sub_nav.item :key_2_1, 'name', url, options
24
24
  end
25
25
 
26
+ # You can also specify a condition-proc that needs to be fullfilled to display an item.
27
+ # Conditions are part of the options. They are evaluated in the context of the views,
28
+ # thus you can use all the methods and vars you have available in the views.
29
+ primary.item :key_3, 'Admin', url, :class => 'special', :if => Proc.new { current_user.admin? }
30
+ primary.item :key_4, 'Account', url, :unless => Proc.new { logged_in? }
31
+
26
32
  end
27
33
 
28
34
  end
@@ -19,11 +19,18 @@ module SimpleNavigation
19
19
  #
20
20
  # The <tt>url</tt> is the address that the generated item points to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
21
21
  #
22
- # The <tt>html_options</tt> can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
22
+ # The <tt>options</tt> can be used to specify the following things:
23
+ # * <tt>html_attributes</tt> - will be included in the rendered navigation item (e.g. id, class etc.)
24
+ # * <tt>:if</tt> - Specifies a proc to call to determine if the item should
25
+ # be rendered (e.g. <tt>:if => Proc.new { current_user.admin? }</tt>). The
26
+ # proc should evaluate to a true or false value and is evaluated in the context of the view.
27
+ # * <tt>:unless</tt> - Specifies a proc to call to determine if the item should not
28
+ # be rendered (e.g. <tt>:unless => Proc.new { current_user.admin? }</tt>). The
29
+ # proc should evaluate to a true or false value and is evaluated in the context of the view.
23
30
  #
24
31
  # The <tt>block</tt> - if specified - will hold the item's sub_navigation.
25
- def item(key, name, url, html_options={}, &block)
26
- @items << Item.new(key, name, url, html_options, block)
32
+ def item(key, name, url, options={}, &block)
33
+ (@items << Item.new(key, name, url, options, block)) if should_add_item?(options)
27
34
  end
28
35
 
29
36
  # Returns the Item with the specified key, nil otherwise.
@@ -37,6 +44,25 @@ module SimpleNavigation
37
44
  def render(current_navigation, include_sub_navigation=false, current_sub_navigation=nil)
38
45
  self.renderer.new(current_navigation, current_sub_navigation).render(self, include_sub_navigation)
39
46
  end
47
+
48
+ private
49
+
50
+ # partially borrowed from ActionSupport::Callbacks
51
+ def should_add_item?(options) #:nodoc:
52
+ [options.delete(:if)].flatten.compact.all? { |m| evaluate_method(m) } &&
53
+ ![options.delete(:unless)].flatten.compact.any? { |m| evaluate_method(m) }
54
+ end
55
+
56
+ # partially borrowed from ActionSupport::Callbacks
57
+ def evaluate_method(method) #:nodoc:
58
+ case method
59
+ when Proc, Method
60
+ method.call
61
+ else
62
+ raise ArgumentError, ":if or :unless must be procs or lambdas"
63
+ end
64
+ end
65
+
40
66
 
41
67
  end
42
68
 
@@ -17,36 +17,115 @@ describe SimpleNavigation::ItemContainer do
17
17
 
18
18
  describe 'item' do
19
19
 
20
- context 'block given' do
20
+ context 'unconditional item' do
21
+
21
22
  before(:each) do
22
- @sub_container = stub(:sub_container)
23
- SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
23
+ @item_container.stub!(:should_add_item?).and_return(true)
24
24
  end
25
+
26
+ context 'block given' do
27
+ before(:each) do
28
+ @sub_container = stub(:sub_container)
29
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
30
+ end
25
31
 
26
- it "should should yield an new ItemContainer" do
27
- @item_container.item('key', 'name', 'url', 'options') do |container|
28
- container.should == @sub_container
32
+ it "should should yield an new ItemContainer" do
33
+ @item_container.item('key', 'name', 'url', 'options') do |container|
34
+ container.should == @sub_container
35
+ end
36
+ end
37
+ it "should create a new Navigation-Item with the given params and the specified block" do
38
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', @proc)
39
+ @item_container.item('key', 'name', 'url', 'options', &@proc)
40
+ end
41
+ it "should add the created item to the list of items" do
42
+ @item_container.items.should_receive(:<<)
43
+ @item_container.item('key', 'name', 'url', 'options') {}
29
44
  end
30
45
  end
31
- it "should create a new Navigation-Item with the given params and the specified block" do
32
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', @proc)
33
- @item_container.item('key', 'name', 'url', 'options', &@proc)
34
- end
35
- it "should add the created item to the list of items" do
36
- @item_container.items.should_receive(:<<)
37
- @item_container.item('key', 'name', 'url', 'options') {}
46
+
47
+ context 'no block given' do
48
+ it "should create a new Navigation_item with the given params and nil as sub_navi" do
49
+ SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', nil)
50
+ @item_container.item('key', 'name', 'url', 'options')
51
+ end
52
+ it "should add the created item to the list of items" do
53
+ @item_container.items.should_receive(:<<)
54
+ @item_container.item('key', 'name', 'url', 'options')
55
+ end
38
56
  end
57
+
39
58
  end
40
59
 
41
- context 'no block given' do
42
- it "should create a new Navigation_item with the given params and nil as sub_navi" do
43
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', 'options', nil)
44
- @item_container.item('key', 'name', 'url', 'options')
45
- end
46
- it "should add the created item to the list of items" do
47
- @item_container.items.should_receive(:<<)
48
- @item_container.item('key', 'name', 'url', 'options')
60
+ context 'conditions given for item' do
61
+
62
+ context '"if" given' do
63
+
64
+ before(:each) do
65
+ @options = {:if => Proc.new {@condition}}
66
+ end
67
+
68
+ it "should remove if from options" do
69
+ @item_container.item('key', 'name', 'url', @options)
70
+ @options[:if].should be_nil
71
+ end
72
+
73
+ context 'if evals to true' do
74
+ before(:each) do
75
+ @condition = true
76
+ end
77
+ it "should create a new Navigation-Item" do
78
+ SimpleNavigation::Item.should_receive(:new)
79
+ @item_container.item('key', 'name', 'url', @options)
80
+ end
81
+ end
82
+
83
+ context 'if evals to false' do
84
+ before(:each) do
85
+ @condition = false
86
+ end
87
+ it "should not create a new Navigation-Item" do
88
+ SimpleNavigation::Item.should_not_receive(:new)
89
+ @item_container.item('key', 'name', 'url', @options)
90
+ end
91
+ end
92
+
93
+ context '"unless" given' do
94
+
95
+ before(:each) do
96
+ @options = {:unless => Proc.new {@condition}}
97
+ end
98
+
99
+
100
+ it "should remove unless from options" do
101
+ @item_container.item('key', 'name', 'url', @options)
102
+ @options[:unless].should be_nil
103
+ end
104
+
105
+ context 'unless evals to false' do
106
+ before(:each) do
107
+ @condition = false
108
+ end
109
+ it "should create a new Navigation-Item" do
110
+ SimpleNavigation::Item.should_receive(:new)
111
+ @item_container.item('key', 'name', 'url', @options)
112
+ end
113
+ end
114
+
115
+ context 'unless evals to true' do
116
+ before(:each) do
117
+ @condition = true
118
+ end
119
+ it "should not create a new Navigation-Item" do
120
+ SimpleNavigation::Item.should_not_receive(:new)
121
+ @item_container.item('key', 'name', 'url', @options)
122
+ end
123
+ end
124
+
125
+ end
126
+
49
127
  end
128
+
50
129
  end
51
130
 
52
131
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  ENV["RAILS_ENV"] = "test"
2
- RAILS_ENV = "test"
2
+ RAILS_ENV = "test" unless defined? RAILS_ENV
3
3
  require 'rubygems'
4
4
  require 'spec'
5
5
  require 'active_support'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-navigation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andi Schacke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-18 00:00:00 +02:00
12
+ date: 2009-05-10 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15