Class: RailsAuditLog::AuditLogEntry
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- RailsAuditLog::AuditLogEntry
- Defined in:
- app/models/rails_audit_log/audit_log_entry.rb
Constant Summary collapse
- EVENTS =
%w[create update destroy].freeze
Instance Method Summary collapse
- #changed_attributes ⇒ Object
- #diff ⇒ Object
- #next ⇒ Object
- #previous ⇒ Object
-
#reify ⇒ Object
Instance methods.
Instance Method Details
#changed_attributes ⇒ Object
75 76 77 |
# File 'app/models/rails_audit_log/audit_log_entry.rb', line 75 def changed_attributes object_changes&.keys || [] end |
#diff ⇒ Object
79 80 81 82 83 |
# File 'app/models/rails_audit_log/audit_log_entry.rb', line 79 def diff return {} unless object_changes object_changes.transform_values { |from_to| { from: from_to[0], to: from_to[1] } } end |
#next ⇒ Object
71 72 73 |
# File 'app/models/rails_audit_log/audit_log_entry.rb', line 71 def next self.class.where(item_type: item_type, item_id: item_id).where("id > ?", id).order(id: :asc).first end |
#previous ⇒ Object
67 68 69 |
# File 'app/models/rails_audit_log/audit_log_entry.rb', line 67 def previous self.class.where(item_type: item_type, item_id: item_id).where("id < ?", id).order(id: :desc).first end |
#reify ⇒ Object
Instance methods
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/rails_audit_log/audit_log_entry.rb', line 41 def reify return nil if event == "create" klass = item_type.constantize if object.present? instance = klass.new instance.assign_attributes(object.except("id")) instance.id = object.fetch("id") { item_id } return instance end # Fallback: diff-only mode or entries recorded before snapshot support from_attrs = (object_changes || {}).transform_values { |from_to| from_to[0] } if event == "update" record = klass.find_by(id: item_id) from_attrs = record.attributes.merge(from_attrs) if record end instance = klass.new instance.assign_attributes(from_attrs.except("id")) instance.id = from_attrs.fetch("id") { item_id } instance end |