Class: Infold::FormElement

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/infold/property/form_element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, **attrs) ⇒ FormElement

Returns a new instance of FormElement.



14
15
16
17
18
# File 'lib/infold/property/form_element.rb', line 14

def initialize(field, **attrs)
  @field = field
  @association_fields = []
  super(**attrs)
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



8
9
10
# File 'lib/infold/property/form_element.rb', line 8

def association
  @association
end

#association_fieldsObject (readonly)

Returns the value of attribute association_fields.



5
6
7
# File 'lib/infold/property/form_element.rb', line 5

def association_fields
  @association_fields
end

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/infold/property/form_element.rb', line 5

def field
  @field
end

#form_kindObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/infold/property/form_element.rb', line 38

def form_kind
  if @form_kind.to_s == 'association_search'
    if field.association&.belongs_to?
      :association_search
    else
      :text
    end
  elsif @form_kind.to_s == 'select'
    if field.association&.belongs_to? || field.enum.present?
      :select
    else
      :text
    end
  elsif kind_datetime?
    :datetime
  elsif kind_file?
    :file
  elsif field.type == :boolean
    :switch
  else
    (@form_kind.presence || :text).to_sym
  end
end

#in_associationObject

Returns the value of attribute in_association.



8
9
10
# File 'lib/infold/property/form_element.rb', line 8

def in_association
  @in_association
end

#seqObject

Returns the value of attribute seq.



8
9
10
# File 'lib/infold/property/form_element.rb', line 8

def seq
  @seq
end

Instance Method Details

#add_association_fields(field, **attrs) ⇒ Object



20
21
22
23
24
# File 'lib/infold/property/form_element.rb', line 20

def add_association_fields(field, **attrs)
  field.build_form_element(in_association: true, **attrs)
  @association_fields << field
  field
end

#kind_datetime?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/infold/property/form_element.rb', line 34

def kind_datetime?
  field.type.to_s == 'datetime'
end

#kind_file?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/infold/property/form_element.rb', line 30

def kind_file?
  field.active_storage.present? || @form_kind.to_s == 'file'
end

#kind_has_association?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/infold/property/form_element.rb', line 26

def kind_has_association?
  field.association && !field.association.belongs_to? || @form_kind.to_s == 'association'
end