simple-navigation 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ *2.1.0
2
+
3
+ * included Ben Marini's commit which allows individual id-generators. Thanks Ben!
4
+ * added ability to specify navigation items through items_provider. This is useful for generating the navigation dynamically (e.g. from database)
5
+ * items can now even be passed directly into render_navigation method.
6
+
1
7
  *2.0.1
2
8
 
3
9
  * fixed handling of a non-existent explicit navigation item for a navigation context
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 0
3
3
  :major: 2
4
- :minor: 0
4
+ :minor: 1
@@ -15,6 +15,11 @@ SimpleNavigation::Configuration.run do |navigation|
15
15
  # Item keys are normally added to list items as id.
16
16
  # This setting turns that off
17
17
  # navigation.autogenerate_item_ids = false
18
+
19
+ # You can override the default logic that is used to autogenerate the item ids.
20
+ # To do this, define a Proc which takes the key of the current item as argument.
21
+ # The example below would add a prefix to each key.
22
+ # navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"}
18
23
 
19
24
  # The auto highlight feature is turned on by default.
20
25
  # This turns it off globally (for the whole plugin)
@@ -2,8 +2,10 @@
2
2
  require 'simple_navigation/configuration'
3
3
  require 'simple_navigation/helpers'
4
4
  require 'simple_navigation/controller_methods'
5
+ require 'simple_navigation/item_adapter'
5
6
  require 'simple_navigation/item'
6
7
  require 'simple_navigation/item_container'
8
+ require 'simple_navigation/items_provider'
7
9
  require 'simple_navigation/renderer/base'
8
10
  require 'simple_navigation/renderer/list'
9
11
 
@@ -114,3 +116,7 @@ end
114
116
  # 4) Enhance SampleProject (more examples)
115
117
  #
116
118
  # 5) Make SampleProject public
119
+ #
120
+ # - allow :function navigation item to specify function
121
+ # - allow specification of link-options in item (currently options are passed to li-element)
122
+ # - render_navigation: do not rescue from config-file not found error if no items are passed in directly
@@ -6,7 +6,7 @@ module SimpleNavigation
6
6
  class Configuration
7
7
  include Singleton
8
8
 
9
- attr_accessor :renderer, :selected_class, :render_all_levels, :autogenerate_item_ids, :auto_highlight
9
+ attr_accessor :renderer, :selected_class, :render_all_levels, :autogenerate_item_ids, :id_generator, :auto_highlight
10
10
  attr_reader :primary_navigation
11
11
 
12
12
  class << self
@@ -47,13 +47,35 @@ module SimpleNavigation
47
47
  @selected_class = 'selected'
48
48
  @render_all_levels = false
49
49
  @autogenerate_item_ids = true
50
+ @id_generator = Proc.new {|id| id.to_s }
50
51
  @auto_highlight = true
51
52
  end
52
53
 
53
- # Yields an SimpleNavigation::ItemContainer for adding navigation items
54
- def items(&block)
54
+ # This is the main method for specifying the navigation items. It can be used in two ways:
55
+ #
56
+ # 1. Declaratively specify your items in the config/navigation.rb file using a block. It then yields an SimpleNavigation::ItemContainer for adding navigation items.
57
+ # 1. Directly provide your items to the method (e.g. when loading your items from the database). Either specify
58
+ #
59
+ # ==== Example for block style
60
+ # config.items do |primary|
61
+ # primary.item :my_item, 'My item', my_item_path
62
+ # ...
63
+ # end
64
+ #
65
+ # ==== To consider when directly providing items
66
+ # * a methodname (as symbol) that returns your items. The method needs to be available in the view (i.e. a helper method)
67
+ # * an object that responds to :items
68
+ # * an enumerable containing your items
69
+ # The items you specify have to fullfill certain requirements. See SimpleNavigation::ItemAdapter for more details.
70
+ #
71
+ def items(items_provider=nil, &block)
72
+ raise 'please specify either items_provider or block, but not both' if (items_provider && block) || (items_provider.nil? && block.nil?)
55
73
  @primary_navigation = ItemContainer.new
56
- block.call @primary_navigation
74
+ if block
75
+ block.call @primary_navigation
76
+ else
77
+ @primary_navigation.items = SimpleNavigation::ItemsProvider.new(items_provider).items
78
+ end
57
79
  end
