Class: BootstrapBuilders::Button

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Button

Returns a new instance of Button.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bootstrap_builders/button.rb', line 22

def initialize(args)
  @args = args
  @label = args[:label]
  @class = args[:class]
  @url = args.fetch(:url)
  @args = args
  @context = args.fetch(:context)
  @icon = args[:icon]
  @can = args[:can]
  @mini = args[:mini]

  @data = args[:data] || {}
  @data[:bb_icon] = @icon.present?

  @args[:title] ||= @label if @args[:responsive] && @label.present?
end

Instance Attribute Details

#labelObject

Returns the value of attribute label.



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

def label
  @label
end

Class Method Details

.parse_url_args(args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bootstrap_builders/button.rb', line 4

def self.parse_url_args(args)
  args_parser = BootstrapBuilders::ArgumentsParser.new(
    arguments: args,
    short_true_arguments: [
      :block, :confirm, :danger, :disabled, :info, :link, :primary, :remote, :responsive, :warning, :lg, :md, :mini, :sm, :xs
    ]
  )
  args = args_parser.arguments

  is_an_active_record = BootstrapBuilders::IsAChecker.is_a?(args.first, "ActiveRecord::Base")
  is_a_baza_model = BootstrapBuilders::IsAChecker.is_a?(args.first, "BazaModels::Model")

  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)
  args_parser.arguments_hash
end

Instance Method Details

#can_modelObject



90
91
92
93
# File 'lib/bootstrap_builders/button.rb', line 90

def can_model
  can_object unless @can_model
  @can_model
end

#can_model_classObject



95
96
97
98
# File 'lib/bootstrap_builders/button.rb', line 95

def can_model_class
  can_object unless @can_model_class
  @can_model_class
end

#classesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bootstrap_builders/button.rb', line 39

def classes
  unless @classes
    @classes = BootstrapBuilders::ClassAttributeHandler.new(class: ["btn"])
    handle_mini_argument
    @classes.add(@class) if @class
    @classes.add("bb-btn-responsive") if @args[:responsive]
    add_default_as_default
    @classes.add("btn-block") if @args[:block]
    @classes.add("btn-danger") if @args[:danger]
    @classes.add("btn-info") if @args[:info]
    @classes.add("btn-link") if @args[:link]
    @classes.add("btn-primary") if @args[:primary]
    @classes.add("btn-warning") if @args[:warning]

    size_classes = [:lg, :md, :sm, :xs]
    size_classes.each do |size_class|
      next unless @args[size_class]
      btn_size_class = "btn-#{size_class}"
      @classes.add(btn_size_class) unless @classes.include?(btn_size_class)
    end
  end

  @classes
end

#htmlObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bootstrap_builders/button.rb', line 64

def html
  return unless can?

  handle_confirm_argument

  link_args = {
    class: classes.classes,
    data: @data,
    method: @args[:method],
    target: @args[:target],
    remote: @args[:remote],
    title: @args[:title]
  }

  link_args[:disabled] = true if @args[:disabled]

  @context.link_to(@url, link_args) do
    html = ""
    html << @context.(:i, nil, class: ["fa", "fa-#{@icon}"]) if @icon

    html << @context.(:span, " #{@label}", class: "bb-btn-label") if @label && !@mini

    html.strip.html_safe # rubocop:disable Rails/OutputSafety
  end
end