simple-navigation 1.4.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,21 +3,22 @@ module SimpleNavigation
3
3
 
4
4
  # Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
5
5
  # It adds the 'selected' class to li element AND the link inside the li element that is currently active.
6
+ #
6
7
  # If the sub navigation should be included, it renders another <ul> containing the sub navigation inside the active <li> element.
7
8
  #
8
- # By default, the renderer sets the item's key as dom_id for the rendered <li> element. The id can be explicitely specified by setting
9
- # the id in the html-options of the 'item' method in the config/navigation.rb file.
10
- class List < Renderer::Base
11
-
9
+ # If the SimpleNavigation.config.render_all_levels option is set to true, it always renders all levels of navigation (fully expanded tree).
10
+ #
11
+ # By default, the renderer sets the item's key as dom_id for the rendered <li> element unless the config option <tt>autogenerate_item_ids</tt> is set to false.
12
+ # The id can also be explicitely specified by setting the id in the html-options of the 'item' method in the config/navigation.rb file.
13
+ class List < SimpleNavigation::Renderer::Base
14
+
12
15
  def render(item_container, include_sub_navigation=false)
13
16
  list_content = item_container.items.inject([]) do |list, item|
14
- html_options = item.html_options(current_navigation)
15
- li_content = link_to(item.name, item.url, :class => item.selected_class(current_navigation), :method => item.method)
17
+ html_options = item.html_options
18
+ li_content = link_to(item.name, item.url, :class => item.selected_class, :method => item.method)
16
19
  if item.sub_navigation
17
- if SimpleNavigation.config.render_all_levels
18
- li_content << (item.sub_navigation.render(current_sub_navigation))
19
- else
20
- li_content << (item.sub_navigation.render(current_sub_navigation)) if include_sub_navigation && item.selected?(current_navigation)
20
+ if SimpleNavigation.config.render_all_levels || (include_sub_navigation && item.selected?)
21
+ li_content << (item.sub_navigation.render(include_sub_navigation))
21
22
  end
22
23
  end
23
24
  list << content_tag(:li, li_content, html_options)
@@ -10,9 +10,8 @@ require 'simple_navigation/renderer/list'
10
10
  # A plugin for generating a simple navigation. See README for resources on usage instructions.
11
11
  module SimpleNavigation
12
12
 
13
- mattr_accessor :config_files
14
- mattr_accessor :config_file_path
15
- mattr_accessor :controller
13
+ mattr_accessor :config_files, :config_file_path, :controller, :template, :explicit_current_navigation
14
+
16
15
  self.config_files = {}
17
16
 
18
17
  class << self
@@ -30,20 +29,83 @@ module SimpleNavigation
30
29
 
31
30
  # Returns the singleton instance of the SimpleNavigation::Configuration
32
31
  def config
33
- Configuration.instance
32
+ SimpleNavigation::Configuration.instance
34
33
  end
35
34
 
36
35
  # Returns the ItemContainer that contains the items for the primary navigation
37
36
  def primary_navigation
38
37
  config.primary_navigation
39
38
  end
40
-
39
+
41
40
  # Returns the path to the config_file for the given navigation_context
42
41
  def config_file_name(navigation_context = :default)
43
42
  file_name = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
44
43
  File.join(config_file_path, "#{file_name}navigation.rb")
45
44
  end
46
45
 
46
+ def explicit_navigation_args
47
+ self.controller.instance_variable_get(:"@sn_current_navigation_args")
48
+ end
49
+
50
+ # Reads the current navigation for the specified level from the controller.
51
+ # Returns nil if there is no current navigation set for level.
52
+ def current_navigation_for(level)
53
+ self.controller.instance_variable_get(:"@sn_current_navigation_#{level}")
54
+ end
55
+
56
+ def active_item_container_for(level)
57
+ self.primary_navigation.active_item_container_for(level)
58
+ end
59
+
60
+ # If any navigation has been explicitely set in the controller this method evaluates the specified args set in the controller and sets
61
+ # the correct instance variable in the controller.
62
+ def handle_explicit_navigation
63
+ if SimpleNavigation.explicit_navigation_args
64
+ level, navigation = parse_explicit_navigation_args
65
+ self.controller.instance_variable_set(:"@sn_current_navigation_#{level}", navigation)
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ # TODO: refactor this ugly thing to make it nice and short
72
+ def parse_explicit_navigation_args
73
+ args = SimpleNavigation.explicit_navigation_args
74
+ args = [Hash.new] if args.empty?
75
+ if args.first.kind_of? Hash
76
+ options = args.first
77
+ else # args is a list of current navigation for several levels
78
+ options = {}
79
+ if args.size == 1 #only an navi-key has been specified, try to find out level
80
+ level = SimpleNavigation.primary_navigation.level_for_item(args.first)
81
+ options[:"level_#{level}"] = args.first
82
+ else
83
+ args.each_with_index {|arg, i| options[:"level_#{i + 1}"] = arg}
84
+ end
85
+ end
86
+ #only the deepest level is relevant
87
+ level = options.inject(0) do |max, kv|
88
+ kv.first.to_s =~ /level_(\d)/
89
+ max = $1.to_i if $1.to_i > max
90
+ max
91
+ end
92
+ raise ArgumentError, "Invalid level specified or item key not found" if level == 0
93
+ [level, options[:"level_#{level}"]]
94
+ end
95
+
47
96
  end
