Sha256: daca4838480668662eccdff4d7a6932e81212af42071a1e874c3dd223c04cab7

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Amoeba
  module Macros
    class HasMany < ::Amoeba::Macros::Base
      def follow(relation_name, association)
        if @cloner.amoeba.clones.include?(relation_name.to_sym)
          follow_with_clone(relation_name)
        else
          follow_without_clone(relation_name, association)
        end
      end

      def follow_with_clone(relation_name)
        # This  is  a  M:M  "has many  through"  where  we
        # actually copy  and reassociate the  new children
        # rather than only maintaining the associations
        @old_object.__send__(relation_name).each do |old_obj|
          relation_name = remapped_relation_name(relation_name)
          # associate this new child to the new parent object
          @new_object.__send__(relation_name) << old_obj.amoeba_dup
        end
      end

      def follow_without_clone(relation_name, association)
        # This is a regular 1:M "has many"
        #
        # copying the children of the regular has many will
        # effectively do what is desired anyway, the through
        # association is really just for convenience usage
        # on the model
        return if association.is_a?(ActiveRecord::Reflection::ThroughReflection)

        @old_object.__send__(relation_name).each do |old_obj|
          copy_of_obj = old_obj.amoeba_dup(@options)
          copy_of_obj[:"#{association.foreign_key}"] = nil
          relation_name = remapped_relation_name(relation_name)
          # associate this new child to the new parent object
          @new_object.__send__(relation_name) << copy_of_obj
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
amoeba-3.2.0 lib/amoeba/macros/has_many.rb
amoeba-3.1.0 lib/amoeba/macros/has_many.rb
amoeba-3.0.0 lib/amoeba/macros/has_many.rb