58
80
 
59
81
  # Returns true if the config_file has already been evaluated.
@@ -61,6 +83,10 @@ module SimpleNavigation
61
83
  !@primary_navigation.nil?
62
84
  end
63
85
 
86
+ def context_for_eval
87
+ self.class.context_for_eval
88
+ end
89
+
64
90
  end
65
91
 
66
92
  end
@@ -24,14 +24,18 @@ module SimpleNavigation
24
24
  # * <tt>:context</tt> - specifies the context for which you would render the navigation. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb).
25
25
  # If you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call <tt>render_navigation(:context => :admin)</tt> the file config/admin_navigation.rb
26
26
  # will be loaded and used for rendering the navigation.
27
- #
27
+ # * <tt>:all_open</tt> - setting this options to true means that all items are always open (ie. fully expanded tree). Same as setting render_all_levels option in the config-file.
28
+ # * <tt>:items</tt> - you can specify the items directly (e.g. if items are dynamically generated from database). See SimpleNavigation::ItemsProvider for documentation on what to provide as items.
28
29
  def render_navigation(*args)
29
30
  args = [Hash.new] if args.empty?
30
31
  options = extract_backwards_compatible_options(*args)
31
32
  options = {:context => :default, :level => :nested}.merge(options)
32
- SimpleNavigation.load_config(options[:context])
33
- SimpleNavigation::Configuration.eval_config(self, options[:context])
33
+ SimpleNavigation.load_config(options[:context]) rescue nil
34
+ SimpleNavigation::Configuration.eval_config(self, options[:context]) rescue nil
35
+ SimpleNavigation.config.render_all_levels = options[:all_open] unless options[:all_open].nil?
36
+ SimpleNavigation.config.items(options[:items]) if options[:items]
34
37
  SimpleNavigation.handle_explicit_navigation
38
+ raise "no primary navigation defined, either use a navigation config file or pass items directly to render_navigation" unless SimpleNavigation.primary_navigation
35
39
  case options[:level]
36
40
  when Integer
37
41
  active_item_container = SimpleNavigation.active_item_container_for(options[:level])
@@ -6,16 +6,19 @@ module SimpleNavigation
6
6
  attr_writer :html_options
7
7
 
8
8
  # see ItemContainer#item
9
- def initialize(container, key, name, url, options, sub_nav_block) #:nodoc:
9
+ #
10
+ # The subnavigation (if any) is either provided by a block or passed in directly as <tt>items</tt>
11
+ def initialize(container, key, name, url, options, items=nil, &sub_nav_block)
10
12
  @container = container
11
13
  @key = key
12
14
  @method = options.delete(:method)
13
15
  @name = name
14
16
  @url = url
15
17
  @html_options = options
16
- if sub_nav_block
18
+ if sub_nav_block || items
17
19
  @sub_navigation = ItemContainer.new(@container.level + 1)
18
- sub_nav_block.call @sub_navigation
20
+ sub_nav_block.call @sub_navigation if sub_nav_block
21
+ @sub_navigation.items = items if items
19
22
  end
20
23
  end
21
24
 
@@ -33,7 +36,7 @@ module SimpleNavigation
33
36
  # Returns the html-options hash for the item, i.e. the options specified for this item in the config-file.
34
37
  # It also adds the 'selected' class to the list of classes if necessary.
35
38
  def html_options
36
- default_options = self.autogenerate_item_ids? ? {:id => key.to_s} : {}
39
+ default_options = self.autogenerate_item_ids? ? {:id => autogenerated_item_id} : {}
37
40
  options = default_options.merge(@html_options)
