simple-navigation 3.1.1 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ *3.2.0
2
+
3
+ * Added Renderer::Text for rendering selected navigation items without markup (useful for dynamic page titles). Credits to Tim Cowlishaw.
4
+ * Added ability to add custom markup around item names specifying a name_generator in the config file. Thanks to Jack Dempsey.
5
+
1
6
  *3.1.1
2
7
 
3
8
  * Item#selected_by_url? now strips anchors from the item's url before comparing it with the current request's url. Credits to opengovernment.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 3.2.0
@@ -17,6 +17,10 @@ SimpleNavigation::Configuration.run do |navigation|
17
17
  # The example below would add a prefix to each key.
18
18
  # navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"}
19
19
 
20
+ # If you need to add custom html around item names, you can define a proc that will be called with the name you pass in to the navigation.
21
+ # The example below shows how to wrap items spans.
22
+ # navigation.name_generator = Proc.new {|name| "<span>#{name}</span>"}
23
+
20
24
  # The auto highlight feature is turned on by default.
21
25
  # This turns it off globally (for the whole plugin)
22
26
  # navigation.auto_highlight = false
@@ -24,7 +24,8 @@ module SimpleNavigation
24
24
  self.registered_renderers = {
25
25
  :list => SimpleNavigation::Renderer::List,
26
26
  :links => SimpleNavigation::Renderer::Links,
27
- :breadcrumbs => SimpleNavigation::Renderer::Breadcrumbs
27
+ :breadcrumbs => SimpleNavigation::Renderer::Breadcrumbs,
28
+ :text => SimpleNavigation::Renderer::Text
28
29
  }
29
30
 
30
31
  class << self
@@ -6,7 +6,7 @@ module SimpleNavigation
6
6
  class Configuration
7
7
  include Singleton
8
8
 
9
- attr_accessor :renderer, :selected_class, :autogenerate_item_ids, :id_generator, :auto_highlight
9
+ attr_accessor :renderer, :selected_class, :autogenerate_item_ids, :id_generator, :auto_highlight, :name_generator
10
10
  attr_reader :primary_navigation
11
11
 
12
12
  class << self
@@ -29,6 +29,7 @@ module SimpleNavigation
29
29
  @selected_class = 'selected'
30
30
  @autogenerate_item_ids = true
31
31
  @id_generator = Proc.new {|id| id.to_s }
32
+ @name_generator = Proc.new {|name| name}
32
33
  @auto_highlight = true
33
34
  end
34
35
 
@@ -2,7 +2,7 @@ module SimpleNavigation
2
2
 
3
3
  # Represents an item in your navigation. Gets generated by the item method in the config-file.
4
4
  class Item
5
- attr_reader :key, :name, :url, :sub_navigation, :method, :highlights_on
5
+ attr_reader :key, :url, :sub_navigation, :method, :highlights_on
6
6
  attr_writer :html_options
7
7
 
8
8
  # see ItemContainer#item
@@ -25,6 +25,18 @@ module SimpleNavigation
25
25
  end
26
26
  end
27
27
 
28
+ # Returns the item's name. If option :apply_generator is set to true (default),
29
+ # the name will be passed to the name_generator specified in the configuration.
30
+ #
31
+ def name(options = {})
32
+ options.reverse_merge!(:apply_generator => true)
33
+ if (options[:apply_generator])
34
+ SimpleNavigation.config.name_generator.call(@name)
35
+ else
36
+ @name
37
+ end
38
+ end
39
+
28
40
  # Returns true if this navigation item should be rendered as 'selected'.
29
41
  # An item is selected if
30
42
  #
@@ -6,5 +6,6 @@ module SimpleNavigation
6
6
  autoload :List, 'simple_navigation/rendering/renderer/list'
7
7
  autoload :Links, 'simple_navigation/rendering/renderer/links'
8
8
  autoload :Breadcrumbs, 'simple_navigation/rendering/renderer/breadcrumbs'
9
+ autoload :Text, 'simple_navigation/rendering/renderer/text'
9
10
  end
10
- end
11
+ end
@@ -55,7 +55,7 @@ module SimpleNavigation
55
55
  options[:level] = :leaves if options[:level] == :all
56
56
  active_item_container = SimpleNavigation.active_item_container_for(options[:level])
57
57
  if active_item_container && !active_item_container.selected_item.nil?
58
- active_item_container.selected_item.name
58
+ active_item_container.selected_item.name(:apply_generator => false)
59
59
  else
60
60
  ''
61
61
  end
@@ -0,0 +1,26 @@
1
+ module SimpleNavigation
2
+ module Renderer
3
+
4
+ # Renders the 'chain' of selected navigation items as simple text items, joined with an optional separator (similar to breadcrumbs, but without markup).
5
+ #
6
+ class Text < SimpleNavigation::Renderer::Base
7
+
8
+ def render(item_container)
9
+ list(item_container).compact.join(options[:join_with] || " ")
10
+ end
11
+
12
+ private
13
+
14
+ def list(item_container)
15
+ item_container.items.inject([]) do |array, item|
16
+ if item.selected?
17
+ array + [item.name(:apply_generator => false)] + (include_sub_navigation?(item) ? list(item.sub_navigation) : [])
18
+ else
19
+ array
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -52,6 +52,9 @@ describe SimpleNavigation::Configuration do
52
52
  it "should set the id_generator" do
53
53
  @config.id_generator.should_not be_nil
54
54
  end
55
+ it "should set the name_generator" do
56
+ @config.name_generator.should_not be_nil
57
+ end
55
58
  end
