Class: BootstrapBuilders::ButtonDropDown

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrap_builders/button_drop_down.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ButtonDropDown

Returns a new instance of ButtonDropDown.



4
5
6
7
# File 'lib/bootstrap_builders/button_drop_down.rb', line 4

def initialize(args)
  @args = args
  @buttons = []
end

Instance Attribute Details

#view_contextObject

Returns the value of attribute view_context.



2
3
4
# File 'lib/bootstrap_builders/button_drop_down.rb', line 2

def view_context
  @view_context
end

Instance Method Details

#htmlObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bootstrap_builders/button_drop_down.rb', line 26

def html
  btn_group = HtmlGen::Element.new(:div, classes: ["btn-group"])
  main_button = btn_group.add_ele(
    :button,
    attr: {
      "aria-haspopup" => true,
      "aria-exapended" => false,
      type: "button"
    },
    classes: ["btn", "btn-default", "dropdown-toggle"],
    data: {
      toggle: "dropdown"
    }
  )
  main_button.add_str(@args.fetch(:label))
  main_button.add_ele(:span, classes: ["caret"])

  ul = btn_group.add_ele(:ul, classes: ["dropdown-menu"])

  @buttons.each do |button|
    li = ul.add_ele(:li)

    url = button.fetch(:url)
    url = view_context.polymorphic_url(url) if url.is_a?(Array)

    a_href = li.add_ele(:a, attr: {href: url}, classes: BootstrapBuilders::ClassAttributeHandler.short(button[:class]))

    a_href.data[:confirm] = I18n.t("are_you_sure") if button[:confirm]
    a_href.data[:method] = button[:method] if button[:method].present?
    a_href.data.merge!(button[:data]) if button[:data]

    if button[:icon]
      a_href.add_ele(:i, classes: ["fa", "fa-fw", "fa-#{button.fetch(:icon)}"])
    end

    a_href.add_str(button.fetch(:label))
  end

  btn_group.html
end

#option(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bootstrap_builders/button_drop_down.rb', line 9

def option(*args)
  args_parser = BootstrapBuilders::ArgumentsParser.new(
    arguments: args,
    short_true_arguments: [:confirm]
  )

  args = args_parser.arguments

  if args.first.is_a?(Array) || args.first.is_a?(String) || is_an_active_record || is_a_baza_model
    args_parser.arguments_hash[:url] ||= args.shift
  end

  args_parser.arguments_hash[:label] ||= args.shift if args.first.is_a?(String)

  @buttons << args_parser.arguments_hash
end