Class: Lookbook::TagComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ClassNamesHelper, UiElementsHelper
Defined in:
app/components/lookbook/tag_component.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassNamesHelper

#build_tag_values, #class_names

Methods included from UiElementsHelper

#code, #icon, #lookbook_render, #lookbook_tag, #prose

Constructor Details

#initialize(tag: :div, name: nil, cloak: false, **html_attrs) ⇒ TagComponent

Returns a new instance of TagComponent.



6
7
8
9
10
11
12
13
# File 'app/components/lookbook/tag_component.rb', line 6

def initialize(tag: :div, name: nil, cloak: false, **html_attrs)
  @tag = tag
  html_attrs[:data] ||= {}
  html_attrs[:data][:component] = name if name.present?
  html_attrs[:"x-cloak"] = true if cloak == true
  html_attrs[self.class.escape_attribute_key] = false
  @html_attrs = html_attrs
end

Class Method Details

.escape_attribute_keyObject



22
23
24
25
26
27
28
29
30
31
# File 'app/components/lookbook/tag_component.rb', line 22

def self.escape_attribute_key
  @escape_attribute_key ||= (
    (
      Gem::Version.new(Rails.version) < Gem::Version.new("5.2.7.1")
    ) || (
      Gem::Version.new(Rails.version) >= Gem::Version.new("6") &&
      Gem::Version.new(Rails.version) < Gem::Version.new("6.1.5.1")
    )
  ) ? :escape_attributes : :escape
end

Instance Method Details

#callObject



15
16
17
18
19
20
# File 'app/components/lookbook/tag_component.rb', line 15

def call
  @html_attrs[:class] = class_names(@html_attrs[:class])
  tag.public_send(@tag.to_sym, **@html_attrs) do
    content
  end
end