simple-navigation 3.6.0 → 3.7.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
+ *3.7.0
2
+
3
+ * Added new adapater for working with the Nanoc static site generation framework.
4
+ * Fix issue #22 - last link in a breadcrumb trail may now be rendered as static text insted by supplying :static_leaf => true as an option
5
+ * Allow breadcrumbs to be provided with link id and classes by supplying :allow_classes_and_ids => true as an option
6
+
1
7
  *3.6.0
2
8
 
3
9
  * Added linkless items functionality - the `url` parameter is now optional, items which aren't links will be rendered within a 'span' element rather than an 'a' element. `options` remain optional (defaults to an empty Hash). `options` may be provided without providing a `url` (detected by checking if the '`url`' parameter is a Hash or otherwise).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.6.0
1
+ 3.7.0
@@ -46,6 +46,7 @@ module SimpleNavigation
46
46
  return :rails if defined?(Rails)
47
47
  return :padrino if defined?(Padrino)
48
48
  return :sinatra if defined?(Sinatra)
49
+ return :nanoc if defined?(Nanoc3)
49
50
  raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
50
51
  end
51
52
 
@@ -58,6 +59,8 @@ module SimpleNavigation
58
59
  SimpleNavigation::Adapters::Sinatra
59
60
  when :padrino
60
61
  SimpleNavigation::Adapters::Padrino
62
+ when :nanoc
63
+ SimpleNavigation::Adapters::Nanoc
61
64
  end
62
65
  end
63
66
 
@@ -5,5 +5,6 @@ module SimpleNavigation
5
5
  autoload :Rails, 'simple_navigation/adapters/rails'
6
6
  autoload :Padrino, 'simple_navigation/adapters/padrino'
7
7
  autoload :Sinatra, 'simple_navigation/adapters/sinatra'
8
+ autoload :Nanoc, 'simple_navigation/adapters/nanoc'
8
9
  end
9
- end
10
+ end
@@ -0,0 +1,45 @@
1
+ module SimpleNavigation
2
+ module Adapters
3
+ class Nanoc < Base
4
+ class << self
5
+ def register(root)
6
+ SimpleNavigation.set_env(root, 'development')
7
+ Nanoc3::Context.send(:include, SimpleNavigation::Helpers)
8
+ end
9
+ end
10
+
11
+ def initialize(ctx)
12
+ @context = ctx
13
+ end
14
+
15
+ # Returns the context in which the config files will be evaluated
16
+ def context_for_eval
17
+ context
18
+ end
19
+
20
+ # Returns true if the current request's url matches the specified url.
21
+ # Used to determine if an item should be autohighlighted.
22
+ def current_page?(url)
23
+ path = context.item.path
24
+ path && path.chop == url
25
+ end
26
+
27
+ # Returns a link with the specified name, url and options.
28
+ # Used for rendering.
29
+ def link_to(name, url, options={})
30
+ "<a href='#{url}' #{to_attributes(options)}>#{name}</a>"
31
+ end
32
+
33
+ # Returns a tag of the specified type, content and options.
34
+ # Used for rendering.
35
+ def content_tag(type, content, options={})
36
+ "<#{type} #{to_attributes(options)}>#{content}</#{type}>"
37
+ end
38
+
39
+ private
40
+ def to_attributes(options)
41
+ options.map {|k, v| v.nil? ? nil : "#{k}='#{v}'"}.compact.join(' ')
42
+ end
43
+ end
44
+ end
45
+ end
@@ -59,7 +59,7 @@ module SimpleNavigation
59
59
  # Returns the configured active_leaf_class if the item is the selected leaf,
60
60
  # nil otherwise
61
61
  def active_leaf_class
62
- selected_by_condition? ? SimpleNavigation.config.active_leaf_class : nil
62
+ !selected_by_subnav? && selected_by_condition? ? SimpleNavigation.config.active_leaf_class : nil
63
63
  end
64
64
 
65
65
  # Returns the configured selected_class if the item is selected,
@@ -65,7 +65,43 @@ module SimpleNavigation
65
65
  def expand_sub_navigation?(item)
