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,58 @@
1
+ require 'spec_helper'
2
+ require 'html/document' unless defined? HTML::Document
3
+
4
+ describe SimpleNavigation::Renderer::Links do
5
+
6
+
7
+ describe 'render' do
8
+
9
+ def render(current_nav=nil, options={:level => :all})
10
+ primary_navigation = primary_container
11
+ select_item(current_nav) if current_nav
12
+ setup_renderer_for SimpleNavigation::Renderer::Links, :rails, options
13
+ HTML::Document.new(@renderer.render(primary_navigation)).root
14
+ end
15
+
16
+ context 'regarding result' do
17
+
18
+ it "should render a div-tag around the items" do
19
+ HTML::Selector.new('div').select(render).should have(1).entries
20
+ end
21
+ it "the rendered div-tag should have the specified dom_id" do
22
+ HTML::Selector.new('div#nav_dom_id').select(render).should have(1).entries
23
+ end
24
+ it "the rendered div-tag should have the specified class" do
25
+ HTML::Selector.new('div.nav_dom_class').select(render).should have(1).entries
26
+ end
27
+ it "should render an a-tag for each item" do
28
+ HTML::Selector.new('a').select(render).should have(3).entries
29
+ end
30
+ it "should pass the specified html_options to the a element" do
31
+ HTML::Selector.new('a[style=float:right]').select(render).should have(1).entries
32
+ end
33
+ it "should give the a-tag the id specified in the html_options" do
34
+ HTML::Selector.new('a#my_id').select(render).should have(1).entries
35
+ end
36
+ it "should give the a-tag the default id (stringified key) if no id is specified in the html_options" do
37
+ HTML::Selector.new('a#invoices').select(render).should have(1).entries
38
+ end
39
+ it "should not apply the the default id where there is an id specified in the html_options" do
40
+ HTML::Selector.new('a#users').select(render).should be_empty
41
+ end
42
+
43
+ context 'with current_navigation set' do
44
+ it "should mark the matching a-item as selected (with the css_class specified in configuration)" do
45
+ HTML::Selector.new('a.selected').select(render(:invoices)).should have(1).entries
46
+ end
47
+ end
48
+
49
+ context 'without current_navigation set' do
50
+ it "should not mark any of the items as selected" do
51
+ HTML::Selector.new('a.selected').select(render).should be_empty
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,189 @@
1
+ require 'spec_helper'
2
+ require 'html/document' unless defined? HTML::Document
3
+
4
+ describe SimpleNavigation::Renderer::List do
5
+
6
+ describe 'render' do
7
+
8
+ def render(current_nav=nil, options={:level => :all})
9
+ primary_navigation = primary_container
10
+ select_item(current_nav) if current_nav
11
+ setup_renderer_for SimpleNavigation::Renderer::List, :rails, options
12
+ HTML::Document.new(@renderer.render(primary_navigation)).root
13
+ end
14
+
15
+ context 'regarding result' do
16
+
17
+ it "should render a ul-tag around the items" do
18
+ HTML::Selector.new('ul').select(render).should have(1).entries
19
+ end
20
+ it "the rendered ul-tag should have the specified dom_id" do
21
+ HTML::Selector.new('ul#nav_dom_id').select(render).should have(1).entries
22
+ end
23
+ it "the rendered ul-tag should have the specified class" do
24
+ HTML::Selector.new('ul.nav_dom_class').select(render).should have(1).entries
25
+ end
26
+ it "should render a li tag for each item" do
27
+ HTML::Selector.new('li').select(render).should have(3).entries
28
+ end
29
+ it "should render an a-tag inside each li-tag" do
30
+ HTML::Selector.new('li a').select(render).should have(3).entries
31
+ end
32
+
33
+ context 'concerning html attributes' do
34
+ context 'default case (no options defined for link tag)' do
35
+ it "should pass the specified html_options to the li element" do
36
+ HTML::Selector.new('li[style=float:right]').select(render).should have(1).entries
37
+ end
38
+ it "should give the li the id specified in the html_options" do
39
+ HTML::Selector.new('li#my_id').select(render).should have(1).entries
40
+ end
41
+ it "should give the li the default id (stringified key) if no id is specified in the html_options" do
42
+ HTML::Selector.new('ul li#invoices').select(render).should have(1).entries
43
+ end
44
+ it "should not apply the the default id where there is an id specified in the html_options" do
45
+ HTML::Selector.new('ul li#users').select(render).should be_empty
46
+ end
47
+ end
48
+ context 'with attributes defined for the link tag as well' do
49
+ it "should add the link attributes to the link" do
50
+ HTML::Selector.new('a[style=float:left]').select(render).should have(1).entries
51
+ end
52
+ it "should add the li attributes to the li element" do
53
+ HTML::Selector.new('li[style=float:right]').select(render).should have(1).entries
54
+ end
55
+ it "should give the li the default id (stringified key) if no id is specified in the html_options for the li-element" do
56
+ HTML::Selector.new('ul li#invoices').select(render).should have(1).entries
57
+ end
58
+ it "should not apply the the default id where there is an id specified in the html_options for th li-element" do
59
+ HTML::Selector.new('ul li#users').select(render).should be_empty
60
+ end
61
+ end
62
+ end
63
+
64
+ context 'with current_navigation set' do
65
+ it "should mark the matching li-item as selected (with the css_class specified in configuration)" do
66
+ HTML::Selector.new('li.selected').select(render(:invoices)).should have(1).entries
67
+ end
68
+ it "should also mark the links inside the selected li's as selected" do
69
+ HTML::Selector.new('li.selected a.selected').select(render(:invoices)).should have(1).entries
70
+ end
71
+ end
72
+
73
+ context 'without current_navigation set' do
74
+ it "should not mark any of the items as selected" do
75
+ HTML::Selector.new('li.selected').select(render).should be_empty
76
+ end
77
+ it "should not mark any links as selected" do
78
+ HTML::Selector.new('a.selected').select(render).should be_empty
79
+ end
80
+ end
81
+
82
+ context 'nested sub_navigation' do
83
+ it "should nest the current_primary's subnavigation inside the selected li-element" do
84
+ HTML::Selector.new('li.selected ul li').select(render(:invoices)).should have(2).entries
85
+ end
86
+ it "should be possible to identify sub items using an html selector (using ids)" do
87
+ HTML::Selector.new('#invoices #subnav1').select(render(:invoices)).should have(1).entries
88
+ end
89
+ context 'expand_all => false' do
90
+ it "should not render the invoices submenu if the user-primary is active" do
91
+ HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => false)).should be_empty
92
+ HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => false)).should be_empty
93
+ end
94
+ end
95
+
96
+ context 'expand_all => true' do
97
+ it "should render the invoices submenu even if the user-primary is active" do
98
+ HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
99
+ HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ context 'skip_if_empty' do
106
+
107
+ def render_container(options={})
108
+ setup_renderer_for SimpleNavigation::Renderer::List, :rails, options
109
+ HTML::Document.new(@renderer.render(@container)).root
110
+ end
111
+
112
+ context 'container is empty' do
113
+ before(:each) do
114
+ @container = SimpleNavigation::ItemContainer.new(0)
115
+ end
116
+ context 'skip_if_empty is true' do
117
+ it "should not render a ul tag for the empty container" do
118
+ HTML::Selector.new('ul').select(render_container(:skip_if_empty => true)).should be_empty
119
+ end
120
+ end
121
+ context 'skip_if_empty is false' do
122
+ it "should render a ul tag for the empty container" do
123
+ HTML::Selector.new('ul').select(render_container(:skip_if_empty => false)).should have(1).entry
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'container is not empty' do
129
+ before(:each) do
130
+ @container = primary_container
131
+ end
132
+ context 'skip_if_empty is true' do
133
+ it "should render a ul tag for the container" do
134
+ HTML::Selector.new('ul').select(render_container(:skip_if_empty => true)).should have(1).entry
135
+ end
136
+ end
137
+ context 'skip_if_empty is false' do
138
+ it "should render a ul tag for the container" do
139
+ HTML::Selector.new('ul').select(render_container(:skip_if_empty => false)).should have(1).entry
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ describe 'link_options_for' do
147
+ before(:each) do
148
+ setup_renderer_for SimpleNavigation::Renderer::List, :rails, {}
149
+ end
150
+ context 'no link options specified' do
151
+ context 'method specified' do
152
+ context 'item selected' do
153
+ before(:each) do
154
+ @item = stub(:item, :method => :delete, :selected_class => 'selected', :html_options => {})
155
+ end
156
+ it {@renderer.send(:link_options_for, @item).should == {:method => :delete, :class => 'selected'}}
157
+ end
158
+ context 'item not selected' do
159
+ before(:each) do
160
+ @item = stub(:item, :method => :delete, :selected_class => nil, :html_options => {})
161
+ end
162
+ it {@renderer.send(:link_options_for, @item).should == {:method => :delete}}
163
+ end
164
+ end
165
+ context 'method not specified' do
166
+ context 'item selected' do
167
+ before(:each) do
168
+ @item = stub(:item, :method => nil, :selected_class => 'selected', :html_options => {})
169
+ end
170
+ it {@renderer.send(:link_options_for, @item).should == {:class => 'selected'}}
171
+ end
172
+ context 'item not selected' do
173
+ before(:each) do
174
+ @item = stub(:item, :method => nil, :selected_class => nil, :html_options => {})
175
+ end
176
+ it {@renderer.send(:link_options_for, @item).should == {}}
177
+ end
178
+ end
179
+ end
180
+ context 'link options specified' do
181
+ before(:each) do
182
+ @item = stub(:item, :method => :delete, :selected_class => 'selected', :html_options => {:link => {:class => 'link_class', :style => 'float:left'}})
183
+ end
184
+ it {@renderer.send(:link_options_for, @item).should == {:method => :delete, :class => 'link_class selected', :style => 'float:left'}}
185
+ end
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,307 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleNavigation do
4
+
5
+ before(:each) do
6
+ SimpleNavigation.config_file_path = 'path_to_config'
7
+ end
8
+
9
+ describe 'config_file_name' do
10
+ context 'for :default navigation_context' do
11
+ it "should return the name of the default config file" do
12
+ SimpleNavigation.config_file_name.should == 'navigation.rb'
13
+ end
14
+ end
15
+ context 'for other navigation_context' do
16
+ it "should return the name of the config file matching the specified context" do
17
+ SimpleNavigation.config_file_name(:my_other_context).should == 'my_other_context_navigation.rb'
18
+ end
19
+ it "should convert camelcase-contexts to underscore" do
20
+ SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'why_would_you_do_this_navigation.rb'
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'config_file_path=' do
26
+ before(:each) do
27
+ SimpleNavigation.config_file_paths = ['existing_path']
28
+ end
29
+ it "should override the config_file_paths" do
30
+ SimpleNavigation.config_file_path = 'new_path'
31
+ SimpleNavigation.config_file_paths.should == ['new_path']
32
+ end
33
+ end
34
+
35
+ describe 'config_file' do
36
+ context 'no config_file_paths are set' do
37
+ before(:each) do
38
+ SimpleNavigation.config_file_paths = []
39
+ end
40
+ it "should return nil" do
41
+ SimpleNavigation.config_file.should be_nil
42
+ end
43
+ end
44
+ context 'one config_file_path is set' do
45
+ before(:each) do
46
+ SimpleNavigation.config_file_paths = ['my_config_file_path']
47
+ end
48
+ context 'requested config file does exist' do
49
+ before(:each) do
50
+ File.stub!(:exists? => true)
51
+ end
52
+ it "should return the path to the config_file" do
53
+ SimpleNavigation.config_file.should == 'my_config_file_path/navigation.rb'
54
+ end
55
+ end
56
+ context 'requested config file does not exist' do
57
+ before(:each) do
58
+ File.stub!(:exists? => false)
59
+ end
60
+ it "should return nil" do
61
+ SimpleNavigation.config_file.should be_nil
62
+ end
63
+ end
64
+ end
65
+ context 'multiple config_file_paths are set' do
66
+ before(:each) do
67
+ SimpleNavigation.config_file_paths = ['first_path', 'second_path']
68
+ end
69
+ context 'requested config file does exist' do
70
+ before(:each) do
71
+ File.stub!(:exists? => true)
72
+ end
73
+ it "should return the path to the first matching config_file" do
74
+ SimpleNavigation.config_file.should == 'first_path/navigation.rb'
75
+ end
76
+ end
77
+ context 'requested config file does not exist' do
78
+ before(:each) do
79
+ File.stub!(:exists? => false)
80
+ end
81
+ it "should return nil" do
82
+ SimpleNavigation.config_file.should be_nil
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ describe 'config_file?' do
89
+ context 'config_file present' do
90
+ before(:each) do
91
+ SimpleNavigation.stub!(:config_file => 'file')
92
+ end
93
+ it {SimpleNavigation.config_file?.should be_true}
94
+ end
95
+ context 'config_file not present' do
96
+ before(:each) do
97
+ SimpleNavigation.stub!(:config_file => nil)
98
+ end
99
+ it {SimpleNavigation.config_file?.should be_false}
100
+ end
101
+ end
102
+
103
+ describe 'self.default_config_file_path' do
104
+ before(:each) do
105
+ SimpleNavigation.stub!(:root => 'root')
106
+ end
107
+ it {SimpleNavigation.default_config_file_path.should == 'root/config'}
108
+ end
109
+
110
+ describe 'regarding renderers' do
111
+ it "should have registered the builtin renderers by default" do
112
+ SimpleNavigation.registered_renderers.should_not be_empty
113
+ end
114
+
115
+ describe 'register_renderer' do
116
+ before(:each) do
117
+ @renderer = stub(:renderer)
118
+ end
119
+ it "should add the specified renderer to the list of renderers" do
120
+ SimpleNavigation.register_renderer(:my_renderer => @renderer)
121
+ SimpleNavigation.registered_renderers[:my_renderer].should == @renderer
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ describe 'set_env' do
128
+ before(:each) do
129
+ SimpleNavigation.config_file_paths = []
130
+ SimpleNavigation.stub!(:default_config_file_path => 'default_path')
131
+ SimpleNavigation.set_env('root', 'my_env')
132
+ end
133
+ it "should set the root" do
134
+ SimpleNavigation.root.should == 'root'
135
+ end
136
+ it "should set the environment" do
137
+ SimpleNavigation.environment.should == 'my_env'
138
+ end
139
+ it "should add the default-config path to the list of config_file_paths" do
140
+ SimpleNavigation.config_file_paths.should == ['default_path']
141
+ end
142
+ end
143
+
144
+ describe 'load_config' do
145
+ context 'config_file_path is set' do
146
+ before(:each) do
147
+ SimpleNavigation.stub!(:config_file => 'path_to_config_file')
148
+ end
149
+ context 'config_file does exist' do
150
+ before(:each) do
151
+ SimpleNavigation.stub!(:config_file? => true)
152
+ IO.stub!(:read => 'file_content')
153
+ end
154
+ it "should not raise an error" do
155
+ lambda{SimpleNavigation.load_config}.should_not raise_error
156
+ end
157
+ it "should read the specified config file from disc" do
158
+ IO.should_receive(:read).with('path_to_config_file')
159
+ SimpleNavigation.load_config
160
+ end
161
+ it "should store the read content in the module (default context)" do
162
+ SimpleNavigation.should_receive(:config_file).with(:default)
163
+ SimpleNavigation.load_config
164
+ SimpleNavigation.config_files[:default].should == 'file_content'
165
+ end
166
+ it "should store the content in the module (non default context)" do
167
+ SimpleNavigation.should_receive(:config_file).with(:my_context)
168
+ SimpleNavigation.load_config(:my_context)
169
+ SimpleNavigation.config_files[:my_context].should == 'file_content'
170
+ end
171
+ end
172
+
173
+ context 'config_file does not exist' do
174
+ before(:each) do
175
+ SimpleNavigation.stub!(:config_file? => false)
176
+ end
177
+ it {lambda{SimpleNavigation.load_config}.should raise_error}
178
+ end
179
+ end
180
+
181
+ context 'config_file_path is not set' do
182
+ before(:each) do
183
+ SimpleNavigation.config_file_path = nil
184
+ end
185
+ it {lambda{SimpleNavigation.load_config}.should raise_error}
186
+ end
187
+
188
+ describe 'regarding caching of the config-files' do
189
+ before(:each) do
190
+ IO.stub!(:read).and_return('file_content')
191
+ SimpleNavigation.config_file_path = 'path_to_config'
192
+ File.stub!(:exists? => true)
193
+ end
194
+ context "environment undefined" do
195
+ before(:each) do
196
+ SimpleNavigation.stub!(:environment => nil)
197
+ end
198
+ it "should load the config file twice" do
199
+ IO.should_receive(:read).twice
200
+ SimpleNavigation.load_config
201
+ SimpleNavigation.load_config
202
+ end
203
+ end
204
+ context "environment defined" do
205
+ before(:each) do
206
+ SimpleNavigation.stub!(:environment => 'production')
207
+ end
208
+ context "environment=production" do
209
+ it "should load the config file only once" do
210
+ IO.should_receive(:read).once
211
+ SimpleNavigation.load_config
212
+ SimpleNavigation.load_config
213
+ end
214
+ end
215
+
216
+ context "environment=development" do
217
+ before(:each) do
218
+ SimpleNavigation.stub!(:environment => 'development')
219
+ end
220
+ it "should load the config file twice" do
221
+ IO.should_receive(:read).twice
222
+ SimpleNavigation.load_config
223
+ SimpleNavigation.load_config
224
+ end
225
+ end
226
+
227
+ context "environment=test" do
228
+ before(:each) do
229
+ SimpleNavigation.stub!(:environment => 'test')
230
+ end
231
+ it "should load the config file twice" do
232
+ IO.should_receive(:read).twice
233
+ SimpleNavigation.load_config
234
+ SimpleNavigation.load_config
235
+ end
236
+ end
237
+ end
238
+ after(:each) do
239
+ SimpleNavigation.config_files = {}
240
+ end
241
+ end
242
+ end
243
+
244
+ describe 'config' do
245
+ it {SimpleNavigation.config.should == SimpleNavigation::Configuration.instance}
246
+ end
247
+
248
+ describe 'active_item_container_for' do
249
+ before(:each) do
250
+ @primary = stub(:primary)
251
+ SimpleNavigation.config.stub!(:primary_navigation => @primary)
252
+ end
253
+ context 'level is :all' do
254
+ it "should return the primary_navigation" do
255
+ SimpleNavigation.active_item_container_for(:all).should == @primary
256
+ end
257
+ end
258
+ context 'level is :leaves' do
259
+ it "should return the currently active leaf-container" do
260
+ @primary.should_receive(:active_leaf_container)
261
+ SimpleNavigation.active_item_container_for(:leaves)
262
+ end
263
+ end
264
+ context 'level is a Range' do
265
+ it "should take the min of the range to lookup the active container" do
266
+ @primary.should_receive(:active_item_container_for).with(2)
267
+ SimpleNavigation.active_item_container_for(2..3)
268
+ end
269
+ end
270
+ context 'level is an Integer' do
271
+ it "should consider the Integer to lookup the active container" do
272
+ @primary.should_receive(:active_item_container_for).with(1)
273
+ SimpleNavigation.active_item_container_for(1)
274
+ end
275
+ end
276
+ context 'level is something else' do
277
+ it "should raise an ArgumentError" do
278
+ lambda {SimpleNavigation.active_item_container_for('something else')}.should raise_error(ArgumentError)
279
+ end
280
+ end
281
+ end
282
+
283
+ describe 'load_adapter' do
284
+ context 'Rails' do
285
+ before(:each) do
286
+ SimpleNavigation.stub!(:framework => :rails)
287
+ SimpleNavigation.load_adapter
288
+ end
289
+ it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Rails}
290
+ end
291
+ context 'Padrino' do
292
+ before(:each) do
293
+ SimpleNavigation.stub!(:framework => :padrino)
294
+ SimpleNavigation.load_adapter
295
+ end
296
+ it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Padrino}
297
+ end
298
+ context 'Sinatra' do
299
+ before(:each) do
300
+ SimpleNavigation.stub!(:framework => :sinatra)
301
+ SimpleNavigation.load_adapter
302
+ end
303
+ it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Sinatra}
304
+ end
305
+ end
306
+
307
+ end
@@ -0,0 +1,96 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require 'rubygems'
3
+ require 'rspec'
4
+ require 'action_controller'
5
+
6
+ module Rails
7
+ module VERSION
8
+ MAJOR = 2
9
+ end
10
+ end unless defined? Rails
11
+
12
+ $:.unshift File.dirname(__FILE__)
13
+ $:.unshift File.join(File.dirname(__FILE__), '../lib')
14
+
15
+ require 'simple_navigation'
16
+
17
+ # SimpleNavigation.root = './'
18
+ RAILS_ROOT = './' unless defined?(RAILS_ROOT)
19
+ RAILS_ENV = 'test' unless defined?(RAILS_ENV)
20
+
21
+
22
+ RSpec.configure do |config|
23
+ # == Mock Framework
24
+ #
25
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
26
+ #
27
+ # config.mock_with :mocha
28
+ # config.mock_with :flexmock
29
+ # config.mock_with :rr
30
+ config.mock_with :rspec
31
+
32
+ end
33
+
34
+ # spec helper methods
35
+ def sub_items
36
+ [
37
+ [:subnav1, 'subnav1', 'subnav1_url', {}],
38
+ [:subnav2, 'subnav2', 'subnav2_url', {}]
39
+ ]
40
+ end
41
+
42
+ def primary_items
43
+ [
44
+ [:users, 'users', 'first_url', {:id => 'my_id'}],
45
+ [:invoices, 'invoices', 'second_url', {}],
46
+ [:accounts, 'accounts', 'third_url', {:style => 'float:right', :link => {:style => 'float:left'}}]
47
+ ]
48
+ end
49
+
50
+ def primary_container
51
+ container = SimpleNavigation::ItemContainer.new(1)
52
+ container.dom_id = 'nav_dom_id'
53
+ container.dom_class = 'nav_dom_class'
54
+ @items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
55
+ @items.each {|i| i.stub!(:selected? => false)}
56
+ container.instance_variable_set(:@items, @items)
57
+ primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, subnav_container)}
58
+ container
59
+ end
60
+
61
+ def primary_item(key)
62
+ yield @items.find {|i| i.key == key}
63
+ end
64
+
65
+ def select_item(key)
66
+ if(key == :subnav1)
67
+ select_item(:invoices)
68
+ primary_item(:invoices) do |item|
69
+ item.instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}.stub!(:selected? => true)
70
+ end
71
+ else
72
+ primary_item(key) {|item| item.stub!(:selected? => true) unless item.frozen?}
73
+ end
74
+ end
75
+
76
+ def subnav_container
77
+ container = SimpleNavigation::ItemContainer.new(2)
78
+ items = sub_items.map {|params| SimpleNavigation::Item.new(container, *params)}
79
+ items.each {|i| i.stub!(:selected? => false)}
80
+ container.instance_variable_set(:@items, items)
81
+ container
82
+ end
83
+
84
+ def setup_renderer_for(renderer_class, framework, options)
85
+ setup_adapter_for framework
86
+ @renderer = renderer_class.new(options)
87
+ end
88
+
89
+ def setup_adapter_for(framework)
90
+ adapter = case framework
91
+ when :rails
92
+ SimpleNavigation::Adapters::Rails.new(stub(:context, :view_context => ActionView::Base.new))
93
+ end
94
+ SimpleNavigation.stub!(:adapter => adapter)
95
+ adapter
96
+ end