Sha256: 5d19b05e287ccec333ece4491a83ed1e9b4133f303f0e3f1b83bb58b52a41888

Contents?: true

Size: 1013 Bytes

Versions: 1

Compression:

Stored size: 1013 Bytes

Contents

class InputComponent < ViewComponent::Base
  include Alveole::Concerns::Bem

  MODIFIERS = %i[disabled error].freeze

  def initialize(type: nil, label: nil,
                 value: nil,
                 form: nil,
                 fieldname: nil,
                 collection: nil,
                 modifiers: [],
                 options: {})
    super

    @label = label
    @form = form
    @fieldname = fieldname
    @collection = collection
    @options = options
    @options[:value] = value if value
    @type = type || :text
    @error_message = error_message

    modifiers << :error if error?
    modifiers << :disabled if options[:disabled]
    self.modifiers = modifiers
  end

  private

  def error?
    return false unless @form.object.respond_to? :errors

    @error ||= @form.object.errors.include?(@fieldname.to_s.sub(/_id$/, ''))
  end

  def error_message
    return unless error?

    @error_message ||= @form.object.errors.messages_for(@fieldname.to_s.sub(/_id$/, '')).join(', ')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alveole-0.0.1 lib/alveole/components/input_component.rb