Module: Para::Cloneable::ClassMethods

Defined in:
lib/para/cloneable.rb

Instance Method Summary collapse

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

  options = args.extract_options!

  # Allow nested STI resources to define their own relations to clone even
  # if other sibling models don't define those relations
  options[: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.
  previous_cloneable_options = cloneable_options || {}

  # Prepare the new cloneable options hash with the provided arguments
  new_cloneable_options = options.reverse_merge({
    include: args
  })

  # Merges previous and new cloneable options into the cloneable_options class
  # attribute, also merging the `:include` array
  self.cloneable_options =
    previous_cloneable_options.merge(new_cloneable_options) do |key, a, b|
      a.is_a?(Array) && b.is_a?(Array) ? (a + b).uniq : b
    end
end

#cloneable?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/para/cloneable.rb', line 87

def cloneable?
  @cloneable ||= false
end