you are here: codestackercodes [RSS] → tag tab [RSS]

simple tabs helper

class TabHelper
  attr_reader :html, :tabs

  def initialize(template, states)
    @template = template
    @states = states
    @html = []
    @tabs = {}
  end

  def add(action, text)
    url = { :action => action }
    html = 
      if @template.request.path.sub(/\?.*/, '') == @template.url_for(url)
        @states[:active].call(text, url)
      else
        @states[:inactive].call(text, url)
      end
    @tabs[action] = html
    @html << html
  end
  alias_method :[]=, :add
  
  def [](*args)
    @tabs.values_at(*args)
  end
end

# example


module ProductsHelper
  def subnav_links
    t = TabHelper.new(self,
      :active   => Proc.new {|text, url| %|<div class="current tab">#{text}</div>| },
      :inactive => Proc.new {|text, url| %|<div class="tab">#{link_to text, url}</div>| }
    )
    t[:index] = 'Manage Products'
    t[:front_page] = 'Manage Front Page'
    
    '<div id="tabs">' +
      '<div style="float: left">' +
        t[:index, :front_page].join +
      '</div>' +
      '<div class="clear"></div>' +
    '</div>'
  end
end
Displaying 1 Code