DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Simpler Tabs

Snippets Manager user avatar by
Snippets Manager
·
Sep. 19, 08 · · Code Snippet
Like (0)
Save
Tweet
490 Views

Join the DZone community and get the full member experience.

Join For Free
Just 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.

Popular on DZone

  • Modern REST API Design Principles and Rules
  • Memory Debugging and Watch Annotations
  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • How to Leverage Method Chaining To Add Smart Message Routing in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo