xebec 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -62,6 +62,10 @@ If you only have one navigation bar on your site, you can leave off the name, li
62
62
 
63
63
  <%= nav_bar %>
64
64
 
65
+ If you want to add extra HTML attributes to the root navigation bar element (say, for compatibility with existing CSS and Javascript), you can pass them to the `nav_bar` call:
66
+
67
+ <%= nav_bar :site, :id => 'sitenav' %>
68
+
65
69
  Xebec will assign this navigation bar the name `:default` in case you need to refer to it elsewhere, but you probably won't.
66
70
 
67
71
  #### Populating a Navigation Bar ####
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.1.0
@@ -11,15 +11,21 @@ module Xebec
11
11
  # Looks up the named nav bar, creates it if it
12
12
  # doesn't exist, and evaluates the the block, if
13
13
  # given, in the scope of +self+, yielding the nav bar.
14
- def look_up_nav_bar_and_eval(name = nil, &block)
14
+ #
15
+ # @param [Symbol, String] name the name of the navigation bar to look up
16
+ # @param [Hash] html_attributes additional HTML attributes to add to the
17
+ # navigation bar
18
+ def look_up_nav_bar_and_eval(name = nil, html_attributes = {}, &block)
15
19
  name ||= Xebec::NavBar::DEFAULT_NAME
16
- look_up_nav_bar(name).tap do |bar|
20
+ look_up_nav_bar(name, html_attributes).tap do |bar|
17
21
  block.bind(self).call(bar) if block_given?
18
22
  end
19
23
  end
20
24
 
21
- def look_up_nav_bar(name)
22
- nav_bars[name] ||= NavBar.new(name)
25
+ def look_up_nav_bar(name, html_attributes)
26
+ (nav_bars[name] ||= NavBar.new(name, html_attributes)).tap do |bar|
27
+ bar.html_attributes.merge!(html_attributes)
28
+ end
23
29
  end
24
30
 
25
31
  def nav_bars
data/lib/xebec/nav_bar.rb CHANGED
@@ -8,13 +8,17 @@ module Xebec
8
8
 
9
9
  attr_reader :name
10
10
  attr_reader :items
11
+ attr_reader :html_attributes
11
12
  attr_accessor :current
12
13
 
13
14
  # Create a new NavBar object.
14
15
  #
15
16
  # @param [String] name the name of the navigation bar; defaults to :default
16
- def initialize(name = nil)
17
+ # @param [Hash] html_attributes additional HTML attributes for the
18
+ # navigation bar, e.g. { :id => 'my-navbar' }
19
+ def initialize(name = nil, html_attributes = nil)
17
20
  @name = name || DEFAULT_NAME
21
+ @html_attributes = html_attributes || {}
18
22
  @items = []
19
23
  @current = nil
20
24
  end
@@ -13,7 +13,7 @@ module Xebec
13
13
  # or "=navbar" in HAML), renders the navigation bar.
14
14
  #
15
15
  # @example
16
- # <%= navbar :tabs%>
16
+ # <%= navbar :tabs %>
17
17
  # # => <ul class="navbar tabs">...</ul>
18
18
  #
19
19
  # @see Xebec::NavBarProxy#to_s
@@ -31,8 +31,8 @@ module Xebec
31
31
  # @see Xebec::HasNavBars#nav_bar
32
32
  #
33
33
  # @return [Xebec::NavBarProxy]
34
- def nav_bar(name = nil, &block)
35
- look_up_nav_bar_and_eval name, &block
34
+ def nav_bar(name = nil, html_attributes = {}, &block)
35
+ look_up_nav_bar_and_eval name, html_attributes, &block
36
36
  end
37
37
 
38
38
  # Renders a navigation bar if and only if it contains any
@@ -40,8 +40,8 @@ module Xebec
40
40
  # accept a block.
41
41
  #
42
42
  # @return [String, Xebec::NavBarProxy]
43
- def nav_bar_unless_empty(name = nil)
44
- bar = look_up_nav_bar name
43
+ def nav_bar_unless_empty(name = nil, html_attributes = {})
44
+ bar = look_up_nav_bar name, html_attributes
45
45
  bar.empty? ? '' : bar
46
46
  end
47
47
 
@@ -63,8 +63,8 @@ EOS
63
63
 
64
64
  # Override HasNavBars#look_up_nav_bar to replace with a
