Class: AuditedModelsObserver
- Inherits:
-
ActiveRecord::Observer
- Object
- ActiveRecord::Observer
- AuditedModelsObserver
- Defined in:
- lib/audit_log/observer.rb
Defined Under Namespace
Classes: WhatBuilder
Instance Attribute Summary collapse
-
#controller ⇒ Object
Returns the value of attribute controller.
Class Method Summary collapse
Instance Method Summary collapse
- #after_create(model) ⇒ Object
- #after_update(model) ⇒ Object
- #before_destroy(model) ⇒ Object
- #before_update(model) ⇒ Object
- #before_validation(model) ⇒ Object
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
3 4 5 |
# File 'lib/audit_log/observer.rb', line 3 def controller @controller end |
Class Method Details
.observed_classes ⇒ Object
5 6 7 |
# File 'lib/audit_log/observer.rb', line 5 def self.observed_classes AuditLog::Mapping.instance.audit_mappings.keys.collect{|model_as_symbol| model_as_symbol.to_s.camelize.constantize} end |
Instance Method Details
#after_create(model) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/audit_log/observer.rb', line 14 def after_create(model) if self.controller && self.controller.audited_model == model logged_model = LoggedModel.new( who: self.controller.current_user_for_audit_log ? self.controller.current_user_for_audit_log.id : nil, what: {id: model.id, event: :create}, model_class_name: model.class.name, model_id: model.id ) logged_model.save end end |
#after_update(model) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/audit_log/observer.rb', line 46 def after_update(model) if self.controller && self.controller.audited_model == model changes = Thread.current[:audited_model_changes] if changes what = WhatBuilder.new(changes).build logged_model = LoggedModel.new( who: self.controller.current_user_for_audit_log ? self.controller.current_user_for_audit_log.id : nil, what: what, model_class_name: model.class.name, model_id: model.id ) logged_model.save end self.controller.audited_model = nil end end |
#before_destroy(model) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/audit_log/observer.rb', line 27 def before_destroy(model) set_model_to_audit(model) if self.controller && self.controller.audited_model == model what = {id: model.id, event: :destroy} what = what.merge(ActiveSupport::JSON.decode(model.to_json(root: false, include: association_audit_loggers(model)))) logged_model = LoggedModel.new( who: self.controller.current_user_for_audit_log ? self.controller.current_user_for_audit_log.id : nil, what: what, model_class_name: model.class.name, model_id: model.id ) logged_model.save self.controller.audited_model = nil end end |
#before_update(model) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/audit_log/observer.rb', line 66 def before_update(model) if self.controller && self.controller.audited_model == model if (model.changed? && !(model.changed_attributes.keys.collect{|attr| attr.to_sym}.uniq.sort - ignored_fields(model).uniq.sort).empty?) || has_some_association_changed?(model) changes = {model: model, fields_updates: {}, has_many: {}, has_one: {}} # build main model changes model.changed_attributes. select{|attribute| attribute != "updated_at" && !ignored_fields(model).include?(attribute.to_sym)}. each{|attribute, old_value| changes[:fields_updates][attribute.to_sym] = {from: old_value, to: model.send(attribute.to_sym)} } # build has_many model creations association_audit_loggers(model).select{|association| model.send(association).kind_of?(ActiveRecord::Associations::CollectionProxy)}.each{|method_name| changes[:has_many][method_name.to_sym] ||= [] model.send(method_name).select{|a| a.new_record?}.each{|nested| changes[:has_many][method_name.to_sym] << {model: nested, event: :create} } } # build has_many model updates association_audit_loggers(model).select{|association| model.send(association).kind_of?(ActiveRecord::Associations::CollectionProxy)}.each{|method_name| changes[:has_many][method_name.to_sym] ||= [] model.send(method_name).select{|a| a.changed? && !a.new_record?}.each{|nested| nested_changes = {model: nested, fields_updates: {}, event: :update} changes[:has_many][method_name.to_sym] << nested_changes nested.changed_attributes. select{|attribute| attribute != "updated_at"}. each{|attribute, old_value| nested_changes[:fields_updates][attribute.to_sym] = {from: old_value, to: nested.send(attribute.to_sym)} } } } # build has_many model deletions association_audit_loggers(model).select{|association| model.send(association).kind_of?(ActiveRecord::Associations::CollectionProxy)}.each{|method_name| changes[:has_many][method_name.to_sym] ||= [] model.send(method_name).select{|a| a.marked_for_destruction?}.each{|nested| changes[:has_many][method_name.to_sym] << {model: nested, event: :destroy} } } # build has_one model creations association_audit_loggers(model).select{|association| !model.send(association).kind_of?(ActiveRecord::Associations::CollectionProxy) && !model.send(association).nil?}.each{|method_name| has_one_model = model.send(method_name) if has_one_model.new_record? changes[:has_one][method_name.to_sym] = {model: has_one_model, event: :create} elsif has_one_model.changed? has_one_changes = {model: has_one_model, event: :update, fields_updates: {}} has_one_model.changed_attributes. select{|attribute| attribute != "updated_at"}. each{|attribute, old_value| has_one_changes[:fields_updates][attribute.to_sym] = {from: old_value, to: has_one_model.send(attribute.to_sym)} } changes[:has_one][method_name.to_sym] = has_one_changes elsif has_one_model.marked_for_destruction? changes[:has_one][method_name.to_sym] = {model: has_one_model, event: :destroy} end } Thread.current[:audited_model_changes] = changes end end end |
#before_validation(model) ⇒ Object
10 11 12 |
# File 'lib/audit_log/observer.rb', line 10 def before_validation(model) set_model_to_audit(model) end |