Class: TabsOnRails::Tabs::TabsBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/tabs_on_rails/tabs/tabs_builder.rb

Overview

Tabs Builder

The TabsBuilder is and example of custom Builder.

Instance Method Summary collapse

Methods inherited from Builder

#current_tab?, #initialize

Constructor Details

This class inherits a constructor from TabsOnRails::Tabs::Builder

Instance Method Details

#close_tabs(options = {}) ⇒ Object

Returns an unordered list close tag.

The options hash is ignored here. It exists only for coherence with the parent Builder API.

close_tag
# => "</ul>"

close_tag :class => "centered"
# => "</ul>"

Implements Builder#close_tabs.



77
78
79
# File 'lib/tabs_on_rails/tabs/tabs_builder.rb', line 77

def close_tabs(options = {})
  "</ul>".html_safe
end

#open_tabs(options = {}) ⇒ Object

Returns an unordered list open tag.

The options hash is used to customize the HTML attributes of the tag.

open_tag
# => "<ul>"

open_tag :class => "centered"
# => "<ul class=\"centered\">"

Implements Builder#open_tabs.



60
61
62
# File 'lib/tabs_on_rails/tabs/tabs_builder.rb', line 60

def open_tabs(options = {})
  @context.tag("ul", options, open = true)
end

#tab_for(tab, name, url_options, item_options = {}) ⇒ Object

Returns a link_to tab with name and options if tab is not the current tab, a simple tab name wrapped by a span tag otherwise.

current_tab? :foo   # => true

tab_for :foo, 'Foo', foo_path
# => "<li class="current"><span>Foo</span></li>"

tab_for :bar, 'Bar', bar_path
# => "<li><a href="/link/to/bar">Bar</a></li>"

You can pass a hash of item_options to customize the behavior and the style of the li element.

# Pass a custom class to the element
tab_for :bar, 'Bar', bar_path, :class => "custom"
# => "<li class="custom"><a href="/link/to/bar">Bar</a></li>"

Implements Builder#tab_for.



40
41
42
43
44
45
46
# File 'lib/tabs_on_rails/tabs/tabs_builder.rb', line 40

def tab_for(tab, name, url_options, item_options = {})
   item_options[:class] = item_options[:class].to_s.split(" ").push(@options[:active_class] || "current").join(" ") if current_tab?(tab)
   content = @context.link_to_unless(current_tab?(tab), name, url_options) do
     @context.(:span, name)
   end
   @context.(:li, content, item_options)
end