Module: Arkaan::Concerns::Historizable

Extended by:
ActiveSupport::Concern
Included in:
Arkaan::Campaigns::Invitation
Defined in:
lib/arkaan/concerns/historizable.rb

Overview

This module is what I call “a beautiful piece of Ruby engineering” It takes any mongoid field that you may have declared, and historizes it in a dedicated relation if this relation does not already exists.

What it does exactly :

  • Creates the :history relation if it does not already exists.

  • Creates a method to check for changes of a specific attribute.

  • Check for changes at initialization to insert the first value.

  • Check for changes at update/save to store the history.

Author:

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_history(field:, from:, to:) ⇒ Object

Adds an entry in the history table for the given field. It checks several things to make the history entry valid :

  • the new value is different from the old value

  • the old value is identical to the last recorded new value.

Parameters:

  • field (String)

    the name of the field to historize

  • from (Any)

    the old value before update

  • to (Any)

    the new value after update.



27
28
29
30
31
32
33
# File 'lib/arkaan/concerns/historizable.rb', line 27

def add_history(field:, from:, to:)
  return if from == to
  return if !history.empty? && history.order_by(:created_at.desc).first.to != from

  event = Arkaan::Event.create(field: field, from: from, to: to, document: self)
  event.save
end