Sha256: 4e65f4fb48fc6690cff7a89b38ce6c8f37b7ab2d5345bd42484ccf0bb8b4ef0c

Contents?: true

Size: 1.48 KB

Versions: 26

Compression:

Stored size: 1.48 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
            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


      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

26 entries across 26 versions & 1 rubygems

Version Path
active_type-2.5.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.5.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.4.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.4.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.3.4 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.3.3 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.3.2 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.3.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.3.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.2.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.1.2 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.1.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.1.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-2.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.10.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.10.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.9.1 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.9.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.8.0 lib/active_type/nested_attributes/nests_one_association.rb
active_type-1.7.0 lib/active_type/nested_attributes/nests_one_association.rb