Class: ActiveType::NestedAttributes::NestsOneAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/active_type/nested_attributes/nests_one_association.rb

Instance Method Summary collapse

Methods inherited from Association

#initialize, #save, #validate

Constructor Details

This class inherits a constructor from ActiveType::NestedAttributes::Association

Instance Method Details

#assign_attributes(parent, attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_type/nested_attributes/nests_one_association.rb', line 11

def assign_attributes(parent, attributes)
  return if attributes.nil?
  attributes = attributes.with_indifferent_access
  return if reject?(parent, attributes)

  assigned_child = assigned_children(parent).first
  destroy = truthy?(attributes.delete(:_destroy)) && @allow_destroy

  if id = attributes.delete(:id)
    assigned_child ||= fetch_child(parent, id)
    if assigned_child
      assigned_child.id = id
      if assigned_child.id == assigned_child.id_was
        assigned_child.attributes = attributes
      else
        raise AssignmentError, "child record '#{@target_name}' did not match id '#{id}'"
      end
      if destroy
        assigned_child.mark_for_destruction
      end
    end
  elsif !destroy
    if assigned_child
      assigned_child.attributes = attributes
    else
      add_child(parent, build_child(parent, attributes))
    end
  end
end