Technical
Tabs Helper

Original template was taken from http://labs.silverorange.com/archives/2004/may/updatedsimple Thanks A lot

I had to implement some tabs, this is how I went about implementing tab selection the rails way.

app/helpers/application.rb


 def tabs(selected_main_tab,selected_sub_tab,tab_sets)
  case tab_sets
   when "legal_entity" 
    tabs_content = [
      ["legal_entity",["show","new"]],
      ["legal_entity_address",["list","new"]],
      ["legal_entity_contact",["list","new"]],
      ["back_office_system",["show"]],
      ["documentation",["documentation_contract","documentation_detail"]],
      ["settlement",["list","new"]],
      ["taxation",["show"]],
      ["legal_entity_audit",[]]
    ]
    when "parent" 
    tabs_content = [
      ["parent",["show","new"]],
      ["parent_address",["show","new"]],
      ["parent_contact",["show","new"]],
      ["legal_entity_links",[]],
      ["parent_audit",[]]
    ]
    when "accounts" 
     tabs_content = [
        ["aliases",["show"]],
        ["exceptions",[]]
      ]
    else
      tabs_content = [[],[]]
  end
   tabs_to_show = %(<div id="header">\n)
   tabs_to_show << %(<ul id="primary">\n)
    if tabs_content != [[],[]]
      tabs_content.each do |main_tab,sub_tabs|
        if main_tab == selected_main_tab
          tabs_to_show <<  %(<li nowrap><span>#{main_tab.titleize}</span></li>\n)
          if !sub_tabs.blank?
            tabs_to_show << %(<ul id="secondary">\n)
              sub_tabs.each do |value|
                if value == selected_sub_tab
                  tabs_to_show << %(<li><span>#{value.titleize}</span></li>\n)
                else
                  tabs_to_show << %(<li>#{link_to value.titleize ,{:controller => main_tab,:action => value}}</li>\n)
                end
          end
          tabs_to_show << %(</ul>\n)
        end
        else
          tabs_to_show << %(<li nowrap="nowrap">#{link_to main_tab.titleize ,{:controller => main_tab }}</li>\n)
        end
      end
      end
   tabs_to_show  << %(</ul>\n)
   tabs_to_show  << %(</div>\n)
   return tabs_to_show
  end

Layout


<div id="body_div">
   <%= tabs(@selected_main_tab,@selected_sub_tab,@tab_sets) %>
    <div id="main">
      <div id="contents">
        <%= @content_for_layout %>
      </div>
    </div>
</div>

Controller


 def initialize
   @selected_main_tab = "legal_entity" 
   @tab_sets = "legal_entity" 
 end
def show
  @selected_sub_tab = "show" 
  ....
end

Short Screencast

tabs_generator.jpg

Tests


  def test_tabs_for_legal_entity_on_show

   legal_entity_with_sub_tab_selected_on_show = %(<div id=\"header\">
<ul id=\"primary\">
<li nowrap><span>Legal Entity</span></li>
<ul id=\"secondary\">
<li><span>Show</span></li>
<li><a href=\"/legal_entity/new\">New</a></li>
</ul>
<li nowrap=\"nowrap\"><a href=\"/legal_entity_address\">Legal Entity Address</a></li>
<li nowrap=\"nowrap\"><a href=\"/legal_entity_contact\">Legal Entity Contact</a></li>
<li nowrap=\"nowrap\"><a href=\"/back_office_system\">Back Office System</a></li>
<li nowrap=\"nowrap\"><a href=\"/documentation\">Documentation</a></li>
<li nowrap=\"nowrap\"><a href=\"/settlement\">Settlement</a></li>
<li nowrap=\"nowrap\"><a href=\"/taxation\">Taxation</a></li>
<li nowrap=\"nowrap\"><a href=\"/legal_entity_audit\">Legal Entity Audit</a></li>
</ul>
</div>
)
    assert_equal legal_entity_with_sub_tab_selected_on_show,tabs("legal_entity","show","legal_entity")
    assert_not_nil tabs("legal_entity","show","legal_entity")
  end

 def test_tabs_sending_parent_and_new_for_tab_sets_and_subtab

    parent_with_sub_tab_new = %(<div id=\"header\">
<ul id=\"primary\">
<li nowrap><span>Parent</span></li>
<ul id=\"secondary\">
<li><a href=\"/parent/show\">Show</a></li>
<li><span>New</span></li>
</ul>
<li nowrap=\"nowrap\"><a href=\"/parent_address\">Parent Address</a></li>
<li nowrap=\"nowrap\"><a href=\"/parent_contact\">Parent Contact</a></li>
<li nowrap=\"nowrap\"><a href=\"/legal_entity_links\">Legal Entity Links</a></li>
<li nowrap=\"nowrap\"><a href=\"/parent_audit\">Parent Audit</a></li>
</ul>
</div>
)
    assert_equal parent_with_sub_tab_new,tabs("parent","new","parent")
    assert_not_nil tabs("parent","new","parent")
  end

P.S. – Any refactoring deeply appreciated