Posted By

mcmire on 10/03/08


Tagged

helper tabs


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

bcalloway
nhutto
webstic
davidsi


Simpler tabs


 / Published in: Rails
 

Just one of many ways to help you add tabs to your layout.

  1. class TabHelper
  2. attr_reader :html, :tabs
  3.  
  4. def initialize(template, states)
  5. @template = template
  6. @states = states
  7. @html = []
  8. @tabs = {}
  9. end
  10.  
  11. def add(action, text)
  12. url = { :action => action }
  13. html =
  14. if @template.request.path.sub(/\?.*/, '') == @template.url_for(url)
  15. @states[:active].call(text, url)
  16. else
  17. @states[:inactive].call(text, url)
  18. end
  19. @tabs[action] = html
  20. @html << html
  21. end
  22. alias_method :[]=, :add
  23.  
  24. def [](*args)
  25. @tabs.values_at(*args)
  26. end
  27. end
  28.  
  29. #== Example ==
  30.  
  31. module ProductsHelper
  32. def subnav_links
  33. t = TabHelper.new(self,
  34. :active => Proc.new {|text, url| %|<div class="current tab">#{text}</div>| },
  35. :inactive => Proc.new {|text, url| %|<div class="tab">#{link_to text, url}</div>| }
  36. )
  37. t[:index] = 'Manage Products'
  38. t[:front_page] = 'Manage Front Page'
  39.  
  40. '<div id="tabs">' +
  41. '<div style="float: left">' +
  42. t[:index, :front_page].join +
  43. '</div>' +
  44. '<div class="clear"></div>' +
  45. '</div>'
  46. end
  47. end

Report this snippet  

You need to login to post a comment.