38
41
  options[:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ')
39
42
  options.delete(:class) if options[:class].blank?
@@ -81,6 +84,10 @@ module SimpleNavigation
81
84
  SimpleNavigation.config.autogenerate_item_ids
82
85
  end
83
86
 
87
+ def autogenerated_item_id
88
+ SimpleNavigation.config.id_generator.call(key)
89
+ end
90
+
84
91
  def auto_highlight?
85
92
  SimpleNavigation.config.auto_highlight && @container.auto_highlight
86
93
  end
@@ -0,0 +1,41 @@
1
+ module SimpleNavigation
2
+
3
+ # This class acts as an adapter to items that are not defined using the DSL in the config/navigation.rb, but directly provided inside the application.
4
+ # When defining the items that way, every item you provide needs to define the following methods:
5
+ #
6
+ # * <tt>key</tt>
7
+ # * <tt>name</tt>
8
+ # * <tt>url</tt>
9
+ #
10
+ # and optionally
11
+ #
12
+ # * <tt>options</tt>
13
+ # * <tt>items</tt> - if one of your items has a subnavigation it must respond to <tt>items</tt> providing the subnavigation.
14
+ #
15
+ # See SimpleNavigation::ItemContainer#item for the purpose of these methods.
16
+ class ItemAdapter
17
+ delegate :key, :name, :url, :to => :item
18
+
19
+ attr_reader :item
20
+
21
+ def initialize(item)
22
+ @item = item
23
+ end
24
+
25
+ # Returns the options for this item. If the wrapped item does not implement an options method, an empty hash is returned.
26
+ def options
27
+ @item.respond_to?(:options) ? @item.options : {}
28
+ end
29
+
30
+ # Returns the items (subnavigation) for this item if it responds to :items and the items-collection is not empty. Returns nil otherwise.
31
+ def items
32
+ (@item.respond_to?(:items) && !(@item.items.nil? || @item.items.empty?)) ? @item.items : nil
33
+ end
34
+
35
+ # Converts this Item into a SimpleNavigation::Item
36
+ def to_simple_navigation_item(item_container)
37
+ SimpleNavigation::Item.new(item_container, key, name, url, options, items)
38
+ end
39
+
40
+ end
41
+ end
@@ -32,7 +32,14 @@ module SimpleNavigation
32
32
  #
33
33
  # The <tt>block</tt> - if specified - will hold the item's sub_navigation.
34
34
  def item(key, name, url, options={}, &block)
35
- (@items << SimpleNavigation::Item.new(self, key, name, url, options, block)) if should_add_item?(options)
35
+ (@items << SimpleNavigation::Item.new(self, key, name, url, options, nil, &block)) if should_add_item?(options)
36
+ end
37
+
38
+ def items=(items)
39
+ items.each do |item|
40
+ item = SimpleNavigation::ItemAdapter.new(item)
41
+ (@items << item.to_simple_navigation_item(self)) if should_add_item?(item.options)
42
+ end
36
43
  end
37
44
 
38
45
  # Returns the Item with the specified key, nil otherwise.
@@ -0,0 +1,35 @@
1
+ module SimpleNavigation
2
+
3
+ # Acts as a proxy to navigation items that are passed into the SimpleNavigation::Configuration#items method. It hides the logic
4
+ # for finding items from the Configuration object.
5
+ #
6
+ class ItemsProvider
7
+
8
+ attr_reader :provider
9
+
10
+ # It accepts the following types of provider:
11
+ # * methodname as symbol - the specified method should return the relevant items and has to be available in the view (a helper method)
12
+ # * object that responds to :items
13
+ # * enumerable object that represents the items
14
+ #
15
+ # See SimpleNavigation::ItemAdapter for the requirements that need to be fulfilled by the provided items.
16
+ #
17
+ def initialize(provider)
18
+ @provider = provider
19
+ end
20
+
21
+ # Returns the navigation items
22
+ def items
23
+ if provider.instance_of?(Symbol)
24
+ SimpleNavigation.config.context_for_eval.send(provider)
25
+ elsif provider.respond_to?(:items)
26
+ provider.items
27
+ elsif provider.respond_to?(:each)
28
+ provider
29
+ else
30
+ raise "items_provider either must be a symbol specifying the helper-method to call, an object with an items-method defined or an enumerable representing the items"
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple-navigation}
8
- s.version = "2.0.1"
8
+ s.version = "2.1.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-10-13}
12
+ s.date = %q{2009-12-18}
13
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 = [
@@ -32,7 +32,9 @@ Gem::Specification.new do |s|
32
32
  "lib/simple_navigation/controller_methods.rb",
33
33
  "lib/simple_navigation/helpers.rb",
34
34
  "lib/simple_navigation/item.rb",
35
+ "lib/simple_navigation/item_adapter.rb",
35
36
  "lib/simple_navigation/item_container.rb",
37
+ "lib/simple_navigation/items_provider.rb",
36
38
  "lib/simple_navigation/renderer/base.rb",
37
39
  "lib/simple_navigation/renderer/list.rb",
38
40
  "rails/init.rb",
@@ -40,8 +42,10 @@ Gem::Specification.new do |s|
40
42
  "spec/lib/simple_navigation/configuration_spec.rb",
41
43
  "spec/lib/simple_navigation/controller_methods_spec.rb",
42
44
  "spec/lib/simple_navigation/helpers_spec.rb",
45
+ "spec/lib/simple_navigation/item_adapter_spec.rb",
43
46
  "spec/lib/simple_navigation/item_container_spec.rb",
44
47
  "spec/lib/simple_navigation/item_spec.rb",
48
+ "spec/lib/simple_navigation/items_provider_spec.rb",
45
49
  "spec/lib/simple_navigation/renderer/base_spec.rb",
46
50
  "spec/lib/simple_navigation/renderer/list_spec.rb",
47
51
  "spec/lib/simple_navigation_spec.rb",
@@ -58,8 +62,10 @@ Gem::Specification.new do |s|
58
62
  "spec/lib/simple_navigation/configuration_spec.rb",
59
63
  "spec/lib/simple_navigation/controller_methods_spec.rb",
60
64
  "spec/lib/simple_navigation/helpers_spec.rb",
65
+ "spec/lib/simple_navigation/item_adapter_spec.rb",
61
66
  "spec/lib/simple_navigation/item_container_spec.rb",
62
67
  "spec/lib/simple_navigation/item_spec.rb",
68
+ "spec/lib/simple_navigation/items_provider_spec.rb",
63
69
  "spec/lib/simple_navigation/renderer/base_spec.rb",
64
70
  "spec/lib/simple_navigation/renderer/list_spec.rb",
65
71
  "spec/lib/simple_navigation_spec.rb",
@@ -129,20 +129,60 @@ describe SimpleNavigation::Configuration do
129
129
  it "should set auto_highlight to true as default" do
130
130
  @config.auto_highlight.should be_true
131
131
  end
132
+ it "should set the id_generator" do
133
+ @config.id_generator.should_not be_nil
134
+ end
132
135
  end
133
136
  describe 'items' do
134
137
  before(:each) do
135
138
  @container = stub(:items_container)
136
139
  SimpleNavigation::ItemContainer.stub!(:new).and_return(@container)
137
140
  end
138
- it "should should yield an new ItemContainer" do
139
- @config.items do |container|
140
- container.should == @container
141
+ context 'block given' do
142
+ context 'items_provider specified' do
143
+ it {lambda {@config.items(stub(:provider)) {}}.should raise_error}
144
+ end
145
+ context 'no items_provider specified' do
146
+ it "should should yield an new ItemContainer" do
147
+ @config.items do |container|
148
+ container.should == @container
149
+ end
150
+ end
151
+ it "should assign the ItemContainer to an instance-var" do
152
+ @config.items {}
153
+ @config.primary_navigation.should == @container
154
+ end
155
+ it "should not set the items on the container" do
156
+ @container.should_not_receive(:items=)
157
+ @config.items {}
158
+ end
141
159
  end
142
160
  end
143
- it "should assign the ItemContainer to an instance-var" do
144
- @config.items {}
145
- @config.primary_navigation.should == @container
161
+ context 'no block given' do
162
+ context 'items_provider specified' do
163
+ before(:each) do
164
+ @external_provider = stub(:external_provider)
165
+ @items = stub(:items)
166
+ @items_provider = stub(:items_provider, :items => @items)
167
+ SimpleNavigation::ItemsProvider.stub!(:new => @items_provider)
168
+ @container.stub!(:items=)
169
+ end
170
+ it "should create an new Provider object for the specified provider" do
171
+ SimpleNavigation::ItemsProvider.should_receive(:new).with(@external_provider)
172
+ @config.items(@external_provider)
173
+ end
174
+ it "should call items on the provider object" do
175
+ @items_provider.should_receive(:items)
176
+ @config.items(@external_provider)
177
+ end
178
+ it "should set the items on the container" do
179
+ @container.should_receive(:items=).with(@items)
180
+ @config.items(@external_provider)
181
+ end
182
+ end
183
+ context 'items_provider not specified' do
184
+ it {lambda {@config.items}.should raise_error}
185
+ end
146
186
  end
147
187
  end
148
188
 
@@ -157,6 +197,13 @@ describe SimpleNavigation::Configuration do
157
197
  end
158
198
  end
159
199
 
200
+ describe 'context_for_eval' do
201
+ it "should delegate to the Configuration class" do
202
+ @config.class.should_receive(:context_for_eval)
203
+ @config.context_for_eval
204
+ end
205
+ end
206
+
160
207
  end
161
208
 
162
209
 
@@ -35,6 +35,52 @@ describe SimpleNavigation::Helpers do
35
35
  @controller.render_navigation
36
36
  end
37
37
 
38
+ describe "regarding setting the 'render_all_levels' option" do
39
+ context 'all_open is not specified' do
40
+ it "should not set the option" do
41
+ SimpleNavigation.config.should_not_receive(:render_all_levels=)
42
+ @controller.render_navigation
43
+ end
44
+ end
45
+ context 'all_open is true' do
46
+ it "should set the option to true" do
47
+ SimpleNavigation.config.should_receive(:render_all_levels=).with(true)
48
+ @controller.render_navigation(:all_open => true)
49
+ end
50
+ end
51
+ context 'all_open is false' do
52
+ it "should set the option to false" do
53
+ SimpleNavigation.config.should_receive(:render_all_levels=).with(false)
54
+ @controller.render_navigation(:all_open => false)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe 'regarding setting of items' do
60
+ context 'not items specified in options' do
61
+ it "should not set the items directly" do
62
+ SimpleNavigation.config.should_not_receive(:items)
63
+ @controller.render_navigation
64
+ end
65
+ end
66
+ context 'items specified in options' do
67
+ before(:each) do
68
+ @items = stub(:items)
69
+ end
70
+ it "should set the items directly" do
71
+ SimpleNavigation.config.should_receive(:items).with(@items)
72
+ @controller.render_navigation(:items => @items)
73
+ end
74
+ end
75
+ end
76
+
77
+ describe 'no primary navigation defined' do
78
+ before(:each) do
79
+ SimpleNavigation.stub!(:primary_navigation => nil)
80
+ end
81
+ it {lambda {@controller.render_navigation}.should raise_error}
82
+ end
83
+
38
84
  context 'primary' do
39
85
  it "should call render on the primary_navigation" do
40
86
  @primary_navigation.should_receive(:render)
@@ -0,0 +1,102 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe SimpleNavigation::ItemAdapter do
4
+
5
+ before(:each) do
6
+ @item = stub(:item)
7
+ @item_adapter = SimpleNavigation::ItemAdapter.new(@item)
8
+ end
9
+
10
+ describe 'key' do
11
+ it "should delegate key to item" do
12
+ @item.should_receive(:key)
13
+ @item_adapter.key
14
+ end
15
+ end
16
+
17
+ describe 'url' do
18
+ it "should delegate url to item" do
19
+ @item.should_receive(:url)
20
+ @item_adapter.url
21
+ end
22
+ end
23
+
24
+ describe 'name' do
25
+ it "should delegate name to item" do
26
+ @item.should_receive(:name)
27
+ @item_adapter.name
28
+ end
29
+ end
30
+
31
+ describe 'initialize' do
32
+ it "should set the item" do
33
+ @item_adapter.item.should == @item
34
+ end
35
+ end
36
+
37
+ describe 'options' do
38
+ context 'item does respond to options' do
39
+ before(:each) do
40
+ @options = stub(:options)
41
+ @item.stub!(:options => @options)
42
+ end
43
+ it "should return the item's options'" do
44
+ @item_adapter.options.should == @options
45
+ end
46
+ end
47
+ context 'item does not respond to options' do
48
+ it "should return an empty hash" do
49
+ @item_adapter.options.should == {}
50
+ end
51
+ end
52
+ end
53
+
54
+ describe 'items' do
55
+ context 'item does respond to items' do
56
+ context 'items is nil' do
57
+ before(:each) do
58
+ @item.stub!(:items => nil)
59
+ end
60
+ it "should return nil" do
61
+ @item_adapter.items.should be_nil
62
+ end
63
+ end
64
+ context 'items is not nil' do
65
+ context 'items is empty' do
66
+ before(:each) do
67
+ @item.stub!(:items => [])
68
+ end
69
+ it "should return nil" do
70
+ @item_adapter.items.should be_nil
71
+ end
72
+ end
73
+ context 'items is not empty' do
74
+ before(:each) do
75
+ @items = stub(:items, :empty? => false)
76
+ @item.stub!(:items => @items)
77
+ end
78
+ it "should return the items" do
79
+ @item_adapter.items.should == @items
80
+ end
81
+ end
82
+ end
83
+ end
84
+ context 'item does not respond to items' do
85
+ it "should return nil" do
86
+ @item_adapter.items.should be_nil
87
+ end
88
+ end
89
+ end
90
+
91
+ describe 'to_simple_navigation_item' do
92
+ before(:each) do
93
+ @container = stub(:container)
94
+ @item.stub!(:url => 'url', :name => 'name', :key => 'key', :options => {}, :items => [])
95
+ end
96
+ it "should create a SimpleNavigation::Item" do
97
+ SimpleNavigation::Item.should_receive(:new).with(@container, 'key', 'name', 'url', {}, nil)
98
+ @item_adapter.to_simple_navigation_item(@container)
99
+ end
100
+ end
101
+
102
+ end
@@ -15,6 +15,48 @@ describe SimpleNavigation::ItemContainer do
15
15
  end
16
16
  end
17
17
 
18
+ describe 'items=' do
19
+ before(:each) do
20
+ @item = stub(:item)
21
+ @items = [@item]
22
+ @item_adapter = stub(:item_adapter, :null_object => true)
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
+
18
60
  describe 'selected?' do
19
61
  before(:each) do
20
62
  @item_1 = stub(:item, :selected? => false)
@@ -148,7 +190,7 @@ describe SimpleNavigation::ItemContainer do
148
190
  end
149
191
  end
150
192
  it "should create a new Navigation-Item with the given params and the specified block" do
151
- SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, @proc)
193
+ SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil, &@proc)
152
194
  @item_container.item('key', 'name', 'url', @options, &@proc)