65
65
  # proxy if necessary.
66
- def look_up_nav_bar(name)
67
- bar = super(name)
66
+ def look_up_nav_bar(name, html_attributes)
67
+ bar = super(name, html_attributes)
68
68
  if bar.kind_of?(Xebec::NavBar)
69
69
  bar = nav_bars[bar.name] = NavBarProxy.new(bar, self)
70
70
  end
@@ -70,19 +70,21 @@ module Xebec
70
70
  attr_reader :bar, :helper
71
71
 
72
72
  def root_navbar_element
73
- klass = "#{bar.name}"
73
+ html_attributes = bar.html_attributes
74
+ html_attributes[:class] ||= ''
75
+ (html_attributes[:class] << " #{bar.name}").strip!
74
76
  if helper.user_agent_supports_html5?
75
- return :nav, { :class => klass }
77
+ return :nav, html_attributes
76
78
  else
77
- klass << ' navbar'
78
- return :div, { :class => klass }
79
+ html_attributes[:class] << " navbar"
80
+ return :div, html_attributes
79
81
  end
80
82
  end
81
83
 
82
84
  def render_nav_item(item)
83
85
  text = text_for_nav_item item
84
86
  href = href_for_nav_item item
85
- options = is_current_nav_item?(item, href) ? { :class => :current } : {}
87
+ options = is_current_nav_item?(item, href) ? { :class => Xebec.currently_selected_nav_item_class } : {}
86
88
  helper.content_tag :li, options do
87
89
  helper.link_to_unless_current text, href
88
90
  end
data/lib/xebec.rb CHANGED
@@ -1,5 +1,14 @@
1
1
  module Xebec
2
2
 
3
+ class <<self
4
+
5
+ # The CSS class that is added to navigation items that are
6
+ # "active." Defaults to "current."
7
+ attr_accessor :currently_selected_nav_item_class
8
+
9
+ end
10
+ self.currently_selected_nav_item_class = :current
11
+
3
12
  autoload :NavBarHelper, 'xebec/nav_bar_helper'
4
13
  autoload :ControllerSupport, 'xebec/controller_support'
5
14
  autoload :StylesheetGenerator, 'xebec/stylesheet_generator'
@@ -27,6 +27,11 @@ class NavBarHelperTest < Test::Unit::TestCase
27
27
  assert_equal desserts, @helper.nav_bar('desserts')
28
28
  end
29
29
 
30
+ should 'allow additional HTML attributes' do
31
+ salads = @helper.nav_bar(:salads, :id => 'salads-nav')
32
+ assert_equal 'salads-nav', @helper.nav_bar(:salads).html_attributes[:id]
33
+ end
34
+
30
35
  should "evaluate a block in the helper's scope" do
31
36
  @helper.expects(:zoink!)
32
37
  @helper.nav_bar do
@@ -33,6 +33,12 @@ class NavBarProxyTest < Test::Unit::TestCase
33
33
  assert_equal 'elephants', @proxy.name
34
34
  end
35
35
 
36
+ should 'support additional HTML properties' do
37
+ @bar.html_attributes.merge!(:id => 'salads-nav')
38
+ assert_equal({:id => 'salads-nav'}, @proxy.html_attributes)
39
+ assert_select_from @proxy.to_s, '#salads-nav'
40
+ end
41
+
36
42
  should 'not respond to a method that the underlying NavBar does not' do
37
43
  assert !@proxy.respond_to?(:cromulize)
38
44
  end
@@ -89,6 +95,20 @@ class NavBarProxyTest < Test::Unit::TestCase
89
95
  assert_select 'li.current', 'Foo'
90
96
  end
91
97
  end
98
+ context 'when Xebec is configured to use a different "current" class' do
99
+ setup do
100
+ @old_class = Xebec.currently_selected_nav_item_class
101
+ Xebec.currently_selected_nav_item_class = 'active'
102
+ end
103
+ should 'use the configured CSS class' do
104
+ assert_select_from @proxy.to_s, 'ul' do
105
+ assert_select 'li.active', 'Foo'
106
+ end
107
+ end
108
+ teardown do
109
+ Xebec.currently_selected_nav_item_class = @old_class
110
+ end
111
+ end
92
112
  end
93
113
 
94
114
  context 'with a NavBar that has a navigation item not set as current' do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 2.0.0
9
+ version: 2.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - James A. Rosen