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
8
9
10
11
12
13
# File 'lib/bootstrap_builders/button_drop_down.rb', line 4

def initialize(*args)
  args_parser = BootstrapBuilders::ArgumentsParser.new(
    arguments: args,
    short_true_arguments: [:block, :danger, :link, :info, :primary, :sm, :warning, :xs]
  )

  args_parser.arguments_hash[:label] ||= args.shift if args.first.is_a?(String)
  @args = args_parser.arguments_hash
  @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



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
66
67
68
69
70
# File 'lib/bootstrap_builders/button_drop_down.rb', line 30

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: classes.classes,
    data: {
      toggle: "dropdown"
    }
  )
  main_button.add_ele(:i, classes: ["fa", "fa-fw", "fa-#{@args.fetch(:icon)}"]) if @args[:icon].present?
  main_button.add_str(@args.fetch(:label)) if @args[:label].present?
  main_button.add_ele(:span, classes: ["caret"]) if !@args.key?(:caret) || @args[:cart]

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

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

    link_args = {class: BootstrapBuilders::ClassAttributeHandler.short(button[:class])}
    link_args.deep_merge!(data: {confirm: I18n.t("are_you_sure")}) if button[:confirm]
    link_args.deep_merge!(data: {method: button[:method]}) if button[:method].present?
    link_args.deep_merge!(data: button[:data]) if button[:data]

    link = view_context.link_to(button.fetch(:url), link_args) do
      if button[:icon]
        view_context.safe_join [view_context.(:i, nil, class: ["fa", "fa-fw", "fa-#{button.fetch(:icon)}"]), " ", button.fetch(:label)]
      else
        button.fetch(:label)
      end
    end

    li.add_html(link)
  end

  btn_group.html
end

#option(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bootstrap_builders/button_drop_down.rb', line 15

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

  args = args_parser.arguments

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

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

  @buttons << args_parser.arguments_hash
end