Module: Bootstrap::ButtonHelper

Defined in:
app/helpers/bootstrap/button_helper.rb

Overview

Examples:

Button / Link


# button
button('Default')

# add url option to make <a> styled as button
button('Home', url: '/')

# a button styled as a link
button('Home', :link)

# give it a type; see BUTTON_TYPES
button('Info', :info)

# give it a size (see BUTTON_SIZES)
button('Small', :small)

# size, type, additional class and additional html attributes
button('Warning', :warning, :large, id: 'warn-id', class: 'more-class', my_key: 'my_value')

Button Group

<%= button_group do %>
  <%= button("Left", url: "/left") %>
  <%= button("Right", id: 'right') %>
<% end %>

Button Toolbar

<%= button_toolbar do %>
  <%= button('Single Button', url: '/single') %>
  <%= button_group do %>
    <%= button('Group Button 1') %>
    <%= button('Group Button 2') %>
  <% end %>
  <%= button('Another Single') %>

Constant Summary collapse

InvalidButtonModifierError =
Class.new(StandardError)
BUTTON_TYPES =
%w(default primary info success warning danger inverse link)
BUTTON_SIZES =
%w(default large small mini)
BUTTON_OTHERS =
%w(
BUTTON_ALL =
BUTTON_TYPES + BUTTON_SIZES + BUTTON_OTHERS

Instance Method Summary collapse

Instance Method Details

#button(text, options = {}) ⇒ String #button(text, type, options = {}) ⇒ String #button(text, size, options = {}) ⇒ String #button(text, type, size, options = {}) ⇒ String

Returns <button> or <a> styled as Bootstrap button

Overloads:

  • #button(text, options = {}) ⇒ String

    Default button

    Parameters:

    • text (String)

      text of button

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

      All keys except :url become html attributes for the generated tag

    Options Hash (options):

    • :url (String)

      if present, return a <a> styled as a button

  • #button(text, type, options = {}) ⇒ String

    Button of type type

    Parameters:

    • text (String)

      text of button

    • type (String, Symbol)

      type of button; see BUTTON_TYPES

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

      All keys except :url become html attributes for the generated tag

    Options Hash (options):

    • :url (String)

      if present, return a <a> styled as a button

  • #button(text, size, options = {}) ⇒ String

    Button of size size

    Parameters:

    • text (String)

      text of button

    • size (String, Symbol)

      size of button; see BUTTON_SIZES

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

      All keys except :url become html attributes for the generated tag

    Options Hash (options):

    • :url (String)

      if present, return a <a> styled as a button

  • #button(text, type, size, options = {}) ⇒ String

    Button of type type, size size

    Parameters:

    • text (String)

      text of button

    • type (String, Symbol)

      type of button; see BUTTON_TYPES

    • size (String, Symbol)

      size of button; see BUTTON_SIZES

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

      All keys except :url become html attributes for the generated tag

    Options Hash (options):

    • :url (String)

      if present, return a <a> styled as a button

Returns:

  • (String)

    Html for a <button> (or <a> if url option passed in)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/bootstrap/button_helper.rb', line 78

def button(*args)
  options = canonicalize_options(args.extract_options!)
  text = args.shift
  href = options.delete(:url)
  options = add_button_classes(options, args)

  text = yield if block_given?
  
  if href.present?
    link_to(text, href, options)
  else
    (:button, text, options)
  end
end

#button_group(options = {}) { ... } ⇒ String

Returns a Bootstrap button group

Parameters:

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

    will be come html attributes of generated <div>

Yields:

  • block usually consists of calls to #button

Yield Returns:

  • (String)

    html for contents of group

Returns:

  • (String)

    html for button group



116
117
118
119
120
121
122
123
# File 'app/helpers/bootstrap/button_helper.rb', line 116

def button_group(options={})
  options = canonicalize_options(options)
  options = ensure_class(options, 'btn-group')
  
  (:div, options) do
    yield
  end
end

#button_toolbar(options = {}) { ... } ⇒ String

Returns a Bootstrap button toolbar

Parameters:

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

    will be come html attributes of generated <div>

Yields:

  • block usually consists of calls to #button or #button_group

Yield Returns:

  • (String)

    html for contents of toolbar

Returns:

  • (String)

    html for button toolbar



100
101
102
103
104
105
106
107
# File 'app/helpers/bootstrap/button_helper.rb', line 100

def button_toolbar(options={})
  options = canonicalize_options(options)
  options = ensure_class(options, 'btn-toolbar')
  
  (:div, options) do
    yield
  end
end

#validate_button_types_and_sizes(types_and_sizes) ⇒ Object

Ensures each entry in types_and_sizes is a valid button modifier.

Raises:

  • InvalidButtonModifierError if one of the args isn’t a valid button modifier.



129
130
131
# File 'app/helpers/bootstrap/button_helper.rb', line 129

def validate_button_types_and_sizes(types_and_sizes)
  types_and_sizes.each { |e| raise(InvalidButtonModifierError, e.inspect) unless BUTTON_ALL.include?(e.to_s) }
end