153
195
  end
154
196
  it "should add the created item to the list of items" do
@@ -3,16 +3,53 @@ require File.dirname(__FILE__) + '/../../spec_helper'
3
3
  describe SimpleNavigation::Item do
4
4
 
5
5
  before(:each) do
6
- @item_container = stub(:item_container)
7
- @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, nil)
6
+ @item_container = stub(:item_container, :level => 1)
7
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
8
8
  end
9
9
 
10
10
  describe 'initialize' do
11
+ context 'subnavigation' do
12
+ before(:each) do
13
+ @subnav_container = stub(:subnav_container, :null_object => true)
14
+ SimpleNavigation::ItemContainer.stub!(:new => @subnav_container)
15
+ end
16
+ context 'block given' do
17
+ it "should create a new ItemContainer with a level+1" do
18
+ SimpleNavigation::ItemContainer.should_receive(:new).with(2)
19
+ SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}) {}
20
+ end
21
+ it "should call the block" do
22
+ @subnav_container.should_receive(:test)
23
+ SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}) {|subnav| subnav.test}
24
+ end
25
+ end
26
+ context 'no block given' do
27
+ context 'items given' do
28
+ before(:each) do
29
+ @items = stub(:items)
30
+ end
31
+ it "should create a new ItemContainer with a level+1" do
32
+ SimpleNavigation::ItemContainer.should_receive(:new).with(2)
33
+ SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, @items)
34
+ end
35
+ it "should set the items on the subnav_container" do
36
+ @subnav_container.should_receive(:items=).with(@items)
37
+ SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, @items)
38
+ end
39
+ end
40
+ context 'no items given' do
41
+ it "should not create a new ItemContainer" do
42
+ SimpleNavigation::ItemContainer.should_not_receive(:new)
43
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
44
+ end
45
+ end
46
+ end
47
+ end
11
48
  context 'method' do
