Class: ActionForm::Base

Inherits:
Phlex::HTML
  • Object
show all
Includes:
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 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, **html_options) ⇒ Base

Returns a new instance of Base.



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

def initialize(object: nil, scope: self.class.scope, params: nil, **html_options)
  super()
  @object = object
  @scope ||= scope
  @params = @scope && params.respond_to?(@scope) ? params.public_send(@scope) : params
  @html_options = html_options
  @elements_instances = []
  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.



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

def elements=(value)
  @elements = value
end

.scope(scope = nil) ⇒ Object



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

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

  @scope = scope
end

Instance Attribute Details

#elements_instancesObject (readonly)

Returns the value of attribute elements_instances.



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

def elements_instances
  @elements_instances
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



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

def html_options
  @html_options
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Class Method Details

.inherited(subclass) ⇒ Object



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

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

.subform_classObject



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

def subform_class
  ActionForm::Subform
end

Instance Method Details

#build_from_objectObject



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

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)
    end
  end
end

#helpersObject



68
69
70
# File 'lib/action_form/base.rb', line 68

def helpers
  @_view_context
end

#render_in(view_context) ⇒ Object



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

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

#view_templateObject



56
57
58
59
60
61
# File 'lib/action_form/base.rb', line 56

def view_template
  render_form do
    render_elements
    render_submit
  end
end