48
97
 
49
- end
98
+ end
99
+
100
+ # TODOs for the next releases:
101
+ # 1) add ability to specify explicit highlighting in the config-file itself (directly with the item)
102
+ # - item.highlight_on :controller => 'users', :action => 'show' ...^
103
+ # --> with that we can get rid of the controller_methods...
104
+ #
105
+ # 2) ability to turn off autohighlighting for a single item...
106
+ #
107
+ # 3) add JoinRenderer (HorizontalRenderer?) (wich does not render a list, but just the items joined with a specified char (e.g. | ))
108
+ #
109
+ # 4) Enhance SampleProject (more examples)
110
+ #
111
+ # 5) Make SampleProject public
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple-navigation}
8
- s.version = "1.4.2"
8
+ s.version = "2.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andi Schacke"]
12
- s.date = %q{2009-09-23}
13
- s.description = %q{Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app.}
12
+ s.date = %q{2009-10-10}
13
+ s.description = %q{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.}
14
14
  s.email = %q{andreas.schacke@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README"
@@ -53,7 +53,7 @@ Gem::Specification.new do |s|
53
53
  s.require_paths = ["lib"]
54
54
  s.rubyforge_project = %q{andi}
55
55
  s.rubygems_version = %q{1.3.5}
56
- s.summary = %q{Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app.}
56
+ s.summary = %q{Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Ruby on Rails application.}
57
57
  s.test_files = [
58
58
  "spec/lib/simple_navigation/configuration_spec.rb",
59
59
  "spec/lib/simple_navigation/controller_methods_spec.rb",
@@ -18,6 +18,7 @@ describe SimpleNavigation::Configuration do
18
18
  before(:each) do
19
19
  @context = mock(:context)
20
20
  @context.stub!(:instance_eval)
21
+ SimpleNavigation::Configuration.stub!(:context_for_eval => @context)
21
22
  @config_files = {:default => 'default', :my_context => 'my_context'}
22
23
  SimpleNavigation.stub!(:config_files).and_return(@config_files)
23
24
  end
@@ -39,6 +40,53 @@ describe SimpleNavigation::Configuration do
39
40
  SimpleNavigation.should_receive(:controller=).with(@controller)
40
41
  SimpleNavigation::Configuration.eval_config(@context)
41
42
  end
43
+ it "should set the template" do
44
+ @template = stub(:template)
45
+ @controller = stub(:controller, :instance_variable_get => @template)
46
+ SimpleNavigation.stub!(:controller => @controller)
47
+ SimpleNavigation.should_receive(:template=).with(@template)
48
+ SimpleNavigation::Configuration.eval_config(@context)
49
+ end
50
+ end
51
+
52
+ describe 'context_for_eval' do
53
+ context 'controller is present' do
54
+ before(:each) do
55
+ @controller = stub(:controller)
56
+ SimpleNavigation.stub!(:controller => @controller)
57
+ end
58
+ context 'template is present' do
59
+ before(:each) do
60
+ @template = stub(:template)
61
+ SimpleNavigation.stub!(:template => @template)
62
+ end
63
+ it {SimpleNavigation::Configuration.context_for_eval.should == @template}
64
+ end
65
+ context 'template is not present' do
66
+ before(:each) do
67
+ SimpleNavigation.stub!(:template => nil)
68
+ end
69
+ it {SimpleNavigation::Configuration.context_for_eval.should == @controller}
70
+ end
71
+ end
72
+ context 'controller is not present' do
73
+ before(:each) do
74
+ SimpleNavigation.stub!(:controller => nil)
75
+ end
76
+ context 'template is present' do
77
+ before(:each) do
78
+ @template = stub(:template)
79
+ SimpleNavigation.stub!(:template => @template)
80
+ end
81
+ it {SimpleNavigation::Configuration.context_for_eval.should == @template}
82
+ end
83
+ context 'template is not present' do
84
+ before(:each) do
85
+ SimpleNavigation.stub!(:template => nil)
86
+ end
87
+ it {lambda {SimpleNavigation::Configuration.context_for_eval}.should raise_error}
88
+ end
89
+ end
42
90
  end
43
91
 
44
92
  describe 'self.extract_controller_from' do
@@ -78,6 +126,9 @@ describe SimpleNavigation::Configuration do
78
126
  it "should set autogenerate_item_ids to true as default" do
79
127
  @config.autogenerate_item_ids.should be_true
80
128
  end
129
+ it "should set auto_highlight to true as default" do
130
+ @config.auto_highlight.should be_true
131
+ end
81
132
  end
82
133
  describe 'items' do
83
134
  before(:each) do
@@ -95,6 +146,16 @@ describe SimpleNavigation::Configuration do
95
146
  end
96
147
  end
97
148
 
149
+ describe 'loaded?' do
150
+ it "should return true if primary_nav is set" do
151
+ @config.instance_variable_set(:@primary_navigation, :bla)
152
+ @config.should be_loaded
153
+ end
154
+ it "should return false if no primary_nav is set" do
155
+ @config.instance_variable_set(:@primary_navigation, nil)
156
+ @config.should_not be_loaded
157
+ end
158
+ end
98
159
 
99
160
  end
100
161
 
@@ -39,17 +39,17 @@ describe SimpleNavigation::ControllerMethods do
39
39
  end
40
40
  end
41
41
 
42
- it "should not have an instance-method 'set_navigation if navigation-method has not been called" do
43
- @controller.respond_to?(:set_navigation).should be_false
42
+ it "should not have an instance-method 'sn_set_navigation' if navigation-method has not been called" do
43
+ @controller.respond_to?(:sn_set_navigation).should be_false
44
44
  end
45
- it 'should create an instance-method "set_navigation" when being called' do
45
+ it 'should create an instance-method "sn_set_navigation" when being called' do
46
46
  call_navigation(:key)
47
- @controller.respond_to?(:set_navigation).should be_true
47
+ @controller.respond_to?(:sn_set_navigation).should be_true
48
48
  end
49
49
  it 'the created method should call current_navigation with the specified keys' do
50
50
  call_navigation(:primary, :secondary)
51
51
  @controller.should_receive(:current_navigation).with(:primary, :secondary)
52
- @controller.set_navigation
52
+ @controller.sn_set_navigation
53
53
  end
54
54
  end
55
55
 
@@ -58,17 +58,13 @@ describe SimpleNavigation::ControllerMethods do
58
58
  describe 'instance_methods' do
59
59
 
60
60
  describe 'current_navigation' do
61
- it "should set the current_primary_navigation as specified" do
61
+ it "should set the sn_current_navigation_args as specified" do
62
62
  @controller.current_navigation(:first)
63
- @controller.instance_variable_get(:@current_primary_navigation).should == :first
63
+ @controller.instance_variable_get(:@sn_current_navigation_args).should == [:first]
64
64
  end
65
- it "should set the current_secondary_navigation as specified" do
65
+ it "should set the sn_current_navigation_args as specified" do
66
66
  @controller.current_navigation(:first, :second)
67
- @controller.instance_variable_get(:@current_secondary_navigation).should == :second
68
- end
69
- it "should set the current_secondary_navigation to nil if no secondary is specified" do
70
- @controller.current_navigation(:first)
71
- @controller.instance_variable_get(:@current_secondary_navigation).should be_nil
67
+ @controller.instance_variable_get(:@sn_current_navigation_args).should == [:first, :second]
72
68
  end
73
69
  end
74
70
 
@@ -36,40 +36,46 @@ describe SimpleNavigation::Helpers do
36
36
  end
37
37
 
38
38
  context 'primary' do
39
- before(:each) do
40
- @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
41
- end
42
39
  it "should call render on the primary_navigation" do
43
- @primary_navigation.should_receive(:render).with(:current_primary)
40
+ @primary_navigation.should_receive(:render)
44
41
  @controller.render_navigation(:primary)
45
42
  end
46
43
  it "should call render on the primary_navigation (specifying level through options)" do
47
- @primary_navigation.should_receive(:render).with(:current_primary)
44
+ @primary_navigation.should_receive(:render)
48
45
  @controller.render_navigation(:level => :primary)
49
46
  end
47
+ it "should call render on the primary_navigation (specifying level through options)" do
48
+ @primary_navigation.should_receive(:render)
49
+ @controller.render_navigation(:level => 1)
50
+ end
50
51
  end
51
52
 
52
53
  context 'secondary' do
53
54
  context 'with current_primary_navigation set' do
54
55
  before(:each) do
55
- @sub_navigation = stub(:sub_navigation, :null_object => true)
56
- @primary_navigation.stub!(:[]).and_return(@sub_navigation)
57
- @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
58
- @controller.instance_variable_set(:@current_secondary_navigation, :current_secondary)
56
+ @selected_item_container = stub(:selected_container, :null_object => true)
57
+ SimpleNavigation.stub!(:active_item_container_for => @selected_item_container)
59
58
  end
60
- it "should find the sub_navigation belonging to the current primary_navigation" do
61
- @primary_navigation.should_receive(:[]).with(:current_primary)
59
+ it "should find the selected sub_navigation for the specified level" do
60
+ SimpleNavigation.should_receive(:active_item_container_for).with(2)
62
61
  @controller.render_navigation(:secondary)
63
62
  end
64
- it "should call render on the current primary_navigation's sub_navigation" do
65
- @sub_navigation.should_receive(:render).with(:current_secondary)
63
+ it "should find the selected sub_navigation for the specified level" do
64
+ SimpleNavigation.should_receive(:active_item_container_for).with(2)
65
+ @controller.render_navigation(:level => :secondary)
66
+ end
67
+ it "should find the selected sub_navigation for the specified level" do
68
+ SimpleNavigation.should_receive(:active_item_container_for).with(1)
69
+ @controller.render_navigation(:level => 1)
70
+ end
71
+ it "should call render on the active item_container" do
72
+ @selected_item_container.should_receive(:render)
66
73
  @controller.render_navigation(:secondary)
67
74
  end
68
75
  end
69
- context 'without current_primary_navigation set' do
76
+ context 'without an active item_container set' do
70
77
  before(:each) do
71
- @primary_navigation.stub!(:[]).and_return(nil)
72
- @controller.instance_variable_set(:@current_primary_navigation, nil)
78
+ SimpleNavigation.stub!(:active_item_container_for => nil)
73
79
  end
74
80
  it "should not raise an error" do
75
81
  lambda{@controller.render_navigation(:secondary)}.should_not raise_error
@@ -79,24 +85,8 @@ describe SimpleNavigation::Helpers do
79
85
  end
80
86
 
81
87
  context 'nested' do
82
- before(:each) do
83
- @controller.instance_variable_set(:@current_primary_navigation, :current_primary)
84
- @controller.instance_variable_set(:@current_secondary_navigation, :current_secondary)
85
- end
86
- it "should call render on the primary navigation" do
87
- @primary_navigation.should_receive(:render).with(anything, anything, anything)
88
- @controller.render_navigation(:nested)
89
- end
90
- it "should call render with the current_primary_navigation" do
91
- @primary_navigation.should_receive(:render).with(:current_primary, anything, anything)
92
- @controller.render_navigation(:nested)
93
- end
94
- it "should call render with the include_subnavigation option set" do
95
- @primary_navigation.should_receive(:render).with(anything, true, anything)
96
- @controller.render_navigation(:nested)
97
- end
98
- it "should call render with the current_sub_navigation" do
99
- @primary_navigation.should_receive(:render).with(anything, anything, :current_secondary)
88
+ it "should call render on the primary navigation with the include_subnavigation option set" do
89
+ @primary_navigation.should_receive(:render).with(true)
100
90
  @controller.render_navigation(:nested)
101
91
  end
102
92
  end
@@ -105,20 +95,30 @@ describe SimpleNavigation::Helpers do
105
95
  it "should raise an error" do
106
96
  lambda {@controller.render_navigation(:unknown)}.should raise_error(ArgumentError)
107
97
  end
98
+ it "should raise an error" do
99
+ lambda {@controller.render_navigation(:level => :unknown)}.should raise_error(ArgumentError)
100
+ end
101
+ it "should raise an error" do
102
+ lambda {@controller.render_navigation('level')}.should raise_error(ArgumentError)
103
+ end
108
104
  end
109
105
  end
110
106
 
111
107
  describe 'render_primary_navigation' do
112
108
  it "should delegate to render_navigation(:primary)" do
113
- @controller.should_receive(:render_navigation).with(:level => :primary)
114
- @controller.render_primary_navigation
109
+ ActiveSupport::Deprecation.silence do
110
+ @controller.should_receive(:render_navigation).with(:level => 1)
111
+ @controller.render_primary_navigation
112
+ end
115
113
  end
116
114
  end
117
115
 
118
116
  describe 'render_sub_navigation' do
119
117
  it "should delegate to render_navigation(:secondary)" do
120
- @controller.should_receive(:render_navigation).with(:level => :secondary)
121
- @controller.render_sub_navigation
118
+ ActiveSupport::Deprecation.silence do
119
+ @controller.should_receive(:render_navigation).with(:level => 2)
120
+ @controller.render_sub_navigation
121
+ end
122
122
  end
123
123
  end
124
124
 
@@ -15,6 +15,118 @@ describe SimpleNavigation::ItemContainer do
15
15
  end
16
16
  end
17
17
 
18
+ describe 'selected?' do
19
+ before(:each) do
20
+ @item_1 = stub(:item, :selected? => false)
21
+ @item_2 = stub(:item, :selected? => false)
22
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
23
+ end
24
+ it "should return nil if no item is selected" do
25
+ @item_container.should_not be_selected
26
+ end
27
+ it "should return true if one item is selected" do
28
+ @item_1.stub!(:selected? => true)
29
+ @item_container.should be_selected
30
+ end
31
+ end
32
+
33
+ describe 'selected_item' do
34
+ before(:each) do
35
+ @item_1 = stub(:item, :selected? => false)
36
+ @item_2 = stub(:item, :selected? => false)
37
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
38
+ end
39
+ context 'navigation explicitely set' do
40
+ before(:each) do
41
+ @item_container.stub!(:[] => @item_1)
42
+ end
43
+ it "should return the explicitely selected item" do
44
+ @item_container.selected_item.should == @item_1
45
+ end
46
+ end
47
+ context 'navigation not explicitely set' do
48
+ before(:each) do
49
+ @item_container.stub!(:[] => nil)
50
+ end
51
+ context 'no item selected' do
52
+ it "should return nil" do
53
+ @item_container.selected_item.should be_nil
54
+ end
55
+ end
56
+ context 'one item selected' do
57
+ before(:each) do
58
+ @item_1.stub!(:selected? => true)
59
+ end
60
+ it "should return the selected item" do
61
+ @item_container.selected_item.should == @item_1
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ describe 'selected_sub_navigation?' do
68
+ context 'with an item selected' do
69
+ before(:each) do
70
+ @selected_item = stub(:selected_item)
71
+ @item_container.stub!(:selected_item => @selected_item)
72
+ end
73
+ context 'selected item has sub_navigation' do
74
+ before(:each) do
75
+ @sub_navigation = stub(:sub_navigation)
76
+ @selected_item.stub!(:sub_navigation => @sub_navigation)
77
+ end
78
+ it {@item_container.send(:selected_sub_navigation?).should be_true}
79
+ end
80
+ context 'selected item does not have sub_navigation' do
81
+ before(:each) do
82
+ @selected_item.stub!(:sub_navigation => nil)
83
+ end
84
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
85
+ end
86
+ end
87
+ context 'without an item selected' do
88
+ before(:each) do
89
+ @item_container.stub!(:selected_item => nil)
90
+ end
91
+ it {@item_container.send(:selected_sub_navigation?).should be_false}
92
+ end
93
+
94
+ end
95
+
96
+ describe 'active_item_container_for' do
97
+ context "the desired level is the same as the container's" do
98
+ it {@item_container.active_item_container_for(1).should == @item_container}
99
+ end
100
+ context "the desired level is different than the container's" do
101
+ context 'with no selected subnavigation' do
102
+ before(:each) do
103
+ @item_container.stub!(:selected_sub_navigation? => false)
104
+ end
105
+ it {@item_container.active_item_container_for(2).should be_nil}
106
+ end
107
+ context 'with selected subnavigation' do
108
+ before(:each) do
109
+ @item_container.stub!(:selected_sub_navigation? => true)
110
+ @sub_nav = stub(:sub_nav)
111
+ @selected_item = stub(:selected_item)
112
+ @item_container.stub!(:selected_item => @selected_item)
113
+ @selected_item.stub!(:sub_navigation => @sub_nav)
114
+ end
115
+ it "should call recursively on the sub_navigation" do
116
+ @sub_nav.should_receive(:active_item_container_for).with(2)
117
+ @item_container.active_item_container_for(2)
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ describe 'current_explicit_navigation' do
124
+ it "should call SimpleNavigation.current_navigation with the container's level" do
125
+ SimpleNavigation.should_receive(:current_navigation_for).with(1)
126
+ @item_container.current_explicit_navigation
127
+ end
128
+ end
129
+
18
130
  describe 'item' do
19
131
 
20
132
  context 'unconditional item' do
@@ -36,7 +148,7 @@ describe SimpleNavigation::ItemContainer do
36
148
  end
37
149
  end
38
150
  it "should create a new Navigation-Item with the given params and the specified block" do
39
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, @proc)
151
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, @proc)
40
152
  @item_container.item('key', 'name', 'url', @options, &@proc)
