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: {}}
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)}
}
association_audit_loggers(model).select{|association| model.send(association).kind_of?(Array)}.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}
}
}
association_audit_loggers(model).select{|association| model.send(association).kind_of?(Array)}.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)}
}
}
}
association_audit_loggers(model).select{|association| model.send(association).kind_of?(Array)}.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}
}
}
association_audit_loggers(model).select{|association| !model.send(association).kind_of?(Array) && !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
|