Module: Core::Models::Concerns::Historizable::ClassMethods

Defined in:
lib/core/models/concerns/historizable.rb

Overview

Submodule holding all the static methods add to the current subclass.

Author:

Instance Method Summary collapse

Instance Method Details

#historize(field) ⇒ Object

Takes the Mongoid declared field and creates the callbacks to intercept any value change and add it to the history.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/core/models/concerns/historizable.rb', line 43

def historize(field)

  unless relations.key?('history')
    embeds_many :history, class_name: 'Core::Models::Event'
  end

  after_initialize do |doc|
    add_history(field: field.name, from: nil, to: doc[field.name])
  end

  after_save do |doc|
    if doc.changed_attributes.key?(field.name)
      from = doc.changed_attributes[field.name]
      add_history(field: field.name, from: from, to: doc[field.name])
    end
  end
end