Class: ActionForm::Base
- Inherits:
-
Phlex::HTML
- Object
- Phlex::HTML
- ActionForm::Base
- 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
Class Attribute Summary collapse
-
.elements ⇒ Object
writeonly
Sets the attribute elements.
- .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, **html_options) ⇒ Base
constructor
A new instance of Base.
- #render_in(view_context) ⇒ Object
- #view_template ⇒ Object
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, **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, **) super() @object = object @scope ||= scope @params = @scope && params.respond_to?(@scope) ? params.public_send(@scope) : params @html_options = @elements_instances = [] build_from_object end |
Class Attribute Details
.elements=(value) ⇒ Object (writeonly)
Sets the attribute elements
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_instances ⇒ Object (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 |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
11 12 13 |
# File 'lib/action_form/base.rb', line 11 def errors @errors end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
11 12 13 |
# File 'lib/action_form/base.rb', line 11 def @html_options end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
11 12 13 |
# File 'lib/action_form/base.rb', line 11 def object @object end |
#scope ⇒ Object (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_class ⇒ Object
16 17 18 |
# File 'lib/action_form/base.rb', line 16 def subform_class ActionForm::Subform end |
Instance Method Details
#build_from_object ⇒ Object
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 |
#helpers ⇒ Object
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_template ⇒ Object
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 |