Class: Forms::FieldsForBuilder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from PhlexForms::Builder

#Checkbox, #Control, #FileInput, #Hidden, #Input, #Label, #Radio, #Select, #Textarea, #Toggle, #field, #group, #row

Constructor Details

#initialize(model:, scope:, errors:, parent_form:) ⇒ FieldsForBuilder

Returns a new instance of FieldsForBuilder.



13
14
15
16
17
18
# File 'lib/forms/fields_for_builder.rb', line 13

def initialize(model:, scope:, errors:, parent_form:)
  @model = model
  @scope = scope
  @errors = errors
  @parent_form = parent_form
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/forms/fields_for_builder.rb', line 10

def errors
  @errors
end

#modelObject (readonly) Also known as: object

Returns the value of attribute model.



10
11
12
# File 'lib/forms/fields_for_builder.rb', line 10

def model
  @model
end

#parent_formObject (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

#scopeObject (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

#default_field_variantsObject



30
31
32
# File 'lib/forms/fields_for_builder.rb', line 30

def default_field_variants
  @parent_form.default_field_variants
end

#field_id(name) ⇒ Object



57
# File 'lib/forms/fields_for_builder.rb', line 57

def field_id(name)    = "#{@scope.tr('[', '_').delete(']')}_#{name}"

#field_name(name) ⇒ Object



56
# File 'lib/forms/fields_for_builder.rb', line 56

def field_name(name)  = "#{@scope}[#{name}]"

#field_object(name, error_name: nil) ⇒ Object Also known as: []



25
26
27
# File 'lib/forms/fields_for_builder.rb', line 25

def field_object(name, error_name: nil)
  Forms::Field.new(name:, model: @model, scope: @scope, errors: @errors, form: @parent_form, error_name:)
end

#field_value(name) ⇒ Object



58
# File 'lib/forms/fields_for_builder.rb', line 58

def field_value(name) = field_object(name).field_value

#fields_for(association_name, model = nil, nested_attributes: true) ⇒ Object

Nested fields_for (single association or has_many collection). nested_attributes: false nests under the raw name (JSONB/hash columns).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/forms/fields_for_builder.rb', line 40

def fields_for(association_name, model = nil, nested_attributes: true, &)
  return unless block_given?

  associated = model || (@model.public_send(association_name) if @model.respond_to?(association_name))
  attributes_key = nested_attributes ? "#{association_name}_attributes" : association_name.to_s
  base_scope = "#{@scope}[#{attributes_key}]"

  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

#renderObject

The Builder mixin renders through render; delegate to the parent form.



21
22
23
# File 'lib/forms/fields_for_builder.rb', line 21

def render(...)
  @parent_form.render(...)
end

#themeObject



34
35
36
# File 'lib/forms/fields_for_builder.rb', line 34

def theme
  @parent_form.theme
end