Module: Core::Models::Concerns::Historizable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Core::Models::Campaigns::Invitation
- Defined in:
- lib/core/models/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.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#add_history(field:, from:, to:) ⇒ Object
Adds an entry in the history table for the given field.
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.
28 29 30 31 32 33 34 |
# File 'lib/core/models/concerns/historizable.rb', line 28 def add_history(field:, from:, to:) return if from == to return if !history.empty? && history.order_by(:created_at.desc).first.to != from event = Core::Models::Event.create(field: field, from: from, to: to, document: self) event.save end |