simple-navigation 1.4.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,18 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe SimpleNavigation::Item do
4
-
4
+
5
+ before(:each) do
6
+ @item_container = stub(:item_container)
7
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, nil)
8
+ end
9
+
5
10
  describe 'initialize' do
6
11
  context 'method' do
7
12
  context 'defined' do
8
13
  before(:each) do
9
14
  @options = {:method => :delete}
10
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
15
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
11
16
  end
12
17
  it 'should set the method as instance_var' do
13
18
  @item.method.should == :delete
@@ -18,9 +23,6 @@ describe SimpleNavigation::Item do
18
23
  end
19
24
 
20
25
  context 'undefined' do
21
- before(:each) do
22
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
23
- end
24
26
  it 'should set the instance-var to nil' do
25
27
  @item.method.should be_nil
26
28
  end
@@ -29,30 +31,60 @@ describe SimpleNavigation::Item do
29
31
  end
30
32
 
31
33
  describe 'selected?' do
32
- before(:each) do
33
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
34
+ context 'explicitly selected' do
35
+ before(:each) do
36
+ @item.stub!(:selected_by_config? => true)
37
+ end
38
+ it {@item.should be_selected}
39
+ it "should not evaluate the subnav or urls" do
40
+ @item.should_not_receive(:selected_by_subnav?)
41
+ @item.should_not_receive(:selected_by_url?)
42
+ @item.selected?
43
+ end
44
+ end
45
+ context 'not explicitly selected' do
46
+ before(:each) do
47
+ @item.stub!(:selected_by_config? => false)
48
+ end
49
+ context 'subnav is selected' do
50
+ before(:each) do
51
+ @item.stub!(:selected_by_subnav? => true)
52
+ end
53
+ it {@item.should be_selected}
54
+ end
55
+ context 'subnav is not selected' do
56
+ before(:each) do
57
+ @item.stub!(:selected_by_subnav? => false)
58
+ end
59
+ context 'selected by url' do
60
+ before(:each) do
61
+ @item.stub!(:selected_by_url? => true)
62
+ end
63
+ it {@item.should be_selected}
64
+ end
65
+ context 'not selected by url' do
66
+ before(:each) do
67
+ @item.stub!(:selected_by_url? => false)
68
+ end
69
+ it {@item.should_not be_selected}
70
+ end
71
+ end
34
72
  end
35
- it {@item.selected?(:my_key).should be_true}
36
- it {@item.selected?(:my_other_key).should be_false}
37
73
  end
38
74
 
39
75
  describe 'selected_class' do
40
- before(:each) do
41
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
42
- end
43
-
44
76
  context 'item is selected' do
45
77
  before(:each) do
46
- @item.stub!(:selected?).and_return(true)
78
+ @item.stub!(:selected? => true)
47
79
  end
48
- it {@item.instance_eval {selected_class(:bla).should == 'selected'}}
80
+ it {@item.instance_eval {selected_class.should == 'selected'}}
49
81
  end
50
82
 
51
83
  context 'item is not selected' do
52
84
  before(:each) do
53
- @item.stub!(:selected?).and_return(false)
85
+ @item.stub!(:selected? => false)
54
86
  end
55
- it {@item.instance_eval {selected_class(:bla).should == nil}}
87
+ it {@item.instance_eval {selected_class.should == nil}}
56
88
  end
57
89
  end
58
90
 
@@ -61,40 +93,40 @@ describe SimpleNavigation::Item do
61
93
  context 'with classes defined in options' do
62
94
  before(:each) do
63
95
  @options = {:class => 'my_class'}
64
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
96
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
65
97
  end
66
98
  context 'with item selected' do
67
99
  before(:each) do
68
- @item.stub!(:selected?).and_return(true)
100
+ @item.stub!(:selected? => true)
69
101
  end
70
- it {@item.html_options(:bla)[:class].should == 'my_class selected'}
102
+ it {@item.html_options[:class].should == 'my_class selected'}
71
103
  end
72
104
 
73
105
  context 'with item not selected' do
74
106
  before(:each) do
75
- @item.stub!(:selected?).and_return(false)
107
+ @item.stub!(:selected? => false)
76
108
  end
77
- it {@item.html_options(:bla)[:class].should == 'my_class'}
109
+ it {@item.html_options[:class].should == 'my_class'}
78
110
  end
79
111
  end
80
112
 
81
113
  context 'without classes in options' do