41
153
  end
42
154
  it "should add the created item to the list of items" do
@@ -47,7 +159,7 @@ describe SimpleNavigation::ItemContainer do
47
159
 
48
160
  context 'no block given' do
49
161
  it "should create a new Navigation_item with the given params and nil as sub_navi" do
50
- SimpleNavigation::Item.should_receive(:new).with('key', 'name', 'url', @options, nil)
162
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil)
51
163
  @item_container.item('key', 'name', 'url', @options)
52
164
  end
53
165
  it "should add the created item to the list of items" do
@@ -91,6 +203,12 @@ describe SimpleNavigation::ItemContainer do
91
203
  end
92
204
  end
93
205
 
206
+ context 'if is not a proc or method' do
207
+ it "should raise an error" do
208
+ lambda {@item_container.item('key', 'name', 'url', {:if => 'text'})}.should raise_error
209
+ end
210
+ end
211
+
94
212
  context '"unless" given' do
95
213
 
96
214
  before(:each) do
@@ -156,8 +274,8 @@ describe SimpleNavigation::ItemContainer do
156
274
  @items = stub(:items)
157
275
  @item_container.stub!(:items).and_return(@items)
158
276
  end
159
- it "should instatiate a renderer with the current_primary and current_secondary" do
160
- @renderer.should_receive(:new).with(:current_navigation, nil)
277
+ it "should instatiate a renderer" do
278
+ @renderer.should_receive(:new)
161
279
  @item_container.render(:current_navigation)
