Module: Para::Ext::DeepCloneExtension

Defined in:
lib/para/ext/deep_cloneable.rb

Instance Method Summary collapse

Instance Method Details

#dup_default_attribute_value_to(kopy, attribute, origin) ⇒ Object

Override the default deep_cloneable method to avoid nested :except rules that target polymorphic relations to try to assign default values to unexisting attributes on models that don’t define the excluded attribute

For example, we can have :

{ except: { comments: { author: [:confirmation_token] } } }

Because some comments have a an author that’s a user, and the user ‘acts_as_cloneable` macro defines `{ except: [:confirmation_token] }`, but if one of the comments has an anonymous user in its author relation, this method would faild with a ActiveModel::MissingAttributeError.



19
20
21
22
23
# File 'lib/para/ext/deep_cloneable.rb', line 19

def dup_default_attribute_value_to(kopy, attribute, origin)
  return unless kopy.attributes.keys.include?(attribute.to_s)

  kopy[attribute] = origin.class.column_defaults.dup[attribute.to_s]
end