12
49
  context 'defined' do
13
50
  before(:each) do
14
51
  @options = {:method => :delete}
15
- @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
52
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
16
53
  end
17
54
  it 'should set the method as instance_var' do
18
55
  @item.method.should == :delete
@@ -93,7 +130,7 @@ describe SimpleNavigation::Item do
93
130
  context 'with classes defined in options' do
94
131
  before(:each) do
95
132
  @options = {:class => 'my_class'}
96
- @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
133
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
97
134
  end
98
135
  context 'with item selected' do
99
136
  before(:each) do
@@ -113,7 +150,7 @@ describe SimpleNavigation::Item do
113
150
  context 'without classes in options' do
114
151
  before(:each) do
115
152
  @options = {}
116
- @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options, nil)
153
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
117
154
  end
118
155
  context 'with item selected' do
119
156
  before(:each) do
@@ -305,34 +342,34 @@ describe SimpleNavigation::Item do
305
342
  @global = stub(:config)
306
343
  SimpleNavigation.stub!(:config => @global)
307
344
  end
308
- context 'global auto_hightlight on' do
345
+ context 'global auto_highlight on' do
309
346
  before(:each) do
310
347
  @global.stub!(:auto_highlight => true)
311
348
  end
312
- context 'container auto_hightlight on' do
349
+ context 'container auto_highlight on' do
313
350
  before(:each) do
