Class: ActionForm::Subform

Inherits:
Phlex::HTML
  • Object
show all
Includes:
Composition, ElementsDSL, Rendering, SchemaDSL
Defined in:
lib/action_form/subform.rb

Overview

Subform class for ActionForm that handles nested form structures. It allows building forms within forms, supporting has_one and has_many relationships. Includes schema and element DSL functionality for defining form elements.

Direct Known Subclasses

Rails::Subform

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composition

included

Methods included from ElementsDSL

included

Methods included from SchemaDSL

included

Methods included from Rendering

#render_element, #render_elements, #render_form, #render_inline_errors, #render_input, #render_label, #render_many_subforms, #render_new_subform_button, #render_remove_subform_button, #render_subform, #render_submit

Constructor Details

#initialize(name:, scope: nil, model: nil, params: nil, owner: nil, **tags) ⇒ Subform

rubocop:disable Metrics/ParameterLists



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/action_form/subform.rb', line 27

def initialize(name:, scope: nil, model: nil, params: nil, owner: nil, **tags) # rubocop:disable Metrics/ParameterLists
  super()
  @name = name
  @scope = scope
  @object = model
  @params = params
  @elements_instances = []
  @tags = tags
  @owner = owner
  build_from_object
end

Class Attribute Details

.defaultObject

Returns the value of attribute default.



14
15
16
# File 'lib/action_form/subform.rb', line 14

def default
  @default
end

.elements=(value) ⇒ Object (writeonly)

Sets the attribute elements

Parameters:

  • value

    the value to set the attribute elements to.



15
16
17
# File 'lib/action_form/subform.rb', line 15

def elements=(value)
  @elements = value
end

Instance Attribute Details

#elements_instancesObject (readonly)

Returns the value of attribute elements_instances.



24
25
26
# File 'lib/action_form/subform.rb', line 24

def elements_instances
  @elements_instances
end

#helpersObject

Returns the value of attribute helpers.



25
26
27
# File 'lib/action_form/subform.rb', line 25

def helpers
  @helpers
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/action_form/subform.rb', line 24

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



24
25
26
# File 'lib/action_form/subform.rb', line 24

def object
  @object
end

#tagsObject (readonly)

Returns the value of attribute tags.



24
25
26
# File 'lib/action_form/subform.rb', line 24

def tags
  @tags
end

Class Method Details

.inherited(subclass) ⇒ Object



17
18
19
20
21
# File 'lib/action_form/subform.rb', line 17

def inherited(subclass)
  super
  subclass.elements = elements.dup
  subclass.default = default.dup
end

Instance Method Details

#build_from_objectObject



39
40
41
42
43
44
45
# File 'lib/action_form/subform.rb', line 39

def build_from_object
  self.class.elements.each do |element_name, element_definition|
    @elements_instances << element_definition.new(element_name, @params || @object, parent_name: @scope)
    @elements_instances.last.tags.merge!(subform: @name)
    @elements_instances.last.owner = self
  end
end

#html_classObject



59
60
61
# File 'lib/action_form/subform.rb', line 59

def html_class
  "#{name}_subform"
end

#html_idObject



55
56
57
# File 'lib/action_form/subform.rb', line 55

def html_id
  "#{name}_#{tags[:index]}"
end

#render?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/action_form/subform.rb', line 47

def render?
  true
end

#template_html_idObject



51
52
53
# File 'lib/action_form/subform.rb', line 51

def template_html_id
  "#{name}_template"
end

#view_templateObject



63
64
65
# File 'lib/action_form/subform.rb', line 63

def view_template
  render_elements
end