Skip to content

Commit

Permalink
. simplifying list-renderer to use convenience methods from base class
Browse files Browse the repository at this point in the history
  • Loading branch information
andi committed Jan 17, 2010
1 parent 818904b commit 031959a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
17 changes: 6 additions & 11 deletions lib/simple_navigation/renderer/list.rb
Expand Up @@ -4,29 +4,24 @@ module Renderer
# Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
# It adds the 'selected' class to li element AND the link inside the li element that is currently active.
#
# If the sub navigation should be included, it renders another <ul> containing the sub navigation inside the active <li> element.
#
# If the SimpleNavigation.config.render_all_levels option is set to true, it always renders all levels of navigation (fully expanded tree).
# If the sub navigation should be included (based on the level and expand_all options), it renders another <ul> containing the sub navigation inside the active <li> element.
#
# 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.
# 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.
class List < SimpleNavigation::Renderer::Base

def render(item_container, include_sub_navigation=false, max_level = nil)
def render(item_container)
list_content = item_container.items.inject([]) do |list, item|
html_options = item.html_options
li_content = link_to(item.name, item.url, :class => item.selected_class, :method => item.method)
if item.sub_navigation
if SimpleNavigation.config.render_all_levels || (include_sub_navigation && item.selected?)
# li_content << (item.sub_navigation.render(include_sub_navigation))
li_content << (item.sub_navigation.render(include_sub_navigation, max_level)) unless !max_level.nil? && (item.sub_navigation.level > max_level)
end
if include_sub_navigation?(item)
li_content << render_sub_navigation_for(item)
end
list << content_tag(:li, li_content, html_options)
end
content_tag(:ul, list_content.join, {:id => item_container.dom_id, :class => item_container.dom_class})
end

end

end
end
end
30 changes: 12 additions & 18 deletions spec/lib/simple_navigation/renderer/list_spec.rb
Expand Up @@ -47,11 +47,11 @@ def subnav_container
container
end

def render(current_nav=nil, include_subnav=false)
def render(current_nav=nil, options={:level => :all})
primary_navigation = primary_container
select_item(current_nav) if current_nav
@renderer = SimpleNavigation::Renderer::List.new
HTML::Document.new(@renderer.render(primary_navigation, include_subnav)).root
@renderer = SimpleNavigation::Renderer::List.new(options)
HTML::Document.new(@renderer.render(primary_navigation)).root
end

it "should render a ul-tag around the items" do
Expand Down Expand Up @@ -103,31 +103,25 @@ def render(current_nav=nil, include_subnav=false)

context 'nested sub_navigation' do
it "should nest the current_primary's subnavigation inside the selected li-element" do
HTML::Selector.new('li.selected ul li').select(render(:invoices, true)).should have(2).entries
HTML::Selector.new('li.selected ul li').select(render(:invoices)).should have(2).entries
end
it "should be possible to identify sub items using an html selector (using ids)" do
HTML::Selector.new('#invoices #subnav1').select(render(:invoices, true)).should have(1).entries
HTML::Selector.new('#invoices #subnav1').select(render(:invoices)).should have(1).entries
end
context 'render_all_levels = false' do
before(:each) do
SimpleNavigation.config.render_all_levels = false
end
context 'expand_all => false' do
it "should not render the invoices submenu if the user-primary is active" do
HTML::Selector.new('#invoices #subnav1').select(render(:users, true)).should be_empty
HTML::Selector.new('#invoices #subnav2').select(render(:users, true)).should be_empty
HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => false)).should be_empty
HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => false)).should be_empty
end
end

context 'render_all_levels = true' do
before(:each) do
SimpleNavigation.config.render_all_levels = true
end
context 'expand_all => true' do
it "should render the invoices submenu even if the user-primary is active" do
HTML::Selector.new('#invoices #subnav1').select(render(:users, true)).should have(1).entry
HTML::Selector.new('#invoices #subnav2').select(render(:users, true)).should have(1).entry
HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
end
end

end

end
Expand Down

0 comments on commit 031959a

Please sign in to comment.