314
351
  @item_container.stub!(:auto_highlight => true)
315
352
  end
316
353
  it {@item.send(:auto_highlight?).should be_true}
317
354
  end
318
- context 'container auto_hightlight off' do
355
+ context 'container auto_highlight off' do
319
356
  before(:each) do
320
357
  @item_container.stub!(:auto_highlight => false)
321
358
  end
322
359
  it {@item.send(:auto_highlight?).should be_false}
323
360
  end
324
361
  end
325
- context 'global auto_hightlight off' do
362
+ context 'global auto_highlight off' do
326
363
  before(:each) do
327
364
  @global.stub!(:auto_highlight => false)
328
365
  end
329
- context 'container auto_hightlight on' do
366
+ context 'container auto_highlight on' do
330
367
  before(:each) do
331
368
  @item_container.stub!(:auto_highlight => true)
332
369
  end
333
370
  it {@item.send(:auto_highlight?).should be_false}
334
371
  end
335
- context 'container auto_hightlight off' do
372
+ context 'container auto_highlight off' do
336
373
  before(:each) do
337
374
  @item_container.stub!(:auto_highlight => false)
338
375
  end
@@ -341,4 +378,20 @@ describe SimpleNavigation::Item do
341
378
  end
342
379
  end
343
380
 
381
+ describe 'autogenerated_item_id' do
382
+ context 'calls' do
383
+ before(:each) do
384
+ @id_generator = stub(:id_generator)
385
+ SimpleNavigation.config.stub!(:id_generator => @id_generator)
386
+ end
387
+ it "should call the configured generator with the key as param" do
388
+ @id_generator.should_receive(:call).with(:my_key)
389
+ @item.send(:autogenerated_item_id)
390
+ end
391
+ end
392
+ context 'default generator' do
393
+ it {@item.send(:autogenerated_item_id).should == 'my_key'}
394
+ end
395
+ end
396
+
344
397
  end