82
114
  before(:each) do
83
115
  @options = {}
84
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', @options, nil)
116
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
85
117
  end
86
118
  context 'with item selected' do
87
119
  before(:each) do
88
- @item.stub!(:selected?).and_return(true)
120
+ @item.stub!(:selected? => true)
89
121
  end
90
- it {@item.html_options(:bla)[:class].should == 'selected'}
122
+ it {@item.html_options[:class].should == 'selected'}
91
123
  end
92
124
 
93
125
  context 'with item not selected' do
94
126
  before(:each) do
95
- @item.stub!(:selected?).and_return(false)
127
+ @item.stub!(:selected? => false)
96
128
  end
97
- it {@item.html_options(:bla)[:class].should be_blank}
129
+ it {@item.html_options[:class].should be_blank}
98
130
  end
99
131
  end
100
132
  end
@@ -102,41 +134,41 @@ describe SimpleNavigation::Item do
102
134
  describe 'id' do
103
135
  context 'with autogenerate_item_ids == true' do
104
136
  before(:each) do
105
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
106
137
  @item.stub!(:autogenerate_item_ids? => true)
138
+ @item.stub!(:selected? => false)
107
139
  end
108
140
  context 'with id defined in options' do
109
141
  before(:each) do
110
142
  @item.html_options = {:id => 'my_id'}
111
143
  end
112
- it {@item.html_options(:bla)[:id].should == 'my_id'}
144
+ it {@item.html_options[:id].should == 'my_id'}
113
145
  end
114
146
 
115
147
  context 'with no id definied in options (using default id)' do
116
148
  before(:each) do
117
149
  @item.html_options = {}
118
150
  end
119
- it {@item.html_options(:bla)[:id].should == 'my_key'}
151
+ it {@item.html_options[:id].should == 'my_key'}
120
152
  end
121
153
  end
122
154
 
123
155
  context 'with autogenerate_item_ids == false' do
124
156
  before(:each) do
125
- @item = SimpleNavigation::Item.new(:my_key, 'name', 'url', {}, nil)
126
157
  @item.stub!(:autogenerate_item_ids? => false)
158
+ @item.stub!(:selected? => false)
127
159
  end
128
160
  context 'with id defined in options' do
129
161
  before(:each) do
130
162
  @item.html_options = {:id => 'my_id'}
131
163
  end
132
- it {@item.html_options(:bla)[:id].should == 'my_id'}
164
+ it {@item.html_options[:id].should == 'my_id'}
133
165
  end
134
166
 
135
167
  context 'with no id definied in options (using default id)' do
136
168
  before(:each) do
137
169
  @item.html_options = {}
138
170
  end
139
- it {@item.html_options(:bla)[:id].should be_nil}
171
+ it {@item.html_options[:id].should be_nil}
140
172
  end
141
173
 
142
174
  end
@@ -144,6 +176,169 @@ describe SimpleNavigation::Item do
144
176
  end
145
177
 
146
178
  end
