lib/amoeba/cloner.rb in amoeba-3.2.0 vs lib/amoeba/cloner.rb in amoeba-3.3.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'forwardable' module Amoeba class Cloner extend Forwardable @@ -27,11 +29,11 @@ end private def parenting_style - amoeba.upbringing ? amoeba.upbringing : _parent_amoeba.parenting + amoeba.upbringing || _parent_amoeba.parenting end def inherit_strict_parent_settings fresh_amoeba(&_parent_amoeba_settings) end @@ -45,12 +47,13 @@ amoeba(&_parent_amoeba_settings) amoeba(&_amoeba_settings) end def inherit_parent_settings - return if !_parent_amoeba.inherit - return unless %w(strict relaxed submissive).include?(parenting_style.to_s) + return unless _parent_amoeba.inherit + return unless %w[strict relaxed submissive].include?(parenting_style.to_s) + __send__("inherit_#{parenting_style}_parent_settings".to_sym) end def apply_clones amoeba.clones.each do |clone_field| @@ -65,24 +68,27 @@ # copy the child records, exclude the regular join # table from copying so we don't end up with the new # and old children on the copy return unless association.macro == :has_many || association.is_a?(::ActiveRecord::Reflection::ThroughReflection) + amoeba.exclude_association(association.options[:through]) end def follow_only_includes amoeba.includes.each do |include, options| next if options[:if] && !@old_object.send(options[:if]) + follow_association(include, @object_klass.reflect_on_association(include)) end end def follow_all_except_excludes @object_klass.reflections.each do |name, association| exclude = amoeba.excludes[name.to_sym] next if exclude && (exclude.blank? || @old_object.send(exclude[:if])) + follow_association(name, association) end end def follow_all @@ -106,11 +112,12 @@ apply_associations end def follow_association(relation_name, association) return unless amoeba.known_macros.include?(association.macro.to_sym) + follow_klass = ::Amoeba::Macros.list[association.macro.to_sym] - follow_klass.new(self).follow(relation_name, association) if follow_klass + follow_klass&.new(self)&.follow(relation_name, association) end def process_overrides amoeba.overrides.each do |block| block.call(@old_object, @new_object)