Sha256: d6a96b6fb2a6cf09509688df549b0aec8188eb3ca196e1c538adfd27270a0c1e

Contents?: true

Size: 1.25 KB

Versions: 43

Compression:

Stored size: 1.25 KB

Contents

module Releaf
  module ButtonHelper

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


    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


    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

  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
releaf-core-3.0.3 app/helpers/releaf/button_helper.rb
releaf-core-3.0.2 app/helpers/releaf/button_helper.rb
releaf-core-3.0.1 app/helpers/releaf/button_helper.rb
releaf-core-3.0.0 app/helpers/releaf/button_helper.rb
releaf-core-2.2.1 app/helpers/releaf/button_helper.rb
releaf-core-2.2.0 app/helpers/releaf/button_helper.rb
releaf-core-2.1.2 app/helpers/releaf/button_helper.rb
releaf-core-2.1.1 app/helpers/releaf/button_helper.rb
releaf-core-2.1.0 app/helpers/releaf/button_helper.rb
releaf-core-2.0.1 app/helpers/releaf/button_helper.rb
releaf-core-2.0.0 app/helpers/releaf/button_helper.rb
releaf-core-1.1.22 app/helpers/releaf/button_helper.rb
releaf-core-1.1.21 app/helpers/releaf/button_helper.rb
releaf-core-1.1.20 app/helpers/releaf/button_helper.rb
releaf-core-1.1.19 app/helpers/releaf/button_helper.rb
releaf-core-1.1.18 app/helpers/releaf/button_helper.rb
releaf-core-1.1.17 app/helpers/releaf/button_helper.rb
releaf-core-1.1.16 app/helpers/releaf/button_helper.rb
releaf-core-1.1.15 app/helpers/releaf/button_helper.rb
releaf-core-1.1.14 app/helpers/releaf/button_helper.rb