Simpler Tabs
Join the DZone community and get the full member experience.
Join For FreeJust one of many ways to help you add tabs to your layout.
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| %|#{text}| },
:inactive => Proc.new {|text, url| %|#{link_to text, url}| }
)
t[:index] = 'Manage Products'
t[:front_page] = 'Manage Front Page'
'' +
'' +
t[:index, :front_page].join +
'' +
'' +
''
end
end
Opinions expressed by DZone contributors are their own.
Comments