66
66
  expand_all? || item.selected?
67
67
  end
68
-
68
+
69
+ # to allow overriding when there is specific logic determining
70
+ # when a link should not be rendered (eg. breadcrumbs renderer
71
+ # does not render the final breadcrumb as a link when instructed
72
+ # not to do so.)
73
+ def suppress_link?(item)
74
+ item.url.nil?
75
+ end
76
+
77
+ # determine and return link or static content depending on
78
+ # item/renderer conditions.
79
+ def tag_for(item)
80
+ if suppress_link?(item)
81
+ content_tag('span', item.name, link_options_for(item).except(:method))
82
+ else
83
+ link_to(item.name, item.url, options_for(item))
84
+ end
85
+ end
86
+
87
+ # to allow overriding when link options should be special-cased
88
+ # (eg. links renderer uses item options for the a-tag rather
89
+ # than an li-tag).
90
+ def options_for(item)
91
+ link_options_for(item)
92
+ end
93
+
94
+ # Extracts the options relevant for the generated link
95
+ #
96
+ def link_options_for(item)
97
+ special_options = {:method => item.method, :class => item.selected_class}.reject {|k, v| v.nil? }
98
+ link_options = item.html_options[:link]
99
+ return special_options unless link_options
100
+ opts = special_options.merge(link_options)
101
+ opts[:class] = [link_options[:class], item.selected_class].flatten.compact.join(' ')
102
+ opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
103
+ opts
104
+ end
69
105
  end
70
106
  end
71
107
  end
@@ -32,14 +32,21 @@ module SimpleNavigation
32
32
  @join_with ||= options[:join_with] || " "
33
33
  end
34
34
 
35
- def tag_for(item)
36
- if item.url.nil?
37
- content_tag('span', item.name, item.html_options.except(:class,:id))
35
+ def suppress_link?(item)
36
+ super || (options[:static_leaf] && item.active_leaf_class)
37
+ end
38
+
39
+ # Extracts the options relevant for the generated link
40
+ #
41
+ def link_options_for(item)
42
+ if options[:allow_classes_and_ids]
43
+ opts = super
44
+ opts[:id] = "breadcrumb_#{opts[:id]}" if opts[:id]
45
+ opts
38
46
  else
39
- link_to(item.name, item.url, {:method => item.method}.merge(item.html_options.except(:class,:id)))
47
+ {:method => item.method}.merge(item.html_options.except(:class,:id))
40
48
  end
41
49
  end
42
50
  end
43
-
44
51
  end
45
52
  end
@@ -11,7 +11,6 @@ module SimpleNavigation
11
11
  # The ItemContainer's dom_class and dom_id are applied to the surrounding <div> element.
12
12
  #
13
13
  class Links < SimpleNavigation::Renderer::Base
14
-
15
14
  def render(item_container)
16
15
  div_content = item_container.items.inject([]) do |list, item|
17
16
  list << tag_for(item)
@@ -19,14 +18,10 @@ module SimpleNavigation
19
18
  content_tag(:div, div_content, {:id => item_container.dom_id, :class => item_container.dom_class})
20
19
  end
21
20
 
22
- def tag_for(item)
23
- if item.url.nil?
24
- content_tag('span', item.name, item.html_options)
25
- else
26
- link_to(item.name, item.url, {:method => item.method}.merge(item.html_options))
27
- end
21
+ protected
22
+ def options_for(item)
23
+ {:method => item.method}.merge(item.html_options)
28
24
  end
29
25
  end
30
-
31
26
  end
32
27
  end
@@ -9,7 +9,6 @@ module SimpleNavigation
9
9
  # 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.
10
10
  # 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.
11
11
  class List < SimpleNavigation::Renderer::Base
12
-
13
12
  def render(item_container)
14
13
  list_content = item_container.items.inject([]) do |list, item|
15
14
  li_options = item.html_options.reject {|k, v| k == :link}
@@ -25,31 +24,6 @@ module SimpleNavigation
25
24
  content_tag(:ul, list_content, {:id => item_container.dom_id, :class => item_container.dom_class})
