Skip to content

Commit

Permalink
amoeba-rb#82: Ensure cloned associations are present in includes if r…
Browse files Browse the repository at this point in the history
…equired.
  • Loading branch information
benjamin-hull committed Jan 22, 2019
1 parent 25a6a22 commit 9473092
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/amoeba/cloner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,29 @@ def inherit_parent_settings

def apply_clones
amoeba.clones.each do |clone_field|
ensure_clone_in_includes(clone_field)
exclude_clone_if_has_many_through(clone_field)
end
end

# If the field to be cloned isn't in the includes, add it so it will
# be processed in the apply_associations step.
# This handles the case where no includes or excludes are set at all.
def ensure_clone_in_includes(clone_field)
return if amoeba.excludes.present?
amoeba.includes[clone_field] = {} unless amoeba.includes.values.include?(clone_field)
end

def exclude_clone_if_has_many_through(clone_field)
association = @object_klass.reflect_on_association(clone_field)

# if this is a has many through and we're gonna deep
# 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 ||
return unless association.macro == :has_many &&
association.is_a?(::ActiveRecord::Reflection::ThroughReflection)

amoeba.exclude_association(association.options[:through])
end

Expand Down

0 comments on commit 9473092

Please sign in to comment.