Helper or partial for tab creation

I have this at the moment for creating my tabs, it works ok but was
wondering if it would be faster to have a basic partial with a series of
if statements ? Im just wondering if there are any overheads involved
with generating lis this way.

 #header navigation helper methods
  def main_tabs(controller)

   items  = [ content_tag('li', link_to('Products', categories_path), 
:class => "page_item"),
              content_tag('li', link_to('Pages', pages_path), :class => 
"page_item"),
              content_tag('li', link_to('Mail', emails_path), :class => 
"page_item"),
              content_tag('li', link_to('Accounts', users_path), :class 
=> "page_item")]

    case controller
        when /categories|products|parts|accessories|actions/
            items[0] = content_tag('li', link_to('Products', 
categories_path), :class => "current_page_item")

        when 'pages'
            items[1] = content_tag('li', link_to('Pages', pages_path), 
:class => "current_page_item")

        when 'emails'
            items[2] = content_tag('li', link_to('Mail', emails_path), 
:class => "current_page_item")

        when 'users' && user.admin?
            items[3] = content_tag('li', link_to('Accounts', 
users_path), :class => "current_page_item")
    end

    content_tag('ul', items.to_s, :class=>'menu')
  end

Adam wrote:

I have this at the moment for creating my tabs, it works ok but was
wondering if it would be faster to have a basic partial with a series of
if statements ? Im just wondering if there are any overheads involved
with generating lis this way.

I think that Rails sort of compiles RHTML templates when they’re loaded.
From ActiveRecord::Base doc:

“By default, Rails will compile each template to a method in order to
render it. When you alter a template, Rails will check the fileâ??s
modification time and recompile it.”

I think therefore that any static HTML in the template is converted into
literal strings in this new method that is created on compilation. So if
your partial has static HTML for the tabs at all, that would definitely
be faster than executing your current code every time the helper is
called.

Also if the contents of your ‘items’ array is not dependent on any
instance variables, you could define its contents at class definition
time (although you may not be able to use #link_to and #content_tag
then), as a class variable. Then that part of the code only gets
executed once. Just make sure you don’t overwrite its elements like you
do currently.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs