Class: Shaf::Formable::Field

Inherits:
Object
  • Object
show all
Extended by:
ImmutableAttr
Defined in:
lib/shaf/formable/field.rb

Constant Summary collapse

HTML_TYPE_MAPPINGS =
{
  string: 'text',
  boolean: 'checkbox'
}.freeze

Constants included from ImmutableAttr

ImmutableAttr::NON_DUPABLE_CLASSES

Instance Method Summary collapse

Methods included from ImmutableAttr

dup, immutable_accessor, immutable_reader, immutable_writer

Constructor Details

#initialize(name, params = {}) ⇒ Field

Returns a new instance of Field.



17
18
19
20
21
22
23
24
25
26
# File 'lib/shaf/formable/field.rb', line 17

def initialize(name, params = {})
  @name = name
  @type = params[:type]&.to_sym
  @title = params[:title] || params[:label]
  @hidden = params.fetch(:hidden, false)
  @has_value = params.key? :value
  @value = params[:value]
  @required = params.fetch(:required, false)
  @accessor_name = params.fetch(:accessor_name, name).to_sym
end

Instance Method Details

#has_value?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/shaf/formable/field.rb', line 28

def has_value?
  @has_value
end

#to_htmlObject



37
38
39
40
41
42
43
44
# File 'lib/shaf/formable/field.rb', line 37

def to_html
  [
    '<div class="form--input-group">',
    label_element,
    input_element,
    '</div>'
  ].compact.join("\n")
end

#value=(v) ⇒ Object



32
33
34
35
# File 'lib/shaf/formable/field.rb', line 32

def value=(v)
  @value = v
  @has_value = true
end