Module: Memento::ActiveRecordMethods::ClassMethods

Defined in:
lib/memento/active_record_methods.rb

Instance Method Summary collapse

Instance Method Details

#memento_changes(*action_types) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/memento/active_record_methods.rb', line 15

def memento_changes(*action_types)
  if defined?(@memento_initialized) && @memento_initialized
    raise "Memento initialized twice. Use memento_changes only once per model"
  end

  @memento_initialized = true

  include InstanceMethods

  self.memento_options = action_types.last.is_a?(Hash) ? action_types.pop : {}

  action_types = Memento::Action::Base.action_types if action_types.empty?
  action_types.map!(&:to_s).uniq!
  unless (invalid = action_types - Memento::Action::Base.action_types).empty?
    raise ArgumentError.new("Invalid memento_changes: #{invalid.to_sentence}; allowed are only #{Memento::Action::Base.action_types.to_sentence}")
  end

  action_types.each do |action_type|
    send :"after_#{action_type}", :"record_#{action_type}"
  end

  has_many :memento_states, :class_name => "Memento::State", :as => :record
end

#memento_optionsObject



39
40
41
# File 'lib/memento/active_record_methods.rb', line 39

def memento_options
  self.memento_options_store ||= {}
end

#memento_options=(options) ⇒ Object



43
44
45
46
47
# File 'lib/memento/active_record_methods.rb', line 43

def memento_options=(options)
  options.symbolize_keys!
  options[:ignore] = [options[:ignore]].flatten.map(&:to_sym) if options[:ignore]
  self.memento_options_store = memento_options_store.merge(options)
end