Class: Forms::FieldsForBuilder
- Inherits:
-
Object
- Object
- Forms::FieldsForBuilder
- Includes:
- PhlexForms::Builder
- Defined in:
- lib/forms/fields_for_builder.rb
Overview
Yielded inside Form#fields_for for nested attributes. Reuses the shared Builder API (f.field, f.Input, ...) against a nested scope, rendering through the parent form.
Constant Summary
Constants included from PhlexForms::Builder
PhlexForms::Builder::INPUT_TYPE_INFERENCE, PhlexForms::Builder::INPUT_TYPE_MODIFIERS
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#parent_form ⇒ Object
readonly
Returns the value of attribute parent_form.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #field_id(name) ⇒ Object
- #field_name(name) ⇒ Object
- #field_object(name) ⇒ Object
-
#fields_for(association_name, model = nil) ⇒ Object
Nested fields_for (single association or has_many collection).
-
#initialize(model:, scope:, errors:, parent_form:) ⇒ FieldsForBuilder
constructor
A new instance of FieldsForBuilder.
-
#render ⇒ Object
The Builder mixin renders through
render; delegate to the parent form.
Methods included from PhlexForms::Builder
#Checkbox, #Control, #FileInput, #Hidden, #Input, #Label, #Radio, #Select, #Textarea, #Toggle, #field
Constructor Details
#initialize(model:, scope:, errors:, parent_form:) ⇒ FieldsForBuilder
Returns a new instance of FieldsForBuilder.
12 13 14 15 16 17 |
# File 'lib/forms/fields_for_builder.rb', line 12 def initialize(model:, scope:, errors:, parent_form:) @model = model @scope = scope @errors = errors @parent_form = parent_form end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
10 11 12 |
# File 'lib/forms/fields_for_builder.rb', line 10 def errors @errors end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
10 11 12 |
# File 'lib/forms/fields_for_builder.rb', line 10 def model @model end |
#parent_form ⇒ Object (readonly)
Returns the value of attribute parent_form.
10 11 12 |
# File 'lib/forms/fields_for_builder.rb', line 10 def parent_form @parent_form end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
10 11 12 |
# File 'lib/forms/fields_for_builder.rb', line 10 def scope @scope end |
Instance Method Details
#field_id(name) ⇒ Object
45 |
# File 'lib/forms/fields_for_builder.rb', line 45 def field_id(name) = "#{@scope.tr('[', '_').delete(']')}_#{name}" |
#field_name(name) ⇒ Object
44 |
# File 'lib/forms/fields_for_builder.rb', line 44 def field_name(name) = "#{@scope}[#{name}]" |
#field_object(name) ⇒ Object
24 25 26 |
# File 'lib/forms/fields_for_builder.rb', line 24 def field_object(name) Forms::Field.new(name:, model: @model, scope: @scope, errors: @errors, form: @parent_form) end |
#fields_for(association_name, model = nil) ⇒ Object
Nested fields_for (single association or has_many collection).
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/forms/fields_for_builder.rb', line 29 def fields_for(association_name, model = nil, &) return unless block_given? associated = model || (@model.public_send(association_name) if @model.respond_to?(association_name)) base_scope = "#{@scope}[#{association_name}_attributes]" if associated.respond_to?(:each_with_index) associated.each_with_index do |item, index| yield build_nested("#{base_scope}[#{index}]", item) end else yield build_nested(base_scope, associated) end end |
#render ⇒ Object
The Builder mixin renders through render; delegate to the parent form.
20 21 22 |
# File 'lib/forms/fields_for_builder.rb', line 20 def render(...) @parent_form.render(...) end |