#-- # Tabs on Rails # # A simple Ruby on Rails plugin for creating and managing Tabs. # # Copyright (c) 2009-2012 Simone Carletti #++ module TabsOnRails module ActionController extend ActiveSupport::Concern included do extend ClassMethods helper HelperMethods helper_method :current_tab, :current_tab? end protected # Sets the value for current tab to given name. # If you need to manage multiple tabs, # then you can pass an optional namespace. # # Examples # # set_tab :homepage # set_tab :dashboard, :menu # # Returns nothing. def set_tab(name, namespace = nil) tab_stack[namespace || :default] = name end # Returns the value for current tab in the default namespace, # or nil if no tab has been set before. # You can pass namespace to get the value # of the current tab for a different namespace. # # Examples # # current_tab # => nil # current_tab :menu # => nil # # set_tab :homepage # set_tab :dashboard, :menu # # current_tab # => :homepage # current_tab :menu # => :dashboard # # Returns the String/Symbol current tab. def current_tab(namespace = nil) tab_stack[namespace || :default] end # Checks if the current tab in namespace # matches name. # # Returns a Boolean. def current_tab?(name, namespace = nil) current_tab(namespace).to_s == name.to_s end # Initializes and/or returns the tab stack. # You won't probably need to use this method directly # unless you are trying to hack the plugin architecture. # # Returns the Hash stack. def tab_stack @tab_stack ||= {} end module ClassMethods # Sets the value for current tab to given name. # # set_tab :foo # # If you need to manage multiple tabs, then you can pass an optional namespace. # # set_tab :foo, :namespace # # The set_tab method understands all options you are used to pass to a Rails controller filter. # In fact, behind the scenes this method uses a before_filter # to store the tab in the @tab_stack variable. # For example, you can set the tab only for a restricted group of actions in the same controller # using the :only and :except options. # # Examples # # set_tab :foo # set_tab :foo, :except => :new # set_tab :foo, :only => [ :index, :show ] # # set_tab :foo, :namespace # set_tab :foo, :namespace, :only => [ :index, :show ] # def set_tab(*args) options = args.extract_options! name, namespace = args before_filter(options) do |controller| controller.send(:set_tab, name, namespace) end end end module HelperMethods # In your template use the tabs_tag helper to create your tab. # # <%= tabs_tag do |tab| %> # <%= tab.home 'Homepage', root_path %> # <%= tab.dashboard 'Dashboard', dashboard_path %> # <%= tab.account 'Account', account_path %> # <% end %> # # The example above produces the following HTML output. # # # # The usage is similar to the Rails route file. # You create named tabs with the syntax tab.name_of_tab. # # The name you use creating a tab is the same you're going to refer to in your controller # when you want to mark a tab as the current tab. # # class DashboardController < ApplicationController # set_tab :dashboard # end # # Now, if the action belongs to DashboardController, # the template will automatically render the following HTML code. # # # # Use the current_tab helper method if you need to access # the value of current tab in your controller or template. # # class DashboardController < ApplicationController # set_tab :dashboard # end # # # In your view #

The name of current tab is <%= current_tab %>.

# # === Customizing a Tab # # You can pass a hash of options to customize the style and the behavior of the tab item. # Behind the scenes, each time you create a tab, the #tab_for # method is invoked. # # <%= tabs_tag do |tab| %> # <%= tab.home 'Homepage', root_path, :style => "padding: 10px" %> # <%= tab.dashboard 'Dashboard', dashboard_path %> # <%= tab.account 'Account', account_path, :class => "custom" %> # <% end %> # # # # You can pass any option supported by the
  • content_tag
  • Rails helper. # Additionally, the following options have a special meaning: # # * link_current: forces the current tab to be a link, instead of a span tag # # See TabsOnRails::Tabs::TabsBuilder#tab_for for more details. # # === Customizing open_tabs and close_tabs # # The open_tabs and the close_tabs methods can be customized # with the :open_tabs and :close_tabs option. # # <%= tabs_tag :open_tabs => { :id => "tabs", :class => "cool" } do |tab| %> # <%= tab.home 'Homepage', root_path %> # <%= tab.dashboard 'Dashboard', dashboard_path %> # <%= tab.account 'Account', account_path %> # <% end %> # # # # Further customizations require a custom Builder. # def tabs_tag(options = {}, &block) Tabs.new(self, { :namespace => :default }.merge(options)).render(&block) end end end end