Class: ActionForm::Base

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

Overview

Base class for ActionForm components that provides form building functionality and integrates with Phlex for HTML rendering.

Direct Known Subclasses

Rails::Base

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composition

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

Methods included from ElementsDSL

included

Methods included from SchemaDSL

included

Constructor Details

#initialize(object: nil, scope: self.class.scope, params: nil, owner: nil, **html_options) ⇒ Base

Returns a new instance of Base.



34
35
36
37
38
39
40
41
42
43
# File 'lib/action_form/base.rb', line 34

def initialize(object: nil, scope: self.class.scope, params: nil, owner: nil, **html_options)
  super()
  @object = object
  @scope ||= scope
  @params = params
  @html_options = html_options
  @elements_instances = []
  @owner = owner
  build_from_object
end

Class Attribute Details

.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/base.rb', line 15

def elements=(value)
  @elements = value
end

.params_definition=(value) ⇒ Object (writeonly)

Sets the attribute params_definition

Parameters:

  • value

    the value to set the attribute params_definition to.



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

def params_definition=(value)
  @params_definition = value
end

.scope(scope = nil) ⇒ Object



21
22
23
24
25
# File 'lib/action_form/base.rb', line 21

def scope(scope = nil)
  return @scope unless scope

  @scope = scope
end

Instance Attribute Details

#elements_instancesObject (readonly)

Returns the value of attribute elements_instances.



12
13
14
# File 'lib/action_form/base.rb', line 12

def elements_instances
  @elements_instances
end

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/action_form/base.rb', line 12

def errors
  @errors
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



12
13
14
# File 'lib/action_form/base.rb', line 12

def html_options
  @html_options
end

#objectObject (readonly)

Returns the value of attribute object.



12
13
14
# File 'lib/action_form/base.rb', line 12

def object
  @object
end

#scopeObject (readonly)

Returns the value of attribute scope.



12
13
14
# File 'lib/action_form/base.rb', line 12

def scope
  @scope
end

Class Method Details

.inherited(subclass) ⇒ Object



27
28
29
30
31
# File 'lib/action_form/base.rb', line 27

def inherited(subclass)
  super
  subclass.elements = elements.dup
  subclass.scope = scope&.dup
end

.subform_classObject



17
18
19
# File 'lib/action_form/base.rb', line 17

def subform_class
  ActionForm::Subform
end

Instance Method Details

#build_from_objectObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/action_form/base.rb', line 45

def build_from_object
  self.class.elements.each do |name, element_definition|
    if element_definition < ActionForm::SubformsCollection
      @elements_instances << build_many_subforms(name, element_definition)
      @elements_instances.last << build_subform_template(name, element_definition.subform_definition)
    elsif element_definition < ActionForm::Subform
      @elements_instances << build_subform(name, element_definition)
    elsif element_definition < ActionForm::Element
      @elements_instances << element_definition.new(name, @params || @object, parent_name: @scope, owner: self)
    end
  end
end

#helpersObject



77
78
79
# File 'lib/action_form/base.rb', line 77

def helpers
  @_view_context
end

#internal_call(parent: nil, state: nil, &block) ⇒ Object



70
71
72
73
74
75
# File 'lib/action_form/base.rb', line 70

def internal_call(parent: nil, state: nil, &block)
  @_view_context ||= state&.user_context&.dig(:rails_view_context)
  @_view_context ||= parent.helpers if parent.respond_to?(:helpers)
  @_view_context ||= parent.view_context if parent.respond_to?(:view_context)
  super
end

#render_in(view_context) ⇒ Object



65
66
67
68
# File 'lib/action_form/base.rb', line 65

def render_in(view_context)
  @_view_context = view_context
  view_context.render html: call.html_safe
end

#view_templateObject



58
59
60
61
62
63
# File 'lib/action_form/base.rb', line 58

def view_template
  render_form do
    render_elements
    render_submit
  end
end