147
-
179
+
180
+ describe 'selected_by_subnav?' do
181
+ context 'item has subnav' do
182
+ before(:each) do
183
+ @sub_navigation = stub(:sub_navigation)
184
+ @item.stub!(:sub_navigation => @sub_navigation)
185
+ end
186
+ it "should return true if subnav is selected" do
187
+ @sub_navigation.stub!(:selected? => true)
188
+ @item.should be_selected_by_subnav
189
+ end
190
+ it "should return false if subnav is not selected" do
191
+ @sub_navigation.stub!(:selected? => false)
192
+ @item.should_not be_selected_by_subnav
193
+ end
194
+ end
195
+ context 'item does not have subnav' do
196
+ before(:each) do
197
+ @item.stub!(:sub_navigation => @sub_navigation)
198
+ end
199
+ it {@item.should_not be_selected_by_subnav}
200
+ end
201
+ end
202
+
203
+ describe 'selected_by_config?' do
204
+ context 'navigation explicitly set' do
205
+ it "should return true if current matches key" do
206
+ @item_container.stub!(:current_explicit_navigation => :my_key)
207
+ @item.should be_selected_by_config
208
+ end
209
+ it "should return false if current does not match key" do
210
+ @item_container.stub!(:current_explicit_navigation => :other_key)
211
+ @item.should_not be_selected_by_config
212
+ end
213
+ end
214
+ context 'navigation not explicitly set' do
215
+ before(:each) do
216
+ @item_container.stub!(:current_explicit_navigation => nil)
217
+ end
218
+ it {@item.should_not be_selected_by_config}
219
+ end
220
+ end
221
+
222
+ describe 'selected_by_url?' do
223
+ context 'auto_highlight is turned on' do
224
+ before(:each) do
225
+ @item.stub!(:auto_highlight? => true)
226
+ end
227
+ context 'root path matches' do
228
+ before(:each) do
229
+ @item.stub!(:root_path_match? => true)
230
+ end
231
+ it {@item.send(:selected_by_url?).should be_true}
232
+ end
233
+ context 'root path does not match' do
234
+ before(:each) do
235
+ @item.stub!(:root_path_match? => false)
236
+ end
237
+ context 'template is set' do
238
+ before(:each) do
239
+ @template = stub(:template)
240
+ SimpleNavigation.stub!(:template => @template)
241
+ end
242
+ context 'current request url matches url' do
243
+ before(:each) do
244
+ @template.stub!(:current_page? => true)
245
+ end
246
+ it "should test with the item's url" do
247
+ @template.should_receive(:current_page?).with('url')
248
+ @item.send(:selected_by_url?)
249
+ end
250
+ it {@item.send(:selected_by_url?).should be_true}
251
+ end
252
+ context 'no match' do
253
+ before(:each) do
254
+ @template.stub!(:current_page? => false)
255
+ end
256
+ it {@item.send(:selected_by_url?).should be_false}
257
+ end
258
+ end
259
+ context 'template is not set' do
260
+ before(:each) do
261
+ SimpleNavigation.stub!(:template => nil)
262
+ end
263
+ it {@item.send(:selected_by_url?).should be_false}
264
+ end
265
+ end
266
+ end
267
+ context 'auto_highlight is turned off' do
268
+ before(:each) do
269
+ @item.stub!(:auto_highlight? => false)
270
+ end
271
+ it {@item.send(:selected_by_url?).should be_false}
272
+ end
273
+ end
274
+
275
+ describe 'root_path_match?' do
276
+ before(:each) do
277
+ @request = stub(:request)
278
+ @controller = stub(:controller, :request => @request)
279
+ SimpleNavigation.stub!(:controller => @controller)
280
+ end
281
+ it "should match if both url == /" do
282
+ @request.stub!(:path => '/')
283
+ @item.stub!(:url => '/')
284
+ @item.send(:root_path_match?).should be_true
285
+ end
286
+ it "should not match if item url is not /" do
287
+ @request.stub!(:path => '/')
288
+ @item.stub!(:url => '/bla')
289
+ @item.send(:root_path_match?).should be_false
290
+ end
291
+ it "should not match if request url is not /" do
292
+ @request.stub!(:path => '/bla')
293
+ @item.stub!(:url => '/')
294
+ @item.send(:root_path_match?).should be_false
295
+ end
296
+ it "should not match if urls do not match" do
297
+ @request.stub!(:path => '/bla')
298
+ @item.stub!(:url => '/bli')
299
+ @item.send(:root_path_match?).should be_false
300
+ end
301
+ end
302
+
303
+ describe 'auto_highlight?' do
304
+ before(:each) do
305
+ @global = stub(:config)
306
+ SimpleNavigation.stub!(:config => @global)
307
+ end
308
+ context 'global auto_hightlight on' do
309
+ before(:each) do
310
+ @global.stub!(:auto_highlight => true)
311
+ end
312
+ context 'container auto_hightlight on' do
313
+ before(:each) do
314
+ @item_container.stub!(:auto_highlight => true)
315
+ end
316
+ it {@item.send(:auto_highlight?).should be_true}
317
+ end
318
+ context 'container auto_hightlight off' do
319
+ before(:each) do
320
+ @item_container.stub!(:auto_highlight => false)
321
+ end
322
+ it {@item.send(:auto_highlight?).should be_false}
323
+ end
324
+ end
325
+ context 'global auto_hightlight off' do
326
+ before(:each) do
327
+ @global.stub!(:auto_highlight => false)
328
+ end
329
+ context 'container auto_hightlight on' do
330
+ before(:each) do
331
+ @item_container.stub!(:auto_highlight => true)
332
+ end
333
+ it {@item.send(:auto_highlight?).should be_false}
334
+ end
335
+ context 'container auto_hightlight off' do
336
+ before(:each) do
337
+ @item_container.stub!(:auto_highlight => false)
338
+ end
339
+ it {@item.send(:auto_highlight?).should be_false}
340
+ end
341
+ end
342
+ end
148
343
 
