Module: Para::Cloneable::ClassMethods
- Defined in:
- lib/para/cloneable.rb
Instance Method Summary collapse
-
#acts_as_cloneable(*args) ⇒ Object
Allow configuring cloneable options for the model, and making the model cloneable in the admin.
- #cloneable? ⇒ Boolean
Instance Method Details
#acts_as_cloneable(*args) ⇒ Object
Allow configuring cloneable options for the model, and making the model cloneable in the admin
The provided arguments are the cloneable associations for the model, and keyword arguments are passed to the #deep_clone method.
An optional :prepare keyword argument can be passed and will be called by #deep_clone to allow altering the cloned resource before saving it.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/para/cloneable.rb', line 59 def acts_as_cloneable(*args) @cloneable = true = args. # Allow nested STI resources to define their own relations to clone even # if other sibling models don't define those relations [:skip_missing_associations] = true # If `acts_as_cloneable` is called multiple times, for example by a parent class # and the by its subclass, ensure that we don't lose the previously defined # cloneable options, to avoid having to repeat the same include options in each # subclass, if it has to define subclass specific cloneable options. = || {} # Prepare the new cloneable options hash with the provided arguments = .reverse_merge({ include: args }) # Merges previous and new cloneable options into the cloneable_options class # attribute, also merging the `:include` array self. = .merge() do |key, a, b| a.is_a?(Array) && b.is_a?(Array) ? (a + b).uniq : b end end |
#cloneable? ⇒ Boolean
87 88 89 |
# File 'lib/para/cloneable.rb', line 87 def cloneable? @cloneable ||= false end |