26
25
  end
27
26
  end
28
-
29
- protected
30
-
31
- def tag_for(item)
32
- if item.url.nil?
33
- content_tag('span', item.name, link_options_for(item).except(:method))
34
- else
35
- link_to(item.name, item.url, link_options_for(item))
36
- end
37
- end
38
-
39
- # Extracts the options relevant for the generated link
40
- #
41
- def link_options_for(item)
42
- special_options = {:method => item.method, :class => item.selected_class}.reject {|k, v| v.nil? }
43
- link_options = item.html_options[:link]
44
- return special_options unless link_options
45
- opts = special_options.merge(link_options)
46
- opts[:class] = [link_options[:class], item.selected_class].flatten.compact.join(' ')
47
- opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
48
- opts
49
- end
50
-
51
-
52
27
  end
53
-
54
28
  end
55
29
  end
@@ -45,6 +45,33 @@ describe SimpleNavigation::Renderer::Breadcrumbs do
45
45
  tag["class"].should be_nil
46
46
  end
47
47
  end
48
+
49
+ context 'with allow_classes_and_ids option' do
50
+ before(:each) do
51
+ @selection = HTML::Selector.new('div a').select(render(:users, :level => :all, :allow_classes_and_ids => true))
52
+ end
53
+ it "should render class and id" do
54
+ @selection.each do |tag|
55
+ raise unless tag.name == "a"
56
+ tag["id"].should_not be_nil
57
+ tag["class"].should_not be_nil
58
+ end
59
+ end
60
+ end
61
+
62
+ context 'with static_leaf option' do
63
+ before(:each) do
64
+ @selection = HTML::Selector.new('div *').select(render(:subnav1, :level => :all, :static_leaf => true))
65
+ end
66
+ it "should render link for non-leaes" do
67
+ @selection[0..-2].each do |tag|
68
+ tag.name.should == 'a'
69
+ end
70
+ end
71
+ it "should not render link for leaf" do
72
+ @selection.last.name.should == 'span'
73
+ end
74
+ end
48
75
  end
49
76
 
50
77
 
@@ -41,7 +41,7 @@ end
41
41
 
42
42
  def primary_items
43
43
  [
44
- [:users, 'users', 'first_url', {:id => 'my_id'}],
44
+ [:users, 'users', 'first_url', {:id => 'my_id', :link => {:id => 'my_link_id'}}],
45
45
  [:invoices, 'invoices', 'second_url'],
46
46
  [:accounts, 'accounts', 'third_url', {:style => 'float:right', :link => {:style => 'float:left'}}],
47
47
  [:miscellany, 'miscellany']
@@ -77,10 +77,10 @@ def select_item(key)
77
77
  if(key == :subnav1)
78
78
  select_item(:invoices)
79
79
  primary_item(:invoices) do |item|
80
- item.instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}.stub!(:selected? => true)
80
+ item.instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}.stub!(:selected? => true, :selected_by_condition? => true)
81
81
  end
82
82
  else
83
- primary_item(key) {|item| item.stub!(:selected? => true, :selected_by_condition? => true) unless item.frozen?}
83
+ primary_item(key) {|item| item.stub!(:selected? => true) unless item.frozen?}
84
84
  end
85
85
  end
86
86
 
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: 31
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
- - 6
8
+ - 7
9
9
  - 0
10
- version: 3.6.0
10
+ version: 3.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andi Schacke
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-12-03 00:00:00 +00:00
19
+ date: 2012-02-20 00:00:00 +00:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -101,6 +101,7 @@ files:
101
101
  - lib/simple_navigation.rb
102
102
  - lib/simple_navigation/adapters.rb
103
103
  - lib/simple_navigation/adapters/base.rb
104
+ - lib/simple_navigation/adapters/nanoc.rb
104
105
  - lib/simple_navigation/adapters/padrino.rb
105
106
  - lib/simple_navigation/adapters/rails.rb
106
107
  - lib/simple_navigation/adapters/sinatra.rb