Module: ActiveData::Model::Associations::NestedAttributes::PrependMethods

Defined in:
lib/active_data/model/associations/nested_attributes.rb

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attrs) ⇒ Object Also known as: attributes=



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_data/model/associations/nested_attributes.rb', line 18

def assign_attributes(attrs)
  if self.class.nested_attributes_options.present?
    attrs = attrs.to_unsafe_hash if attrs.respond_to?(:to_unsafe_hash)
    attrs = attrs.stringify_keys

    nested_attrs = self.class.nested_attributes_options.keys
      .each_with_object({}) do |association_name, result|
        name = "#{association_name}_attributes"
        result[name] = attrs.delete(name) if attrs.key?(name)
      end

    super(attrs.merge!(nested_attrs))
  else
    super(attrs)
  end
end