@@ -0,0 +1,60 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe SimpleNavigation::ItemsProvider do
4
+
5
+ before(:each) do
6
+ @provider = stub(:provider)
7
+ @items_provider = SimpleNavigation::ItemsProvider.new(@provider)
8
+ end
9
+
10
+ describe 'initialize' do
11
+ it "should set the provider" do
12
+ @items_provider.provider.should == @provider
13
+ end
14
+ end
15
+
16
+ describe 'items' do
17
+ before(:each) do
18
+ @items = stub(:items)
19
+ end
20
+ context 'provider is symbol' do
21
+ before(:each) do
22
+ @items_provider.instance_variable_set(:@provider, :provider_method)
23
+ @context = stub(:context, :provider_method => @items)
24
+ SimpleNavigation::Configuration.stub!(:context_for_eval => @context)
25
+ end
26
+ it "should call the method specified by symbol on the context" do
27
+ @context.should_receive(:provider_method)
28
+ @items_provider.items
29
+ end
30
+ it "should return the items returned by the helper method" do
31
+ @items_provider.items.should == @items
32
+ end
33
+ end
34
+ context 'provider responds to items' do
35
+ before(:each) do
36
+ @provider.stub!(:items => @items)
37
+ end
38
+ it "should get the items from the items_provider" do
39
+ @provider.should_receive(:items)
40
+ @items_provider.items
41
+ end
42
+ it "should return the items of the provider" do
43
+ @items_provider.items.should == @items
44
+ end
45
+ end
46
+ context 'provider is a collection' do
47
+ before(:each) do
48
+ @items_collection = []
49
+ @items_provider.instance_variable_set(:@provider, @items_collection)
50
+ end
51
+ it "should return the collection itsself" do
52
+ @items_provider.items.should == @items_collection
53
+ end
54
+ end
55
+ context 'neither symbol nor items_provider.items nor collection' do
56
+ it {lambda {@items_provider.items}.should raise_error}
57
+ end
58
+ end
59
+
60
+ end
@@ -7,16 +7,16 @@ describe SimpleNavigation::Renderer::List do
7
7
 
