simple-navigation 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ *3.3.0
2
+
3
+ * add a new method active_navigation_item_key which returns the symbol for the currently selected navigation item in a similar way to active_navigation_item_name does for the name (useful for CSS class styling for eg.)
4
+ * open up the helper API to provide active_navigation_item and active_navigation_item_container methods to make it easy to access the items/containers should it be necessary (came for free with the above refactoring)
5
+ * isolate the apply_defaults and load_config private methods from ActionController mixin leakage by refactoring to module class instance methods
6
+ * addition of test coverage for the added helpers within helpers_spec.rb
7
+ * inclusion of new helpers within the rails adapter and minor refactoring to DRY up the helper_method invocations
8
+ * addition of test coverage for the newly included helpers
9
+ * Credits to Mark J. Titorenko for all the changes in this release! Thanks.
10
+
1
11
  *3.2.0
2
12
 
3
13
  * Added Renderer::Text for rendering selected navigation items without markup (useful for dynamic page titles). Credits to Tim Cowlishaw.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.2.0
1
+ 3.3.0
@@ -7,8 +7,9 @@ module SimpleNavigation
7
7
  def self.register
8
8
  SimpleNavigation.set_env(rails_root, rails_env)
9
9
  ActionController::Base.send(:include, SimpleNavigation::Helpers)
10
- ActionController::Base.send(:helper_method, :render_navigation)
11
- ActionController::Base.send(:helper_method, :active_navigation_item_name)
10
+ SimpleNavigation::Helpers.instance_methods.each do |m|
11
+ ActionController::Base.send(:helper_method, m.to_sym)
12
+ end
12
13
  end
13
14
 
14
15
  def initialize(context)
@@ -91,4 +92,4 @@ if defined?(Rails) && Rails::VERSION::MAJOR == 3
91
92
  end
92
93
  end
93
94
  end
94
- end
95
+ end
@@ -32,14 +32,29 @@ module SimpleNavigation
32
32
  # * <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.
33
33
  # * <tt>:renderer</tt> - specify the renderer to be used for rendering the navigation. Either provide the Class or a symbol matching a registered renderer. Defaults to :list (html list renderer).
34
34
  def render_navigation(options={})
35
- options = apply_defaults(options)
36
- load_config(options)
37
- active_item_container = SimpleNavigation.active_item_container_for(options[:level])
38
- active_item_container.render(options) unless active_item_container.nil?
35
+ active_navigation_item_container(options) { |container| container && container.render(options) }
39
36
  end
40
37
 
41
38
  # Returns the name of the currently active navigation item belonging to the specified level.
42
39
  #
40
+ # See Helpers#active_navigation_item for supported options.
41
+ #
42
+ # Returns an empty string if no active item can be found for the specified options
43
+ def active_navigation_item_name(options={})
44
+ active_navigation_item(options,'') { |item| item.name(:apply_generator => false) }
45
+ end
46
+
47
+ # Returns the key of the currently active navigation item belonging to the specified level.
48
+ #
49
+ # See Helpers#active_navigation_item for supported options.
50
+ #
51
+ # Returns <tt>nil</tt> if no active item can be found for the specified options
52
+ def active_navigation_item_key(options={})
53
+ active_navigation_item(options) { |item| item.key }
54
+ end
55
+
56
+ # Returns the currently active navigation item belonging to the specified level.
57
+ #
43
58
  # The following options are supported:
44
59
  # * <tt>:level</tt> - defaults to :all which returns the most specific/deepest selected item (the leaf).
45
60
  # Specify a specific level to only look for the selected item in the specified level of navigation (e.g. :level => 1 for primary_navigation etc...).
@@ -48,35 +63,53 @@ module SimpleNavigation
48
63
  # will be loaded and used for searching the active item.
49
64
  # * <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.
50
65
  #
