Sha256: 9060bedc7b581b663086cd6a4479b7ab3a37de62734e38c07d6f1296ebb5f44e
Contents?: true
Size: 1.38 KB
Versions: 2
Compression:
Stored size: 1.38 KB
Contents
require 'active_type/nested_attributes/association' module ActiveType module NestedAttributes class AssignmentError < StandardError; end class NestsOneAssociation < Association 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 if assigned_child.id == id 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 assigned_child ||= add_child(parent, build_child(parent, {})) assigned_child.attributes = attributes end end private def add_child(parent, child) parent[@target_name] = child end def assign_children(parent, children) parent[@target_name] = children.first end def derive_class_name @target_name.to_s.camelize end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_type-0.7.4 | lib/active_type/nested_attributes/nests_one_association.rb |
active_type-0.7.3 | lib/active_type/nested_attributes/nests_one_association.rb |