Class: Bootstrap3Helper::Tabs::Dropdown

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap3_helper/tabs/dropdown.rb

Overview

Used to rapidly build dropdown menus for Bootstrap tabs.

Examples:

Rendering a Dropdown menu component:

<code>
  <%= menu.dropdown 'Testing Dropdown' do |dropdown| %>
      <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
      <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
      <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
  <% end %>
</code>

Instance Method Summary collapse

Methods inherited from Component

#concat, #content_tag, #parse_arguments, #uuid

Constructor Details

#initialize(template, name = '', &block) ⇒ Dropdown

Creates a new Tabs::Dropdown object.

Parameters:

  • template (ActionView)

    Template in which your are binding too.

  • name (String) (defaults to: '')


20
21
22
23
24
25
# File 'lib/bootstrap3_helper/tabs/dropdown.rb', line 20

def initialize(template, name = '', &block)
  super(template)

  @name    = name
  @content = block || proc { '' } 
end

Instance Method Details

#item(name, args = {}) ⇒ Object

Note:

You can opt out of passing in a block and the li will use the name attribute for the menu item.

Adds a new item to the dropdown object.

Parameters:

  • name (String|Symbol)
    • Used to link nav li to tab-content

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :id (String)

    The ID, if you want one, for the li element.

  • :class (String)

    Custom class for the li element.

  • :data (Hash)

    Any data attributes you want on the li element.

Yield Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bootstrap3_helper/tabs/dropdown.rb', line 41

def item(name, args = {})
  id     = args.fetch(:id, nil)
  klass  = args.fetch(:class, '')
  data   = args.fetch(:data, nil)
  active = klass.include? 'active'

  (
    :li,
    id:    id,
    class: klass,
    data:  data
  ) do
    (
      :a,
      href: "##{name}",
      role: 'tab',
      data: { toggle: 'tab' },
      aria: { controls: name, expanded: active }
    ) do
      block_given? ? yield : name.to_s.titleize
    end
  end
end

#to_sString

Used to render out the object and get the HTML representation.

Returns:

  • (String)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bootstrap3_helper/tabs/dropdown.rb', line 70

def to_s
  content = (
    :a,
    @name,
    href:  '#',
    class: 'dropdown-toggle',
    data: { toggle: 'dropdown' }, aria: { expanded: false }
  )

  content +=  :ul, id: @id, class: 'dropdown-menu ' do
    @content.call(self)
  end
end