Sha256: d3c19b9e49c08d18e691080c97ab6563302a2758b8e0294ea1dd85224c3d7134

Contents?: true

Size: 1.12 KB

Versions: 114

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

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

114 entries across 114 versions & 1 rubygems

Version Path
renalware-core-2.0.127 lib/dropdown_button_item.rb
renalware-core-2.0.126 lib/dropdown_button_item.rb
renalware-core-2.0.125 lib/dropdown_button_item.rb
renalware-core-2.0.124 lib/dropdown_button_item.rb
renalware-core-2.0.123 lib/dropdown_button_item.rb
renalware-core-2.0.121 lib/dropdown_button_item.rb
renalware-core-2.0.120 lib/dropdown_button_item.rb
renalware-core-2.0.119 lib/dropdown_button_item.rb
renalware-core-2.0.118 lib/dropdown_button_item.rb
renalware-core-2.0.117 lib/dropdown_button_item.rb
renalware-core-2.0.116 lib/dropdown_button_item.rb
renalware-core-2.0.115 lib/dropdown_button_item.rb
renalware-core-2.0.113 lib/dropdown_button_item.rb
renalware-core-2.0.112 lib/dropdown_button_item.rb
renalware-core-2.0.111 lib/dropdown_button_item.rb
renalware-core-2.0.110 lib/dropdown_button_item.rb
renalware-core-2.0.109 lib/dropdown_button_item.rb
renalware-core-2.0.108 lib/dropdown_button_item.rb
renalware-core-2.0.106 lib/dropdown_button_item.rb
renalware-core-2.0.105 lib/dropdown_button_item.rb