51
- # Returns an empty string if no active item can be found for the specified options
52
- def active_navigation_item_name(options={})
53
- options = apply_defaults(options)
54
- load_config(options)
55
- options[:level] = :leaves if options[:level] == :all
56
- active_item_container = SimpleNavigation.active_item_container_for(options[:level])
57
- if active_item_container && !active_item_container.selected_item.nil?
58
- active_item_container.selected_item.name(:apply_generator => false)
59
- else
60
- ''
66
+ # Returns the supplied <tt>value_for_nil</tt> object (<tt>nil</tt>
67
+ # by default) if no active item can be found for the specified
68
+ # options
69
+ def active_navigation_item(options={},value_for_nil = nil)
70
+ options[:level] = :leaves if options[:level].nil? || options[:level] == :all
71
+ active_navigation_item_container(options) do |container|
72
+ if container && (item = container.selected_item)
73
+ block_given? ? yield(item) : item
74
+ else
75
+ value_for_nil
76
+ end
61
77
  end
62
78
  end
63
79
 
64
- private
65
-
66
- def load_config(options)
67
- ctx = options.delete(:context)
68
- SimpleNavigation.init_adapter_from self
69
- SimpleNavigation.load_config(ctx)
70
- SimpleNavigation::Configuration.eval_config(ctx)
71
- SimpleNavigation.config.items(options[:items]) if options[:items]
72
- SimpleNavigation.handle_explicit_navigation if SimpleNavigation.respond_to?(:handle_explicit_navigation)
73
- raise "no primary navigation defined, either use a navigation config file or pass items directly to render_navigation" unless SimpleNavigation.primary_navigation
80
+ # Returns the currently active item container belonging to the specified level.
81
+ #
82
+ # The following options are supported:
83
+ # * <tt>:level</tt> - defaults to :all which returns the least specific/shallowest selected item.
84
+ # Specify a specific level to only look for the selected item in the specified level of navigation (e.g. :level => 1 for primary_navigation etc...).
85
+ # * <tt>:context</tt> - specifies the context for which you would like to find the active navigation item. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb).
86
+ # If you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call <tt>active_navigation_item_name(:context => :admin)</tt> the file config/admin_navigation.rb
87
+ # will be loaded and used for searching the active item.
88
+ # * <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.
89
+ #
90
+ # Returns <tt>nil</tt> if no active item container can be found
91
+ def active_navigation_item_container(options={})
92
+ options = SimpleNavigation::Helpers::apply_defaults(options)
93
+ SimpleNavigation::Helpers::load_config(options,self)
94
+ container = SimpleNavigation.active_item_container_for(options[:level])
95
+ block_given? ? yield(container) : container
74
96
  end
75
97
 
76
- def apply_defaults(options)
77
- options[:level] = options.delete(:levels) if options[:levels]
78
- {:context => :default, :level => :all}.merge(options)
79
- end
98
+ class << self
99
+ def load_config(options,includer)
100
+ ctx = options.delete(:context)
101
+ SimpleNavigation.init_adapter_from includer
102
+ SimpleNavigation.load_config(ctx)
103
+ SimpleNavigation::Configuration.eval_config(ctx)
104
+ SimpleNavigation.config.items(options[:items]) if options[:items]
105
+ SimpleNavigation.handle_explicit_navigation if SimpleNavigation.respond_to?(:handle_explicit_navigation)
106
+ raise "no primary navigation defined, either use a navigation config file or pass items directly to render_navigation" unless SimpleNavigation.primary_navigation
107
+ end
80
108
 
109
+ def apply_defaults(options)
110
+ options[:level] = options.delete(:levels) if options[:levels]
111
+ {:context => :default, :level => :all}.merge(options)
112
+ end
113
+ end
81
114
  end
82
115
  end
@@ -30,6 +30,9 @@ describe SimpleNavigation::Adapters::Rails do
30
30
  it "should install the helper methods in the controller" do
31
31
  ActionController::Base.should_receive(:helper_method).with(:render_navigation)
32
32
  ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_name)
33
+ ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_key)
34
+ ActionController::Base.should_receive(:helper_method).with(:active_navigation_item)
35
+ ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_container)
33
36
  SimpleNavigation.register
34
37
  end
35
38
 
@@ -281,4 +284,4 @@ describe SimpleNavigation::Adapters::Rails do
281
284
  end
282
285
  end
283
286
 
284
- end
287
+ end
@@ -7,10 +7,13 @@ describe SimpleNavigation::Helpers do
7
7
 
8
8
  def blackbox_setup()
9
9
  @controller = ControllerMock.new