56
59
  describe 'items' do
57
60
  before(:each) do
@@ -118,6 +118,18 @@ describe SimpleNavigation::Item do
118
118
 
119
119
  end
120
120
 
121
+ describe 'name' do
122
+ before(:each) do
123
+ SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "<span>#{name}</span>"})
124
+ end
125
+ context 'default (generator is applied)' do
126
+ it {@item.name.should == "<span>name</span>"}
127
+ end
128
+ context 'generator is skipped' do
129
+ it {@item.name(:apply_generator => false).should == 'name'}
130
+ end
131
+ end
132
+
121
133
  describe 'selected?' do
122
134
  context 'explicitly selected' do
123
135
  before(:each) do
@@ -39,6 +39,15 @@ describe SimpleNavigation::Helpers do
39
39
  context 'container does not have selected item' do
40
40
  it {@controller.active_navigation_item_name.should == ''}
41
41
  end
42
+ context 'custom name generator set' do
43
+ before(:each) do
44
+ select_item(:subnav1)
45
+ SimpleNavigation.config.name_generator = Proc.new {|name| "<span>name</span>"}
46
+ end
47
+ it "should not apply the generator" do
48
+ @controller.active_navigation_item_name(:level => 1).should == 'invoices'
49
+ end
50
+ end
42
51
  end
43
52
  context 'no active item_container for desired level' do
44
53
  it {@controller.active_navigation_item_name(:level => 5).should == ''}
@@ -30,6 +30,25 @@ describe SimpleNavigation::Renderer::List do
30
30
  HTML::Selector.new('li a').select(render).should have(3).entries
31
31
  end
32
32
 
33
+ context 'concerning item names' do
34
+ context 'with a custom name generator defined' do
35
+ before(:each) do
36
+ SimpleNavigation.config.name_generator = Proc.new {|name| "<span>name</span>"}
37
+ end
38
+ it "should apply the name generator" do
39
+ HTML::Selector.new('li a span').select(render).should have(3).entries
40
+ end
41
+ end
42
+ context 'no customer generator defined' do
43
+ before(:each) do
44
+ SimpleNavigation.config.name_generator = Proc.new {|name| "name"}
45
+ end
46
+ it "should apply the name generator" do
47
+ HTML::Selector.new('li a span').select(render).should have(0).entries
48
+ end
49
+ end
50
+ end
51
+
33
52
  context 'concerning html attributes' do
34
53
  context 'default case (no options defined for link tag)' do
35
54
  it "should pass the specified html_options to the li element" do
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleNavigation::Renderer::Text do
4
+
5
+ describe 'render' do
6
+
7
+ def render(current_nav=nil, options={:level => :all})
8
+ primary_navigation = primary_container
9
+ select_item(current_nav)
10
+ setup_renderer_for SimpleNavigation::Renderer::Text, :rails, options
11
+ @renderer.render(primary_navigation)
12
+ end
13
+ context 'regarding result' do
14
+
15
+ it "should render the selected page" do
16
+ render(:invoices).should == "invoices"
17
+ end
18
+
19
+ context 'nested sub_navigation' do
20
+ it "should add an entry for each selected item" do
21
+ render(:subnav1).should == "invoices subnav1"
22
+ end
23
+ end
24
+
25
+ context 'with a custom seperator specified' do
26
+ it "should separate the items with the separator" do
27
+ render(:subnav1, :join_with => " | ").should == "invoices | subnav1"
28
+ end
29
+ end
30
+
31
+ context 'custom name generator is set' do
32
+ before(:each) do
33
+ SimpleNavigation.config.name_generator = Proc.new {|name| "<span>name</span>"}
34
+ end
35
+ it "should not apply the name generator (since it is text only)" do
36
+ render(:subnav1, :join_with => " | ").should == "invoices | subnav1"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-navigation
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
- - 1
9
- - 1
10
- version: 3.1.1
8
+ - 2
9
+ - 0
10
+ version: 3.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andi Schacke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-13 00:00:00 +01:00
18
+ date: 2011-02-09 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -87,6 +87,7 @@ files:
87
87
  - lib/simple_navigation/rendering/renderer/breadcrumbs.rb
88
88
  - lib/simple_navigation/rendering/renderer/links.rb
89
89
  - lib/simple_navigation/rendering/renderer/list.rb
90
+ - lib/simple_navigation/rendering/renderer/text.rb
90
91
  - rails/init.rb
91
92
  - spec/lib/simple_navigation/adapters/padrino_spec.rb
92
93
  - spec/lib/simple_navigation/adapters/rails_spec.rb
@@ -102,6 +103,7 @@ files:
102
103
  - spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb
103
104
  - spec/lib/simple_navigation/rendering/renderer/links_spec.rb
104
105
  - spec/lib/simple_navigation/rendering/renderer/list_spec.rb
106
+ - spec/lib/simple_navigation/rendering/renderer/text_spec.rb
105
107
  - spec/lib/simple_navigation_spec.rb
106
108
  - spec/spec_helper.rb
107
109
  has_rdoc: true
@@ -154,5 +156,6 @@ test_files:
154
156
  - spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb
155
157
  - spec/lib/simple_navigation/rendering/renderer/links_spec.rb
156
158
  - spec/lib/simple_navigation/rendering/renderer/list_spec.rb
159
+ - spec/lib/simple_navigation/rendering/renderer/text_spec.rb
157
160
  - spec/lib/simple_navigation_spec.rb
158
161
  - spec/spec_helper.rb