simple-navigation-ext 0.0.1

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.
Files changed (46) hide show
  1. data/CHANGELOG +177 -0
  2. data/README +22 -0
  3. data/Rakefile +48 -0
  4. data/VERSION +1 -0
  5. data/generators/navigation_config/USAGE +1 -0
  6. data/generators/navigation_config/navigation_config_generator.rb +8 -0
  7. data/generators/navigation_config/templates/config/navigation.rb +67 -0
  8. data/lib/generators/navigation_config/navigation_config_generator.rb +12 -0
  9. data/lib/simple-navigation.rb +1 -0
  10. data/lib/simple_navigation/adapters/base.rb +37 -0
  11. data/lib/simple_navigation/adapters/padrino.rb +20 -0
  12. data/lib/simple_navigation/adapters/rails.rb +94 -0
  13. data/lib/simple_navigation/adapters/sinatra.rb +68 -0
  14. data/lib/simple_navigation/adapters.rb +9 -0
  15. data/lib/simple_navigation/core/configuration.rb +70 -0
  16. data/lib/simple_navigation/core/item.rb +117 -0
  17. data/lib/simple_navigation/core/item_adapter.rb +63 -0
  18. data/lib/simple_navigation/core/item_container.rb +146 -0
  19. data/lib/simple_navigation/core/items_provider.rb +35 -0
  20. data/lib/simple_navigation/core.rb +5 -0
  21. data/lib/simple_navigation/rails_controller_methods.rb +144 -0
  22. data/lib/simple_navigation/rendering/helpers.rb +82 -0
  23. data/lib/simple_navigation/rendering/renderer/base.rb +71 -0
  24. data/lib/simple_navigation/rendering/renderer/breadcrumbs.rb +37 -0
  25. data/lib/simple_navigation/rendering/renderer/links.rb +25 -0
  26. data/lib/simple_navigation/rendering/renderer/list.rb +47 -0
  27. data/lib/simple_navigation/rendering.rb +10 -0
  28. data/lib/simple_navigation.rb +162 -0
  29. data/rails/init.rb +1 -0
  30. data/spec/lib/simple_navigation/adapters/padrino_spec.rb +29 -0
  31. data/spec/lib/simple_navigation/adapters/rails_spec.rb +284 -0
  32. data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +60 -0
  33. data/spec/lib/simple_navigation/core/configuration_spec.rb +122 -0
  34. data/spec/lib/simple_navigation/core/item_adapter_spec.rb +209 -0
  35. data/spec/lib/simple_navigation/core/item_container_spec.rb +396 -0
  36. data/spec/lib/simple_navigation/core/item_spec.rb +513 -0
  37. data/spec/lib/simple_navigation/core/items_provider_spec.rb +60 -0
  38. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +249 -0
  39. data/spec/lib/simple_navigation/rendering/helpers_spec.rb +183 -0
  40. data/spec/lib/simple_navigation/rendering/renderer/base_spec.rb +199 -0
  41. data/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb +58 -0
  42. data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +58 -0
  43. data/spec/lib/simple_navigation/rendering/renderer/list_spec.rb +189 -0
  44. data/spec/lib/simple_navigation_spec.rb +307 -0
  45. data/spec/spec_helper.rb +96 -0
  46. metadata +158 -0
