Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.

Commit

Permalink
Add html_options for NavItems
Browse files Browse the repository at this point in the history
  • Loading branch information
nbudin committed Aug 16, 2011
1 parent 7ebad4a commit f322a10
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/xebec/nav_bar.rb
Expand Up @@ -27,14 +27,15 @@ def initialize(name = nil, html_attributes = nil)
#
# @param [String, Symbol] name the name of the item
# @param [String, Proc] href the URL of the item; optional
# @param [Hash] html_options additional html_options to be passed to link_to
#
# To customize the link text, set the internationalization key
# <tt>navbar.{{nav bar name}}.{{nav item name}}</tt>.
#
# @see Xebec::NavBarHelper#nav_bar
# @see Xebec::NavBarProxy#to_s
def nav_item(name, href = nil)
items << Xebec::NavItem.new(name, href)
def nav_item(name, href = nil, html_options = {})
items << Xebec::NavItem.new(name, href, html_options)
end

def empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/xebec/nav_bar_renderer.rb
Expand Up @@ -97,7 +97,7 @@ def render_nav_item(item)
if is_current
helper.content_tag :span, text
else
helper.link_to text, href
helper.link_to text, href, item.html_options
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/xebec/nav_item.rb
Expand Up @@ -2,18 +2,19 @@ module Xebec

class NavItem

attr_reader :name, :href
attr_reader :name, :href, :html_options

# Create a new navigation item.
#
# @param [String, Symbol] name the name of the item
# @param [String, Hash, ActiveRecord::Base] href whither the navigation item links;
# defaults to the named route, "#{name}_path"
# @param [Hash] html_options to be passed to the link_to helper method
#
# @see ActionView::Helpers::UrlHelper#url_for
def initialize(name, href = nil)
def initialize(name, href = nil, html_options = {})
raise ArgumentError.new("#{name || '<nil>'} is not a valid name for a navigation item") if name.blank?
@name, @href = name, href
@name, @href, @html_options = name, href, html_options
end

end
Expand Down
13 changes: 13 additions & 0 deletions test/nav_bar_renderer_test.rb
Expand Up @@ -156,6 +156,19 @@ class NavBarRendererTest < Test::Unit::TestCase
end
end
end

context 'with a NavBar that has a navigation item with html_options' do
setup do
@bar.nav_item :foo, 'http://foo.com', :method => :delete
end
should 'render a navigation bar with the appropriate items' do
assert_select_from @renderer.to_s, 'ul' do
assert_select 'li.foo' do
assert_select 'a[href="http://foo.com"][data-method=delete]', 'Foo'
end
end
end
end

context 'with a NavBar that has a navigation item that links to the current page' do
setup do
Expand Down

0 comments on commit f322a10

Please sign in to comment.