Module: DropdownHelper

Defined in:
app/helpers/dropdown_helper.rb

Instance Method Summary collapse

Instance Method Details



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/dropdown_helper.rb', line 2

def dropdown(label:, title: nil)
  menu_options = { class: 'dropdown-menu' }

  if title.present?
    title_id = id_for(title)
    title_tag = (:h2, class: 'sr-only', id: title_id) { title }
    menu_options[:aria] = { labelledby: title_id }
  else
    title_tag = ''
  end

  toggle_options = {
    aria: { expanded: false },
    class: 'dropdown-toggle',
    data: { toggle: "dropdown" },
    type: 'button'
  }
  toggle = (:button, toggle_options) { label }

  menu = (:ul, menu_options) { block_given? ? yield : nil }
  title_tag + (:div, class: 'dropdown') { toggle + menu }
end


32
33
34
35
36
37
38
# File 'app/helpers/dropdown_helper.rb', line 32

def dropdown_for(items = [], label: nil, title: nil, &block)
  item_tags = items.map { |item|
    dropdown_option(item, &block)
  }.join.html_safe

  dropdown(label: label, title: title) { item_tags }
end


25
26
27
28
29
30
# File 'app/helpers/dropdown_helper.rb', line 25

def dropdown_option(option)
  menu_item_options = {
    class: 'dropdown-menu-item'
  }
  (:li, menu_item_options) { block_given? ? yield(option) : option }
end