Module: Arkaan::Concerns::Historizable::ClassMethods

Defined in:
lib/arkaan/concerns/historizable.rb

Overview

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

Author:

Instance Method Summary collapse

Instance Method Details

#historization_after_init(field) ⇒ Object



47
48
49
50
51
# File 'lib/arkaan/concerns/historizable.rb', line 47

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

#historization_after_save(field) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/arkaan/concerns/historizable.rb', line 53

def historization_after_save(field)
  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

#historize(field) ⇒ Object

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



41
42
43
44
45
# File 'lib/arkaan/concerns/historizable.rb', line 41

def historize(field)
  embeds_many :history, class_name: 'Arkaan::Event' unless relations.key?('history')
  historization_after_init(field)
  historization_after_save(field)
end