149
344
  end
@@ -4,7 +4,7 @@ describe SimpleNavigation::Renderer::Base do
4
4
  before(:each) do
5
5
  @controller = stub(:controller)
6
6
  SimpleNavigation.stub!(:controller).and_return(@controller)
7
- @base_renderer = SimpleNavigation::Renderer::Base.new(:current_primary, :current_secondary)
7
+ @base_renderer = SimpleNavigation::Renderer::Base.new
8
8
  end
9
9
  it "should inclue ActionView::Helpers::UrlHelper" do
10
10
  @base_renderer.should respond_to(:link_to)
@@ -20,8 +20,6 @@ describe SimpleNavigation::Renderer::Base do
20
20
  end
21
21
 
22
22
  describe 'initialize' do
23
- it {@base_renderer.current_navigation.should == :current_primary}
24
- it {@base_renderer.current_sub_navigation.should == :current_secondary}
25
23
  it {@base_renderer.controller.should == @controller}
26
24
  end
27
25
 
@@ -53,4 +51,10 @@ describe SimpleNavigation::Renderer::Base do
53
51
  end
54
52
  end
55
53
 
54
+ describe 'render' do
55
+ it "be subclass responsability" do
56
+ lambda {@base_renderer.render(:container)}.should raise_error('subclass responsibility')
57
+ end
58
+ end
59
+
56
60
  end
@@ -7,34 +7,50 @@ describe SimpleNavigation::Renderer::List do
7
7
 
8
8
  def sub_items
9
9
  [
10
- SimpleNavigation::Item.new(:subnav1, 'subnav1', 'subnav1_url', {}, nil),
11
- SimpleNavigation::Item.new(:subnav2, 'subnav2', 'subnav2_url', {}, nil)
10
+ [:subnav1, 'subnav1', 'subnav1_url', {}, nil],
11
+ [:subnav2, 'subnav2', 'subnav2_url', {}, nil]
12
12
  ]
13
13
  end
14
14
 
15
15
  def primary_items
16
- @item1 = SimpleNavigation::Item.new(:users, 'users', 'first_url', {:id => 'my_id'}, nil)
17
- @item2 = SimpleNavigation::Item.new(:invoices, 'invoices', 'second_url', {}, nil)
18
- @item3 = SimpleNavigation::Item.new(:accounts, 'accounts', 'third_url', {:style => 'float:right'}, nil)
19
- @item2.instance_variable_set(:@sub_navigation, item_container(sub_items))
20
- [@item1, @item2, @item3]
16
+ [
17
+ [:users, 'users', 'first_url', {:id => 'my_id'}, nil],
18
+ [:invoices, 'invoices', 'second_url', {}, nil],
19
+ [:accounts, 'accounts', 'third_url', {:style => 'float:right'}, nil]
20
+ ]
21
21
  end
22
22
 
23
- def item_container(items)
24
- container = SimpleNavigation::ItemContainer.new
23
+ def primary_container
24
+ container = SimpleNavigation::ItemContainer.new(0)
25
25
  container.dom_id = 'nav_dom_id'
26
26
  container.dom_class = 'nav_dom_class'
27
- container.instance_variable_set(:@items, items)
27
+ @items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
28
+ @items.each {|i| i.stub!(:selected? => false)}
29
+ container.instance_variable_set(:@items, @items)
30
+ primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, subnav_container)}
28
31
  container
29
32
  end
30
33
 
31
- def primary_navigation
32
- @item_container = item_container(primary_items)
33
- @item_container
34
+ def primary_item(key)
35
+ yield @items.find {|i| i.key == key}
36
+ end
37
+
38
+ def select_item(key)
39
+ primary_item(key) {|item| item.stub!(:selected? => true)}
40
+ end
41
+
42
+ def subnav_container
43
+ container = SimpleNavigation::ItemContainer.new(1)
44
+ items = sub_items.map {|params| SimpleNavigation::Item.new(container, *params)}
45
+ items.each {|i| i.stub!(:selected? => false)}
46
+ container.instance_variable_set(:@items, items)
47
+ container
34
48
  end
35
49
 
