Module: Formed::NestedAttributes

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/formed/nested_attributes.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods Classes: TooManyRecords

Instance Method Summary collapse

Instance Method Details

#_destroyObject



146
147
148
# File 'lib/formed/nested_attributes.rb', line 146

def _destroy
  marked_for_destruction?
end

#_ensure_no_duplicate_errorsObject



71
72
73
# File 'lib/formed/nested_attributes.rb', line 71

def _ensure_no_duplicate_errors
  errors.uniq!
end

#associated_records_to_validate(association, new_record) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/formed/nested_attributes.rb', line 14

def associated_records_to_validate(association, new_record)
  if new_record
    association&.target
  else
    association.target
  end
end

#association_valid?(reflection, record, index = nil) ⇒ Boolean

Returns whether or not the association is valid and applies any errors to the parent, self, if it wasn’t. Skips any :autosave enabled records if they’re marked_for_destruction? or destroyed.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/formed/nested_attributes.rb', line 43

def association_valid?(reflection, record, index = nil)
  context = nil

  unless (valid = record.valid?(context))
    indexed_attribute = !index.nil? && reflection.options[:index_errors]

    record.errors.group_by_attribute.each do |attribute, errors|
      attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)

      errors.each do |error|
        self.errors.import(
          error,
          attribute: attribute
        )
      end
    end
  end
  valid
end

#normalize_reflection_attribute(indexed_attribute, reflection, index, attribute) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/formed/nested_attributes.rb', line 63

def normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
  if indexed_attribute
    "#{reflection.name}[#{index}].#{attribute}"
  else
    "#{reflection.name}.#{attribute}"
  end
end

#validate_collection_association(reflection) ⇒ Object

Validate the associated records if :validate or :autosave is turned on for the association specified by reflection.



33
34
35
36
37
38
# File 'lib/formed/nested_attributes.rb', line 33

def validate_collection_association(reflection)
  return unless (association = association_instance_get(reflection.name))
  return unless (records = associated_records_to_validate(association, new_record?))

  records.each_with_index { |record, index| association_valid?(reflection, record, index) }
end

#validate_single_association(reflection) ⇒ Object

Validate the association if :validate or :autosave is turned on for the association.



24
25
26
27
28
# File 'lib/formed/nested_attributes.rb', line 24

def validate_single_association(reflection)
  association = association_instance_get(reflection.name)
  record      = association&.reader
  association_valid?(reflection, record) if record
end