10
- @controller.stub!(:load_config)
10
+ SimpleNavigation.stub!(:load_config)
11
+ SimpleNavigation::Configuration.stub!(:eval_config)
11
12
  setup_adapter_for :rails
12
- primary_navigation = primary_container
13
- SimpleNavigation.stub!(:primary_navigation => primary_navigation)
13
+ @primary_container, @subnav_container = containers
14
+ @subnav1_item = sub_item(:subnav1)
15
+ @invoices_item = primary_item(:invoices)
16
+ SimpleNavigation.stub!(:primary_navigation => @primary_container)
14
17
  end
15
18
 
16
19
  def whitebox_setup
@@ -54,6 +57,76 @@ describe SimpleNavigation::Helpers do
54
57
  end
55
58
  end
56
59
 
60
+ describe 'active_navigation_item_key' do
61
+ before(:each) do
62
+ blackbox_setup
63
+ end
64
+ context 'active item_container for desired level exists' do
65
+ context 'container has selected item' do
66
+ before(:each) do
67
+ select_item(:subnav1)
68
+ end
69
+ it {@controller.active_navigation_item_key(:level => 2).should == :subnav1}
70
+ it {@controller.active_navigation_item_key.should == :subnav1}
71
+ it {@controller.active_navigation_item_key(:level => 1).should == :invoices}
72
+ it {@controller.active_navigation_item_key(:level => :all).should == :subnav1}
73
+ end
74
+ context 'container does not have selected item' do
75
+ it {@controller.active_navigation_item_key.should == nil}
76
+ end
77
+ end
78
+ context 'no active item_container for desired level' do
79
+ it {@controller.active_navigation_item_key(:level => 5).should == nil}
80
+ end
81
+ end
82
+
83
+ describe 'active_navigation_item' do
84
+ before(:each) do
85
+ blackbox_setup
86
+ end
87
+ context 'active item_container for desired level exists' do
88
+ context 'container has selected item' do
89
+ before(:each) do
90
+ select_item(:subnav1)
91
+ end
92
+ it {@controller.active_navigation_item(:level => 2).should == @subnav1_item}
93
+ it {@controller.active_navigation_item.should == @subnav1_item}
94
+ it {@controller.active_navigation_item(:level => 1).should == @invoices_item}
95
+ it {@controller.active_navigation_item(:level => :all).should == @subnav1_item}
96
+ end
97
+ context 'container does not have selected item' do
98
+ context 'return value defaults to nil' do
99
+ it {@controller.active_navigation_item.should == nil}
100
+ end
101
+ context 'return value reflects passed in value' do
102
+ it {@controller.active_navigation_item({},'none').should == 'none'}
103
+ it {@controller.active_navigation_item({},@invoices_item).should == @invoices_item}
104
+ end
105
+ end
106
+ end
107
+ context 'no active item_container for desired level' do
108
+ it {@controller.active_navigation_item(:level => 5).should == nil}
109
+ end
110
+ end
111
+
112
+ describe 'active_navigation_item_container' do
113
+ before(:each) do
114
+ blackbox_setup
115
+ end
116
+ context 'active item_container for desired level exists' do
117
+ before(:each) do
118
+ select_item(:subnav1)
119
+ end
120
+ it {@controller.active_navigation_item_container(:level => 2).should == @subnav_container}
121
+ it {@controller.active_navigation_item_container.should == @primary_container}
122
+ it {@controller.active_navigation_item_container(:level => 1).should == @primary_container}
123
+ it {@controller.active_navigation_item_container(:level => :all).should == @primary_container}
124
+ end
125
+ context 'no active item_container for desired level' do
126
+ it {@controller.active_navigation_item_container(:level => 5).should == nil}
127
+ end
128
+ end
129
+
57
130
  describe 'render_navigation' do
58
131
 
59
132
  before(:each) do
@@ -189,4 +262,4 @@ describe SimpleNavigation::Helpers do
189
262
  end
190
263
  end
191
264
 
192
- end
265
+ end
data/spec/spec_helper.rb CHANGED
@@ -48,18 +48,28 @@ def primary_items
48
48
  end
49
49
 
50
50
  def primary_container
51
+ containers.first
52
+ end
53
+
54
+ def containers
51
55
  container = SimpleNavigation::ItemContainer.new(1)
52
56
  container.dom_id = 'nav_dom_id'