36
- def render(current_navigation=nil, include_subnav=false)
37
- @renderer = SimpleNavigation::Renderer::List.new(current_navigation)
50
+ def render(current_nav=nil, include_subnav=false)
51
+ primary_navigation = primary_container
52
+ select_item(current_nav) if current_nav
53
+ @renderer = SimpleNavigation::Renderer::List.new
38
54
  HTML::Document.new(@renderer.render(primary_navigation, include_subnav)).root
39
55
  end
40
56
 
@@ -137,4 +137,87 @@ describe SimpleNavigation do
137
137
  it {SimpleNavigation.config.should == SimpleNavigation::Configuration.instance}
138
138
  end
139
139
 
140
+ describe 'current_navigation_for' do
141
+ before(:each) do
142
+ @controller = stub(:controller)
143
+ SimpleNavigation.stub!(:controller => @controller)
144
+ end
145
+ it "should access the correct instance_var in the controller" do
146
+ @controller.should_receive(:instance_variable_get).with(:@sn_current_navigation_1)
147
+ SimpleNavigation.current_navigation_for(1)
148
+ end
149
+ end
150
+
151
+ describe 'active_item_container_for' do
152
+ before(:each) do
153
+ @primary = stub(:primary)
154
+ SimpleNavigation.config.stub!(:primary_navigation => @primary)
155
+ end
156
+ it "should call active_item_container_for on the primary_navigation with the specified level" do
157
+ @primary.should_receive(:active_item_container_for).with(1)
158
+ SimpleNavigation.active_item_container_for 1
159
+ end
160
+ end
161
+
162
+ describe 'handle_explicit_navigation' do
163
+ def args(*args)
164
+ SimpleNavigation.stub!(:explicit_navigation_args => args.compact.empty? ? nil : args)
165
+ end
166
+
167
+ before(:each) do
168
+ @controller = stub(:controller)
169
+ SimpleNavigation.stub!(:controller => @controller)
170
+ end
171
+
172
+ context 'with explicit navigation set' do
173
+ context 'list of navigations' do
174
+ before(:each) do
175
+ args :first, :second, :third
176
+ end
177
+ it "should set the correct instance var in the controller" do
178
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_3, :third)
179
+ SimpleNavigation.handle_explicit_navigation
180
+ end
181
+ end
182
+ context 'single navigation' do
183
+ before(:each) do
184
+ @primary = stub(:primary, :level_for_item => 2)
185
+ SimpleNavigation.stub!(:primary_navigation => @primary)
186
+ args :key
187
+ end
188
+ it "should set the correct instance var in the controller" do
189
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
190
+ SimpleNavigation.handle_explicit_navigation
191
+ end
192
+ end
193
+ context 'hash with level' do
194
+ before(:each) do
195
+ args :level_2 => :key
196
+ end
197
+ it "should set the correct instance var in the controller" do
198
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
199
+ SimpleNavigation.handle_explicit_navigation
200
+ end
201
+ end
202
+ context 'hash with multiple_levels' do
203
+ before(:each) do
204
+ args :level_2 => :key, :level_1 => :bla
205
+ end
206
+ it "should set the correct instance var in the controller" do
207
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
208
+ SimpleNavigation.handle_explicit_navigation
209
+ end
210
+ end
211
+ end
212
+ context 'without explicit navigation set' do
213
+ before(:each) do
214
+ args nil
215
+ end
216
+ it "should not set the current_navigation instance var in the controller" do
217
+ @controller.should_not_receive(:instance_variable_set)
218
+ SimpleNavigation.handle_explicit_navigation
219
+ end
220
+ end
221
+ end
222
+
140
223
  end
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.4.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andi Schacke
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-23 00:00:00 +02:00
12
+ date: 2009-10-10 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app.
16
+ description: With the simple-navigation gem installed you can easily create multilevel navigations for your Ruby on Rails applications. The navigation is defined in a single configuration file. It supports automatic as well as explicit highlighting of the currently active navigation.
17
17
  email: andreas.schacke@gmail.com
18
18
  executables: []
19
19
 
@@ -80,7 +80,7 @@ rubyforge_project: andi
80
80
  rubygems_version: 1.3.5
81
81
  signing_key:
82
82
  specification_version: 3
83
- summary: Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app.
83
+ summary: Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Ruby on Rails application.
84
84
  test_files:
85
85
  - spec/lib/simple_navigation/configuration_spec.rb
86
86
  - spec/lib/simple_navigation/controller_methods_spec.rb