8
8
  def sub_items
9
9
  [
10
- [:subnav1, 'subnav1', 'subnav1_url', {}, nil],
11
- [:subnav2, 'subnav2', 'subnav2_url', {}, nil]
10
+ [:subnav1, 'subnav1', 'subnav1_url', {}],
11
+ [:subnav2, 'subnav2', 'subnav2_url', {}]
12
12
  ]
13
13
  end
14
14
 
15
15
  def primary_items
16
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]
17
+ [:users, 'users', 'first_url', {:id => 'my_id'}],
18
+ [:invoices, 'invoices', 'second_url', {}],
19
+ [:accounts, 'accounts', 'third_url', {:style => 'float:right'}]
20
20
  ]
21
21
  end
22
22
 
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: 2.0.1
4
+ version: 2.1.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-10-13 00:00:00 +02:00
12
+ date: 2009-12-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -37,7 +37,9 @@ files:
37
37
  - lib/simple_navigation/controller_methods.rb
38
38
  - lib/simple_navigation/helpers.rb
39
39
  - lib/simple_navigation/item.rb
40
+ - lib/simple_navigation/item_adapter.rb
40
41
  - lib/simple_navigation/item_container.rb
42
+ - lib/simple_navigation/items_provider.rb
41
43
  - lib/simple_navigation/renderer/base.rb
42
44
  - lib/simple_navigation/renderer/list.rb
43
45
  - rails/init.rb
@@ -45,8 +47,10 @@ files:
45
47
  - spec/lib/simple_navigation/configuration_spec.rb
46
48
  - spec/lib/simple_navigation/controller_methods_spec.rb
47
49
  - spec/lib/simple_navigation/helpers_spec.rb
50
+ - spec/lib/simple_navigation/item_adapter_spec.rb
48
51
  - spec/lib/simple_navigation/item_container_spec.rb
49
52
  - spec/lib/simple_navigation/item_spec.rb
53
+ - spec/lib/simple_navigation/items_provider_spec.rb
50
54
  - spec/lib/simple_navigation/renderer/base_spec.rb
51
55
  - spec/lib/simple_navigation/renderer/list_spec.rb
52
56
  - spec/lib/simple_navigation_spec.rb
@@ -85,8 +89,10 @@ test_files:
85
89
  - spec/lib/simple_navigation/configuration_spec.rb
86
90
  - spec/lib/simple_navigation/controller_methods_spec.rb
87
91
  - spec/lib/simple_navigation/helpers_spec.rb
92
+ - spec/lib/simple_navigation/item_adapter_spec.rb
88
93
  - spec/lib/simple_navigation/item_container_spec.rb
89
94
  - spec/lib/simple_navigation/item_spec.rb
95
+ - spec/lib/simple_navigation/items_provider_spec.rb
90
96
  - spec/lib/simple_navigation/renderer/base_spec.rb
91
97
  - spec/lib/simple_navigation/renderer/list_spec.rb
92
98
  - spec/lib/simple_navigation_spec.rb