53
57
  container.dom_class = 'nav_dom_class'
54
58
  @items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
55
59
  @items.each {|i| i.stub!(:selected? => false)}
56
60
  container.instance_variable_set(:@items, @items)
57
- primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, subnav_container)}
58
- container
61
+ sub_container = subnav_container
62
+ primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, sub_container)}
63
+ [container,sub_container]
59
64
  end
60
65
 
61
66
  def primary_item(key)
62
- yield @items.find {|i| i.key == key}
67
+ item = @items.find {|i| i.key == key}
68
+ block_given? ? yield(item) : item
69
+ end
70
+
71
+ def sub_item(key)
72
+ primary_item(:invoices).instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}
63
73
  end
64
74
 
65
75
  def select_item(key)
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-navigation
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 3
8
- - 2
9
- - 0
10
- version: 3.2.0
4
+ prerelease:
5
+ version: 3.3.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - Andi Schacke
@@ -15,8 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-09 00:00:00 +01:00
19
- default_executable:
13
+ date: 2011-06-14 00:00:00 Z
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
16
  name: rspec
@@ -26,11 +20,6 @@ dependencies:
26
20
  requirements:
27
21
  - - ">="
28
22
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 2
32
- - 0
33
- - 1
34
23
  version: 2.0.1
35
24
  type: :development
36
25
  version_requirements: *id001
@@ -42,11 +31,6 @@ dependencies:
42
31
  requirements:
43
32
  - - ">="
44
33
  - !ruby/object:Gem::Version
45
- hash: 7
46
- segments:
47
- - 2
48
- - 3
49
- - 2
50
34
  version: 2.3.2
51
35
  type: :runtime
52
36
  version_requirements: *id002
@@ -106,7 +90,6 @@ files:
106
90
  - spec/lib/simple_navigation/rendering/renderer/text_spec.rb
107
91
  - spec/lib/simple_navigation_spec.rb
108
92
  - spec/spec_helper.rb
109
- has_rdoc: true
110
93
  homepage: http://github.com/andi/simple-navigation
111
94
  licenses: []
112
95
 
@@ -121,41 +104,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
121
104
  requirements:
122
105
  - - ">="
123
106
  - !ruby/object:Gem::Version
124
- hash: 3
125
- segments:
126
- - 0
127
107
  version: "0"
128
108
  required_rubygems_version: !ruby/object:Gem::Requirement
129
109
  none: false
130
110
  requirements:
131
111
  - - ">="
132
112
  - !ruby/object:Gem::Version
133
- hash: 3
134
- segments:
135
- - 0
136
113
  version: "0"
137
114
  requirements: []
138
115
 
139
116
  rubyforge_project: andi
140
- rubygems_version: 1.3.7
117
+ rubygems_version: 1.8.5
141
118
  signing_key:
142
119
  specification_version: 3
143
120
  summary: simple-navigation is a ruby library for creating navigations (with multiple levels) for your Rails2, Rails3, Sinatra or Padrino application.
144
- test_files:
145
- - spec/lib/simple_navigation/adapters/padrino_spec.rb
146
- - spec/lib/simple_navigation/adapters/rails_spec.rb
147
- - spec/lib/simple_navigation/adapters/sinatra_spec.rb
148
- - spec/lib/simple_navigation/core/configuration_spec.rb
149
- - spec/lib/simple_navigation/core/item_adapter_spec.rb
150
- - spec/lib/simple_navigation/core/item_container_spec.rb
151
- - spec/lib/simple_navigation/core/item_spec.rb
152
- - spec/lib/simple_navigation/core/items_provider_spec.rb
153
- - spec/lib/simple_navigation/rails_controller_methods_spec.rb
154
- - spec/lib/simple_navigation/rendering/helpers_spec.rb
155
- - spec/lib/simple_navigation/rendering/renderer/base_spec.rb
156
- - spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb
157
- - spec/lib/simple_navigation/rendering/renderer/links_spec.rb
158
- - spec/lib/simple_navigation/rendering/renderer/list_spec.rb
159
- - spec/lib/simple_navigation/rendering/renderer/text_spec.rb
160
- - spec/lib/simple_navigation_spec.rb
161
- - spec/spec_helper.rb
121
+ test_files: []
122
+