Module: Releaf::ButtonHelper

Defined in:
app/helpers/releaf/button_helper.rb

Instance Method Summary collapse

Instance Method Details

#releaf_button(text, icon, attributes = {}) ⇒ Object


4
5
6
7
8
9
10
# File 'app/helpers/releaf/button_helper.rb', line 4

def releaf_button(text, icon, attributes = {})
  attributes = releaf_button_attributes( text, icon, attributes )
  tag = attributes.key?(:href) ? :a : :button
  (tag, attributes) do
    releaf_button_content( text, icon, attributes )
  end
end

#releaf_button_attributes(text, icon, attributes = {}) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/releaf/button_helper.rb', line 13

def releaf_button_attributes( text, icon, attributes = {} )
  default_attributes = {
    class: ["button"],
    title: text
  }

  unless attributes.key? :href
    default_attributes[:type] = :button
    default_attributes[:autocomplete] = "off"
  end

  if icon.present?
    icon_class = (text.present?) ? "with-icon" : "only-icon"
    default_attributes[:class] << icon_class
  end

  merge_attributes(default_attributes, attributes)
end

#releaf_button_content(text, icon, attributes = {}) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/releaf/button_helper.rb', line 33

def releaf_button_content( text, icon, attributes = {} )
  if text.blank? && icon.present?
    raise ArgumentError, "Title is required for icon-only buttons" if attributes[:title].blank?
  end

  html = "".html_safe
  html << fa_icon(icon) if icon.present?
  html << text if text.present?

  if html.length < 1
    raise ArgumentError, "Either text or icon is required for buttons"
  end

  html
end