Module: Bh::DropdownHelper

Includes:
BaseHelper
Defined in:
lib/bh/helpers/dropdown_helper.rb

Overview

Provides methods to include dropdowns.

Instance Method Summary collapse

Instance Method Details

Returns an HTML block tag that follows the Bootstrap documentation on how to display dropdowns.

The skeleton of the dropdown is an unordered list; its content is passed as block to the list of dropdown items. Since the most common use for a dropdown is to display a menu of links, a variable is set inside the block so that every call to +link_to+ generates a link surrounded by a list item and with the appropriate menu item attributes.

Examples:

A right-aligned dropdown with two links.

<%= dropdown 'Menu', align: :right do %>
  <%= link_to 'Home', '/' %>
  <%= link_to 'Profile', '/profile' %>
<% end %>

Parameters:

  • caption (#to_s)

    the caption for the dropdown button.

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

    the display options for the dropdown.

Options Hash (options):

  • :groupable (Boolean) — default: true

    if true, uses the "btn-group" class rather than then "dropdown" class, so that multiple dropdown buttons can be aligned in the same row (as a group of buttons).

  • :split (Boolean) — default: false

    if true, creates a split button that only toggles the dropdown when clicked on the rightmost part.

  • :direction (#to_s)

    if set to :up, the dropdown appears above the button, rather than below.

  • :align (#to_s)

    if set to :right, the dropdown is aligned to the right-end of the button, rather than to the left-end.

  • :context (#to_s) — default: :default

    the context for the button, which determines its color.

  • :size (#to_s)

    the size of the button.

Yields:

  • block the content of the dropdown

Returns:

  • (String)

    an HTML block tag for a dropdown.

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bh/helpers/dropdown_helper.rb', line 43

def dropdown(caption, options = {}, &block)
  options ||= {}
  options[:id] ||= "dropdown-#{rand 10**10}"
  options[:caption] = caption
  options[:div_class] = dropdown_div_class options
  options[:button_class] = dropdown_button_class options
  options[:list_class] = dropdown_list_class options
  layout = options[:split] ? 'bh/dropdown_split' : 'bh/dropdown'
  @dropdown_link = true
  dropdown = render layout: layout, locals: options, &block
  dropdown.tap{ @dropdown_link = false }
end