62
63
64
65
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
|
# File 'lib/models_auditor/audit.rb', line 62
def do_audit_process
return unless ModelsAuditor.config.audit_enabled
return unless is_audit_enabled?
mode = get_audit_mode
options = get_audit_options
store = ModelsAuditor.store
initial_data = ma_get_initial_state(store)
current_data = ma_auditor_get_data
action =
case
when transaction_include_any_action?([:create])
ModelsAuditor::AuditRecord::ACTION_CREATE
when transaction_include_any_action?([:update])
ModelsAuditor::AuditRecord::ACTION_UPDATE
when transaction_include_any_action?([:destroy])
ModelsAuditor::AuditRecord::ACTION_DESTROY
end
bridge =
if options[:bridge]
options[:bridge].each_with_object({}) { |(key, model_name), o| o[key] = {model_name => __send__(key)} }
end
Thread.new do
begin
log_anyway = !ModelsAuditor.config.audit_request_changes_only
if store[:audit_request] || log_anyway
(store[:audit_mutex] ||= Mutex.new).synchronize do
request = store[:audit_request]
body =
case
when AUDIT_SNAPSHOT_MODES.include?(mode)
ma_eliminate_not_changed_keys(initial_data, current_data)
when AUDIT_CHANGES_MODES.include?(mode)
current_data
else
raise ArgumentError.new('Incorrect value of argument audit_type')
end
if request.try(:new_record?) && !request.save
ModelsAuditor.log_error("Couldn't save request record")
ModelsAuditor.log_error(request.errors.full_messages)
return
end
record =
ModelsAuditor::AuditRecord.new(
request: request,
auditable: self,
content: body,
action: action,
bridge: bridge
)
unless record.save
ModelsAuditor.log_error("Couldn't logged changes of #{self.class.name} id: #{self.try(:id)}")
ModelsAuditor.log_error(record.errors.full_messages)
end
end
end
rescue StandardError => e
ModelsAuditor.log_error("Couldn't logged changes of #{self.class.name} id: #{self.try(:id)}")
ModelsAuditor.log_error(e.message)
ModelsAuditor.log_error(e.backtrace.take(100).join("\n"))
end
end
end
|