{"id":17196756,"url":"https://github.com/weppos/tabs_on_rails","last_synced_at":"2025-04-12T14:57:17.877Z","repository":{"id":502267,"uuid":"129448","full_name":"weppos/tabs_on_rails","owner":"weppos","description":"Tabs on Rails is a simple Rails plugin for creating and managing tabs and navigation menus.","archived":false,"fork":false,"pushed_at":"2018-07-26T13:32:22.000Z","size":198,"stargazers_count":294,"open_issues_count":3,"forks_count":52,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-30T00:37:24.662Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://simonecarletti.com/code/tabs-on-rails","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weppos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-02-15T16:17:47.000Z","updated_at":"2024-07-31T17:25:26.000Z","dependencies_parsed_at":"2022-07-04T20:31:26.798Z","dependency_job_id":null,"html_url":"https://github.com/weppos/tabs_on_rails","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weppos%2Ftabs_on_rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weppos%2Ftabs_on_rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weppos%2Ftabs_on_rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weppos%2Ftabs_on_rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weppos","download_url":"https://codeload.github.com/weppos/tabs_on_rails/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586249,"owners_count":21128997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T01:54:29.186Z","updated_at":"2025-04-12T14:57:17.856Z","avatar_url":"https://github.com/weppos.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tabs on Rails\n\n\u003ctt\u003eTabsOnRails\u003c/tt\u003e is a simple Rails plugin for creating tabs and navigation menus. It provides helpers for generating navigation menus with a flexible interface.\n\n\n## Requirements\n\n- Rails 4.2 or Rails 5\n\nFor older versions of Ruby or Ruby on Rails, see the [CHANGELOG](CHANGELOG.md).\n\n\n## Installation\n\nAdd this line to your application's `Gemfile`:\n\n    gem \"tabs_on_rails\"\n\nAnd then execute `bundle` to install the dependencies:\n\n    $ bundle\n\nUse [Bundler](http://bundler.io/) and the `:git` option if you want to grab the latest version from the Git repository.\n\n\n## Usage\n\nIn your template use the `tabs_tag` helper to create your tab.\n\n```ruby\n\u003c%= tabs_tag do |tab| %\u003e\n  \u003c%= tab.home      'Homepage', root_path %\u003e\n  \u003c%= tab.dashboard 'Dashboard', dashboard_path %\u003e\n  \u003c%= tab.account   'Account', account_path %\u003e\n\u003c% end %\u003e\n```\n\nrenders\n\n```html\n\u003cul\u003e\n  \u003cli\u003e\u003ca href=\"/\"\u003eHomepage\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/dashboard\"\u003eDashboard\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/account\"\u003eAccount\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nThe 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.\n\n```ruby\nclass DashboardController \u003c ApplicationController\n  set_tab :dashboard\nend\n```\n\nNow, if the action belongs to `DashboardController`, the template will automatically render the following HTML code.\n\n```html\n\u003cul\u003e\n  \u003cli\u003e\u003ca href=\"/\"\u003eHomepage\u003c/a\u003e\u003c/li\u003e\n  \u003cli class=\"custom\"\u003e\u003cspan\u003eDashboard\u003c/span\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/account\"\u003eAccount\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nUse the `current_tab` helper method if you need to access the value of current tab in your controller or template.\n\n```ruby\nclass DashboardController \u003c ApplicationController\n  set_tab :dashboard\nend\n```\n\nIn your view\n\n```html\n\u003cp\u003eThe name of current tab is \u003c%= current_tab %\u003e.\u003c/p\u003e\n```\n\n### Customizing a Tab\n\nYou can pass a hash of options to customize the style and the behavior of the tab item.\nBehind the scenes, each time you create a tab, the `#tab_for` method is invoked.\n\n```ruby\n\u003c%= tabs_tag do |tab| %\u003e\n  \u003c%= tab.home      'Homepage', root_path, :style =\u003e \"padding: 10px\" %\u003e\n  \u003c%= tab.dashboard 'Dashboard', dashboard_path %\u003e\n\u003c% end %\u003e\n```\n\nrenders\n\n```\n\u003cul\u003e\n  \u003cli style=\"padding: 10px\"\u003e\u003ca href=\"/\"\u003eHomepage\u003c/a\u003e\u003c/li\u003e\n  \u003cli class=\"custom\"\u003e\u003cspan\u003eDashboard\u003c/span\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/account\"\u003eAccount\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nSee `TabsOnRails::Tabs::TabsBuilder#tab_for` for more details.\n\n### Customizing `open_tabs` and `close_tabs`\n\nThe `open_tabs` and the `close_tabs` methods can be customized with the `:open_tabs` and `:close_tabs` option.\n\n```ruby\n\u003c%= tabs_tag open_tabs: { id: 'tabs', class: 'cool' } do |tab| %\u003e\n  \u003c%= tab.home      'Homepage', root_path %\u003e\n  \u003c%= tab.dashboard 'Dashboard', dashboard_path %\u003e\n  \u003c%= tab.account   'Account', account_path %\u003e\n\u003c% end %\u003e\n```\n\nrenders\n\n```html\n\u003cul id=\"tabs\" class=\"cool\"\u003e\n  \u003cli\u003e\u003ca href=\"/\"\u003eHomepage\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/dashboard\"\u003eDashboard\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/account\"\u003eAccount\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nFurther customizations require a custom `Builder` (see below).\n\n\n## Restricting `set_tab` scope\n\nThe `set_tab` method understands all options you are used to pass to a Rails controller filter.\nIn fact, behind the scenes this method uses a `before_filter` to store the tab in the `@tab_stack` variable.\n\nTaking advantage of Rails filter options, you can restrict a tab to a selected group of actions in the same controller.\n\n```ruby\nclass PostsController \u003c ApplicationController\n  set_tab :admin\n  set_tab :posts, :only =\u003e %w(index show)\nend\n\nclass ApplicationController \u003c ActionController::Base\n  set_tab :admin, :if =\u003e :admin_controller?\n  \n  def admin_controller?\n    self.class.name =~ /^Admin(::|Controller)/\n  end\nend\n```\n\n## Using Namespaces to create Multiple Tabs\n\nNamespaces enable you to create and manage tabs in parallels. The best way to demonstrate namespace usage is with an example.\n\nLet's assume your application provides a first level navigation menu with 3 elements: `:home`, `:dashboard`, `:projects`. The relationship between your tabs and your controllers is 1:1 so you should end up with the following source code.\n\n```ruby\nclass HomeController\n  set_tab :home\nend\n\nclass DashboardController\n  set_tab :dashboard\nend\n\nclass ProjectsController\n  set_tab :projects\n  \n  def first; end\n  def second; end\n  def third; end\nend\n```\n\nThe project controller contains 3 actions and you might want to create a second-level navigation menu. This menu should reflect the navigation status of the user in the project page.\n\nWithout namespaces, you wouldn't be able to accomplish this task because you already set the current tab value to :projects. You need to create a parallel navigation menu and uniquely identify it with a custom namespace.\nLet's call it :navigation.\n\n```ruby\nclass ProjectsController\n  set_tab :projects\n\n  # Create an other tab navigation level\n  set_tab :first, :navigation, :only =\u003e %w(first)\n  set_tab :second, :navigation, :only =\u003e %w(second)\n  set_tab :third, :navigation, :only =\u003e %w(third)\n\n  def first; end\n  def second; end\n  def third; end\nend\n```\n\nVoilà! That's all you need to do. And you can create an unlimited number of namespaces as long as you use an unique name to identify them.\n\nThe default namespace is called `:default`. Passing `:default` as name is the same as don't using any namespace at all. The following lines are equivalent.\n\n```ruby\nset_tab :projects\nset_tab :projects, :default\n```\n\n### Rendering Tabs with Namespaces\n\nTo switch namespace in your template, just pass the `:namespace` option to the `tabs_tag` helper method.\n\n```ruby\n\u003c%= tabs_tag do |tab| %\u003e\n  \u003c%= tab.home      'Homepage', root_path %\u003e\n  \u003c%= tab.dashboard 'Dashboard', dashboard_path %\u003e\n  \u003c%= tab.projects  'Projects', projects_path %\u003e\n\u003c% end %\u003e\n\n\u003c%= tabs_tag :namespace =\u003e :navigation do |tab| %\u003e\n  \u003c%= tab.first   'First', project_first_path %\u003e\n  \u003c%= tab.second  'Second', project_second_path %\u003e\n  \u003c%= tab.third   'Account', project_third_path %\u003e\n\u003c% end %\u003e\n```\n\n### Namespace scope\n\nAs a bonus feature, the namespace needs to be unique within current request scope, not necessarily across the entire application.\n\nBack to the previous example, you can reuse the same namespace in the other controllers. In this way, you can reuse your templates as well.\n\n```ruby\nclass HomeController\n  set_tab :home\nend\n\nclass DashboardController\n  set_tab :dashboard\n\n  set_tab :index,  :navigation, :only =\u003e %w(index)\n  set_tab :common, :navigation, :only =\u003e %w(foo bar)\n  \n  # ...\nend\n\nclass ProjectsController\n  set_tab :projects\n\n  set_tab :first,  :navigation, :only =\u003e %w(first)\n  set_tab :second, :navigation, :only =\u003e %w(second)\n  set_tab :third,  :navigation, :only =\u003e %w(third)\n  \n  # ...\nend\n```\n\n\n## Tab Builders\n\nThe `Builder` is responsible for creating the tabs HTML code. This library is bundled with two `Builders`:\n\n- `Tabs::Builder`: this is the abstract interface for any custom builder.\n- `Tabs::TabsBuilder`: this is the default builder.\n\n### Understanding the Builder\n\nBuilders act as formatters. A Builder encapsulates all the logic behind the tab creation including the code required to toggle tabs status.\n\nWhen the `tabs_tag` helper is called, it creates a new `Tabs` instance with selected Builder. If you don't provide a custom builder, then `Tabs::TabsBuilder` is used by default.\n\n### Creating a custom Builder\n\nAll builders must extend the base `Tabs::Builder` class and implement at least the `tab_for` method.\nAdditional overridable methods include:\n\n- `open_tabs`: the method called before the tab set\n- `close_tabs`: the method called after the tab set\n- `tab_for`: the method called to create a single tab item\n\nThe following example creates a custom tab builder called `MenuTabBuilder`.\n\n```ruby\nclass MenuTabBuilder \u003c TabsOnRails::Tabs::Builder\n  def open_tabs(options = {})\n    @context.tag(\"ul\", options, open = true)\n  end\n\n  def close_tabs(options = {})\n    \"\u003c/ul\u003e\".html_safe\n  end\n\n  def tab_for(tab, name, options, item_options = {})\n    item_options[:class] = (current_tab?(tab) ? 'active' : '')\n    @context.content_tag(:li, item_options) do\n      @context.link_to(name, options)\n    end\n  end\nend\n```\n\n### Using a custom Builder\n\nIn your view, simply pass the builder class to the `tabs_tag` method.\n\n```ruby\n\u003c%= tabs_tag(:builder =\u003e MenuTabBuilder) do |tab| %\u003e\n  \u003c%= tab.home        'Homepage', root_path %\u003e\n  \u003c%= tab.dashboard,  'Dashboard', dashboard_path %\u003e\n  \u003c%= tab.account     'Account', account_path, style: 'float: right;' %\u003e\n\u003c% end %\u003e\n```\n\nrenders\n\n```html\n\u003cul\u003e\n  \u003cli class=\"\"\u003e\u003ca href=\"/\"\u003eHomepage\u003c/a\u003e\u003c/li\u003e\n  \u003cli class=\"active\"\u003e\u003ca href=\"/dashboard\"\u003eDashboard\u003c/a\u003e\u003c/li\u003e\n  \u003cli class=\"\" style=\"float: right;\"\u003e\u003ca href=\"/account\"\u003eAccount\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\n\n## Credits\n\n\u003ctt\u003eTabsOnRails\u003c/tt\u003e was created and is maintained by [Simone Carletti](https://simonecarletti.com/). Many improvements and bugfixes were contributed by the [open source community](https://github.com/weppos/tabs_on_rails/graphs/contributors).\n\n\n## Contributing\n\nDirect questions and discussions to [Stack Overflow](http://stackoverflow.com/questions/tagged/tabs-on-rails).\n\n[Pull requests](https://github.com/weppos/tabs_on_rails) are very welcome! Please include tests for every patch, and create a topic branch for every separate change you make.\n\nReport issues or feature requests to [GitHub Issues](https://github.com/weppos/tabs_on_rails/issues).\n\n\n## More Information\n\n- [Homepage](https://simonecarletti.com/code/tabs-on-rails)\n- [RubyGems](https://rubygems.org/gems/tabs_on_rails)\n- [Issues](https://github.com/weppos/tabs_on_rails/issues)\n\n\n## License\n\nCopyright (c) 2009-2017 Simone Carletti. This is Free Software distributed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweppos%2Ftabs_on_rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweppos%2Ftabs_on_rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweppos%2Ftabs_on_rails/lists"}