162
280
  end
163
281
  it "should call render on the renderer and pass self" do
@@ -166,9 +284,32 @@ describe SimpleNavigation::ItemContainer do
166
284
  end
167
285
  it "should call render on the renderer and pass the include_sub_navigation option" do
168
286
  @renderer_instance.should_receive(:render).with(anything, true)
169
- @item_container.render(:current_navigation, true, :current_sub_navigation)
287
+ @item_container.render(true)
170
288
  end
171
289
 
172
290
  end
173
291
 
292
+ describe 'level_for_item' do
293
+ before(:each) do
294
+ @item_container.item(:p1, 'p1', 'p1')
295
+ @item_container.item(:p2, 'p2', 'p2') do |p2|
296
+ p2.item(:s1, 's1', 's1')
297
+ p2.item(:s2, 's2', 's2') do |s2|
298
+ s2.item(:ss1, 'ss1', 'ss1')
299
+ s2.item(:ss2, 'ss2', 'ss2')
300
+ end
301
+ p2.item(:s3, 's3', 's3')
302
+ end
303
+ @item_container.item(:p3, 'p3', 'p3')
304
+ end
305
+ it {@item_container.level_for_item(:p1).should == 1}
306
+ it {@item_container.level_for_item(:p3).should == 1}
307
+ it {@item_container.level_for_item(:s1).should == 2}
308
+ it {@item_container.level_for_item(:ss1).should == 3}
309
+ it {@item_container.level_for_item(:x).should be_nil}
310
+
311
+
312
+
313
+ end
314
+
174
315
  end