Sha256: 66457c71952112c6ed5092a3d48d62b4e55c10f7885d141901ec2198ff924103

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

class FoxTail::LabelComponent < FoxTail::BaseComponent
  include FoxTail::Concerns::Formable

  has_option :state
  has_option :required, type: :boolean, default: false
  has_option :value

  def before_render
    super

    update_state_from_object!
    html_attributes[:for] ||= tag_id_for_value value
    html_attributes[:class] = classnames theme.apply(:root, self), html_class
  end

  def call
    content_tag :label, retrieve_content, html_attributes
  end

  private

  def retrieve_content
    return content if content?
    return unless object_name? && method_name?

    label_translator.translate || default_label
  end

  def default_label
    object = convert_to_model self.object
    return object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name)

    method_name&.humanize
  end

  def label_translator(value: self.value, default: "")
    translator value: value, default: default, scope: "helpers.label"
  end

  def update_state_from_object!
    return unless object? && !options.key(:state)

    options[:state] = if object_errors?
                        :invalid
                      elsif html_attributes[:value].present?
                        :valid
                      else
                        :none
                      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fox_tail-0.1.1 app/components/fox_tail/label_component.rb
fox_tail-0.1.0 app/components/fox_tail/label_component.rb