Module: ActsAsReplaceable::ActMethod

Defined in:
lib/acts_as_replaceable/acts_as_replaceable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_replaceable(options = {}) ⇒ Object

If any before_save methods change the attributes, acts_as_replaceable will not function correctly.

OPTIONS :match => what fields to match against when finding a duplicate :insensitive_match => what fields to do case insensitive matching on. :inherit => what attributes of the existing record overwrite our own attributes



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/acts_as_replaceable/acts_as_replaceable.rb', line 13

def acts_as_replaceable(options = {})
  extend ActsAsReplaceable::ClassMethods
  include ActsAsReplaceable::InstanceMethods

  attr_reader :has_been_replaced
  cattr_accessor :acts_as_replaceable_options

  options.symbolize_keys!
  self.acts_as_replaceable_options = {}
  self.acts_as_replaceable_options[:match] = ActsAsReplaceable::HelperMethods.sanitize_attribute_names(self, options[:match])
  self.acts_as_replaceable_options[:insensitive_match] = ActsAsReplaceable::HelperMethods.sanitize_attribute_names(self, options[:insensitive_match])
  self.acts_as_replaceable_options[:inherit] = ActsAsReplaceable::HelperMethods.sanitize_attribute_names(self, options[:inherit], options[:insensitive_match], :id, :created_at, :updated_at)

  if ActsAsReplaceable.concurrency && !Rails.cache.respond_to?(:increment)
    raise LockingUnavailable, "To run ActsAsReplaceable in concurrency mode, the Rails cache must provide an :increment method that performs an atomic addition to the given key, e.g. Memcached"
  end
end