Class: ActionForm::Base
- Inherits:
-
Phlex::HTML
- Object
- Phlex::HTML
- ActionForm::Base
- 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
Class Attribute Summary collapse
-
.elements ⇒ Object
writeonly
Sets the attribute elements.
-
.params_definition ⇒ Object
writeonly
Sets the attribute params_definition.
- .scope(scope = nil) ⇒ Object
Instance Attribute Summary collapse
-
#elements_instances ⇒ Object
readonly
Returns the value of attribute elements_instances.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
- #build_from_object ⇒ Object
- #helpers ⇒ Object
-
#initialize(object: nil, scope: self.class.scope, params: nil, owner: nil, **html_options) ⇒ Base
constructor
A new instance of Base.
- #internal_call(parent: nil, state: nil, &block) ⇒ Object
- #render_in(view_context) ⇒ Object
- #view_template ⇒ Object
Methods included from Composition
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
Methods included from SchemaDSL
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, **) super() @object = object @scope ||= scope @params = params @html_options = @elements_instances = [] @owner = owner build_from_object end |
Class Attribute Details
.elements=(value) ⇒ Object (writeonly)
Sets the attribute elements
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
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_instances ⇒ Object (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 |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
12 13 14 |
# File 'lib/action_form/base.rb', line 12 def errors @errors end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
12 13 14 |
# File 'lib/action_form/base.rb', line 12 def @html_options end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
12 13 14 |
# File 'lib/action_form/base.rb', line 12 def object @object end |
#scope ⇒ Object (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_class ⇒ Object
17 18 19 |
# File 'lib/action_form/base.rb', line 17 def subform_class ActionForm::Subform end |
Instance Method Details
#build_from_object ⇒ Object
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 |
#helpers ⇒ Object
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_template ⇒ Object
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 |