Sha256: 08d599ae8c1d48393ba76947b748c14fc014ea3613b708a7337de3e72a976023

Contents?: true

Size: 1.09 KB

Versions: 24

Compression:

Stored size: 1.09 KB

Contents

require "virtus"

class DropdownButtonItem < ActionView::Base
  include ActionView::Helpers::TagHelper
  include Virtus.model(mass_assignment: false)

  attribute :title, String
  attribute :url, String
  attribute :icon, String
  attribute :url_options, Hash
  attribute :enabled, Boolean

  def initialize(**options)
    @title = options.delete(:title)
    @url = options.delete(:url)
    @icon = options.delete(:icon)
    @url_options = options
    @enabled = options.delete(:enabled) { true }
  end

  def to_html
    enabled? ? enabled_html : disabled_html
  end

  private

  def enabled_html
    content_tag(:li) do
      capture do
        link_to(url, url_options) do
          concat(icon_html)
          concat(title) if title.present?
        end
      end
    end
  end

  def disabled_html
    content_tag(:li, class: "disabled") do
      concat(icon_html)
      concat(title) if title.present?
    end
  end

  # rubocop:disable Rails/OutputSafety
  def icon_html
    return if icon.blank?
    "<i class='fa fa-link-annotation #{icon}'></i>".html_safe
  end
  # rubocop:enable Rails/OutputSafety
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
renalware-core-2.0.16 lib/dropdown_button_item.rb
renalware-core-2.0.15 lib/dropdown_button_item.rb
renalware-core-2.0.14 lib/dropdown_button_item.rb
renalware-core-2.0.13 lib/dropdown_button_item.rb
renalware-core-2.0.12 lib/dropdown_button_item.rb
renalware-core-2.0.11 lib/dropdown_button_item.rb
renalware-core-2.0.9 lib/dropdown_button_item.rb
renalware-core-2.0.8 lib/dropdown_button_item.rb
renalware-core-2.0.7 lib/dropdown_button_item.rb
renalware-core-2.0.5 lib/dropdown_button_item.rb
renalware-core-2.0.4 lib/dropdown_button_item.rb
renalware-core-2.0.3 lib/dropdown_button_item.rb
renalware-core-2.0.2 lib/dropdown_button_item.rb
renalware-core-2.0.1 lib/dropdown_button_item.rb
renalware-core-2.0.0 lib/dropdown_button_item.rb
renalware-core-2.0.0.pre.rc13 lib/dropdown_button_item.rb
renalware-core-2.0.0.pre.rc11 lib/dropdown_button_item.rb
renalware-core-2.0.0.pre.rc10 lib/dropdown_button_item.rb
renalware-core-2.0.0.pre.rc9 lib/dropdown_button_item.rb
renalware-core-2.0.0.pre.rc8 lib/dropdown_button_item.rb