@@ -0,0 +1,396 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleNavigation::ItemContainer do
4
+ before(:each) do
5
+ @item_container = SimpleNavigation::ItemContainer.new
6
+ end
7
+ describe 'initialize' do
8
+ it "should set the renderer to the globally-configured renderer per default" do
9
+ SimpleNavigation::Configuration.instance.should_receive(:renderer)
10
+ @item_container = SimpleNavigation::ItemContainer.new
11
+ end
12
+ it "should have an empty items-array" do
13
+ @item_container = SimpleNavigation::ItemContainer.new
14
+ @item_container.items.should be_empty
15
+ end
16
+ end
17
+
18
+ describe 'items=' do
19
+ before(:each) do
20
+ @item = stub(:item)
21
+ @items = [@item]
22
+ @item_adapter = stub(:item_adapter).as_null_object
23
+ SimpleNavigation::ItemAdapter.stub(:new => @item_adapter)
24
+ @item_container.stub!(:should_add_item? => true)
25
+ end
26
+ it "should wrap each item in an ItemAdapter" do
27
+ SimpleNavigation::ItemAdapter.should_receive(:new)
28
+ @item_container.items = @items
29
+ end
30
+ context 'item should be added' do
31
+ before(:each) do
32
+ @item_container.stub!(:should_add_item? => true)
33
+ @simple_navigation_item = stub(:simple_navigation_item)
34
+ @item_adapter.stub!(:to_simple_navigation_item => @simple_navigation_item)
35
+ end
36
+ it "should convert the item to a SimpleNavigation::Item" do
37
+ @item_adapter.should_receive(:to_simple_navigation_item).with(@item_container)
38
+ @item_container.items = @items
39
+ end
40
+ it "should add the item to the items-collection" do
41
+ @item_container.items.should_receive(:<<).with(@simple_navigation_item)
42
+ @item_container.items = @items
43
+ end
44
+ end
45
+ context 'item should not be added' do
46
+ before(:each) do
47
+ @item_container.stub!(:should_add_item? => false)
48
+ end
49
+ it "should not convert the item to a SimpleNavigation::Item" do
50
+ @item_adapter.should_not_receive(:to_simple_navigation_item)
51
+ @item_container.items = @items
52
+ end
53
+ it "should not add the item to the items-collection" do
54
+ @item_container.items.should_not_receive(:<<)
55
+ @item_container.items = @items
56
+ end
57
+ end
58
+ end
59
+
60
+ describe 'selected?' do
61
+ before(:each) do
62
+ @item_1 = stub(:item, :selected? => false)
63
+ @item_2 = stub(:item, :selected? => false)
64
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
65
+ end
66
+ it "should return nil if no item is selected" do
67
+ @item_container.should_not be_selected
68
+ end
69
+ it "should return true if one item is selected" do
70
+ @item_1.stub!(:selected? => true)
71
+ @item_container.should be_selected
72
+ end
73
+ end
74
+
75
+ describe 'selected_item' do
76
+ before(:each) do
77
+ SimpleNavigation.stub!(:current_navigation_for => :nav)
78
+ @item_container.stub!(:[] => nil)
79
+ @item_1 = stub(:item, :selected? => false)
80
+ @item_2 = stub(:item, :selected? => false)
81
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
82
+ end
83
+ context 'navigation not explicitely set' do
84
+ context 'no item selected' do
85
+ it "should return nil" do
86
+ @item_container.selected_item.should be_nil
87
+ end
88
+ end
89
+ context 'one item selected' do
90
+ before(:each) do
91
+ @item_1.stub!(:selected? => true)
92
+ end
93
+ it "should return the selected item" do
94
+ @item_container.selected_item.should == @item_1
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe 'selected_sub_navigation?' do
101
+ context 'with an item selected' do
102
+ before(:each) do
103
+ @selected_item = stub(:selected_item)
104
+ @item_container.stub!(:selected_item => @selected_item)
105
+ end
106
+ context 'selected item has sub_navigation' do
107
+ before(:each) do
108
+ @sub_navigation = stub(:sub_navigation)
109
+ @selected_item.stub!(:sub_navigation => @sub_navigation)
110
+ end
111
+ it {@item_container.send(:selected_sub_navigation?).should be_true}
112
+ end
113
+ context 'selected item does not have sub_navigation' do
114
+ before(:each) do
115
+ @selected_item.stub!(:sub_navigation => nil)
116
+ end
117
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
118
+ end
119
+ end
120
+ context 'without an item selected' do
121
+ before(:each) do
122
+ @item_container.stub!(:selected_item => nil)
123
+ end
124
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
125
+ end
126
+
127
+ end
128
+
129
+ describe 'active_item_container_for' do
130
+ context "the desired level is the same as the container's" do
131
+ it {@item_container.active_item_container_for(1).should == @item_container}
132
+ end
133
+ context "the desired level is different than the container's" do
134
+ context 'with no selected subnavigation' do
135
+ before(:each) do
136
+ @item_container.stub!(:selected_sub_navigation? => false)
137
+ end
138
+ it {@item_container.active_item_container_for(2).should be_nil}
139
+ end
140
+ context 'with selected subnavigation' do
141
+ before(:each) do
142
+ @item_container.stub!(:selected_sub_navigation? => true)
143
+ @sub_nav = stub(:sub_nav)
144
+ @selected_item = stub(:selected_item)
145
+ @item_container.stub!(:selected_item => @selected_item)
146
+ @selected_item.stub!(:sub_navigation => @sub_nav)
147
+ end
148
+ it "should call recursively on the sub_navigation" do
149
+ @sub_nav.should_receive(:active_item_container_for).with(2)
150
+ @item_container.active_item_container_for(2)
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ describe 'active_leaf_container' do
157
+ context 'the current container has a selected subnavigation' do
158
+ before(:each) do
159
+ @item_container.stub!(:selected_sub_navigation? => true)
160
+ @sub_nav = stub(:sub_nav)
161
+ @selected_item = stub(:selected_item)
162
+ @item_container.stub!(:selected_item => @selected_item)
163
+ @selected_item.stub!(:sub_navigation => @sub_nav)
164
+ end
165
+ it "should call recursively on the sub_navigation" do
166
+ @sub_nav.should_receive(:active_leaf_container)
167
+ @item_container.active_leaf_container
168
+ end
169
+ end
170
+ context 'the current container is the leaf already' do
171
+ before(:each) do
172
+ @item_container.stub!(:selected_sub_navigation? => false)
173
+ end
174
+ it "should return itsself" do
175
+ @item_container.active_leaf_container.should == @item_container
176
+ end
177
+ end
178
+ end
179
+
180
+ describe 'item' do
181
+
182
+ context 'unconditional item' do
183
+
184
+ before(:each) do
185
+ @item_container.stub!(:should_add_item?).and_return(true)
186
+ @options = {}
187
+ end
188
+
189
+ context 'block given' do
190
+ before(:each) do
191
+ @sub_container = stub(:sub_container)
192
+ SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
193
+ end
194
+
195
+ it "should should yield an new ItemContainer" do
196
+ @item_container.item('key', 'name', 'url', @options) do |container|
197
+ container.should == @sub_container
198
+ end
199
+ end
200
+ it "should create a new Navigation-Item with the given params and the specified block" do
201
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil, &@proc)
202
+ @item_container.item('key', 'name', 'url', @options, &@proc)
203
+ end
204
+ it "should add the created item to the list of items" do
205
+ @item_container.items.should_receive(:<<)
206
+ @item_container.item('key', 'name', 'url', @options) {}
207
+ end
208
+ end
209
+
210
+ context 'no block given' do
211
+ it "should create a new Navigation_item with the given params and nil as sub_navi" do
212
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil)
213
+ @item_container.item('key', 'name', 'url', @options)
214
+ end
215
+ it "should add the created item to the list of items" do
216
+ @item_container.items.should_receive(:<<)
217
+ @item_container.item('key', 'name', 'url', @options)
218
+ end
219
+ end
220
+
221
+ end
222
+
223
+ context 'conditions given for item' do
224
+
225
+ context '"if" given' do
226
+
227
+ before(:each) do
228
+ @options = {:if => Proc.new {@condition}}
229
+ end
230
+
231
+ it "should remove if from options" do
232
+ @item_container.item('key', 'name', 'url', @options)
233
+ @options[:if].should be_nil
234
+ end
235
+
236
+ context 'if evals to true' do
237
+ before(:each) do
238
+ @condition = true
239
+ end
240
+ it "should create a new Navigation-Item" do
241
+ SimpleNavigation::Item.should_receive(:new)
242
+ @item_container.item('key', 'name', 'url', @options)
243
+ end
244
+ end
245
+
246
+ context 'if evals to false' do
247
+ before(:each) do
248
+ @condition = false
249
+ end
250
+ it "should not create a new Navigation-Item" do
251
+ SimpleNavigation::Item.should_not_receive(:new)
252
+ @item_container.item('key', 'name', 'url', @options)
253
+ end
254
+ end
255
+
256
+ context 'if is not a proc or method' do
257
+ it "should raise an error" do
258
+ lambda {@item_container.item('key', 'name', 'url', {:if => 'text'})}.should raise_error
259
+ end
260
+ end
261
+
262
+ context '"unless" given' do
263
+
264
+ before(:each) do
265
+ @options = {:unless => Proc.new {@condition}}
266
+ end
267
+
268
+
269
+ it "should remove unless from options" do
270
+ @item_container.item('key', 'name', 'url', @options)
271
+ @options[:unless].should be_nil
272
+ end
273
+
274
+ context 'unless evals to false' do
275
+ before(:each) do
276
+ @condition = false
277
+ end
278
+ it "should create a new Navigation-Item" do
279
+ SimpleNavigation::Item.should_receive(:new)
280
+ @item_container.item('key', 'name', 'url', @options)
281
+ end
282
+ end
283
+
284
+ context 'unless evals to true' do
285
+ before(:each) do
286
+ @condition = true
287
+ end
288
+ it "should not create a new Navigation-Item" do
289
+ SimpleNavigation::Item.should_not_receive(:new)
290
+ @item_container.item('key', 'name', 'url', @options)
291
+ end
292
+ end
293
+
294
+ end
295
+ end
296
+ end
297
+ end
298
+
299
+ describe '[]' do
300
+
301
+ before(:each) do
302
+ @item_container.item(:first, 'first', 'bla')
303
+ @item_container.item(:second, 'second', 'bla')
304
+ @item_container.item(:third, 'third', 'bla')
305
+ end
306
+
307
+ it "should return the item with the specified navi_key" do
308
+ @item_container[:second].name.should == 'second'
309
+ end
310
+ it "should return nil if no item exists for the specified navi_key" do
311
+ @item_container[:invalid].should be_nil
312
+ end
313
+ end
314
+
315
+ describe 'render' do
316
+ before(:each) do
317
+ @renderer_instance = stub(:renderer).as_null_object
318
+ @renderer_class = stub(:renderer_class, :new => @renderer_instance)
319
+ end
320
+ context 'renderer specified as option' do
321
+ context 'renderer-class specified' do
322
+ it "should instantiate the passed renderer_class with the options" do
323
+ @renderer_class.should_receive(:new).with(:renderer => @renderer_class)
324
+ end
325
+ it "should call render on the renderer and pass self" do
326
+ @renderer_instance.should_receive(:render).with(@item_container)
327
+ end
328
+ after(:each) do
329
+ @item_container.render(:renderer => @renderer_class)
330
+ end
331
+ end
332
+ context 'renderer-symbol specified' do
333
+ before(:each) do
334
+ SimpleNavigation.registered_renderers = {:my_renderer => @renderer_class}
335
+ end
336
+ it "should instantiate the passed renderer_class with the options" do
337
+ @renderer_class.should_receive(:new).with(:renderer => :my_renderer)
338
+ end
339
+ it "should call render on the renderer and pass self" do
340
+ @renderer_instance.should_receive(:render).with(@item_container)
341
+ end
342
+ after(:each) do
343
+ @item_container.render(:renderer => :my_renderer)
344
+ end
345
+ end
346
+ end
347
+ context 'no renderer specified' do
348
+ before(:each) do
349
+ @item_container.stub!(:renderer => @renderer_class)
350
+ @options = {}
351
+ end
352
+ it "should instantiate the container's renderer with the options" do
353
+ @renderer_class.should_receive(:new).with(@options)
354
+ end
355
+ it "should call render on the renderer and pass self" do
356
+ @renderer_instance.should_receive(:render).with(@item_container)
357
+ end
358
+ after(:each) do
359
+ @item_container.render(@options)
360
+ end
361
+ end
362
+ end
363
+
364
+ describe 'level_for_item' do
365
+ before(:each) do
366
+ @item_container.item(:p1, 'p1', 'p1')
367
+ @item_container.item(:p2, 'p2', 'p2') do |p2|
368
+ p2.item(:s1, 's1', 's1')
369
+ p2.item(:s2, 's2', 's2') do |s2|
370
+ s2.item(:ss1, 'ss1', 'ss1')
371
+ s2.item(:ss2, 'ss2', 'ss2')
372
+ end
373
+ p2.item(:s3, 's3', 's3')
374
+ end
375
+ @item_container.item(:p3, 'p3', 'p3')
376
+ end
377
+ it {@item_container.level_for_item(:p1).should == 1}
378
+ it {@item_container.level_for_item(:p3).should == 1}
379
+ it {@item_container.level_for_item(:s1).should == 2}
380
+ it {@item_container.level_for_item(:ss1).should == 3}
381
+ it {@item_container.level_for_item(:x).should be_nil}
382
+
383
+ end
384
+
385
+ describe 'empty?' do
386
+ it "should be empty if there are no items" do
387
+ @item_container.instance_variable_set(:@items, [])
388
+ @item_container.should be_empty
389
+ end
390
+ it "should not be empty if there are some items" do
391
+ @item_container.instance_variable_set(:@items, [stub(:item)])
392
+ @item_container.should_not be_empty
393
+ end
394
+ end
395
+
396
+ end