lib/audited/auditor.rb in audited-5.2.0 vs lib/audited/auditor.rb in audited-5.3.0
- old
+ new
@@ -82,10 +82,11 @@
has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: Audited.audit_class.name, inverse_of: :auditable
Audited.audit_class.audited_class_names << to_s
after_create :audit_create if audited_options[:on].include?(:create)
before_update :audit_update if audited_options[:on].include?(:update)
+ after_touch :audit_touch if audited_options[:on].include?(:touch) && ::ActiveRecord::VERSION::MAJOR >= 6
before_destroy :audit_destroy if audited_options[:on].include?(:destroy)
# Define and set after_audit and around_audit callbacks. This might be useful if you want
# to notify a party after the audit has been created or if you want to access the newly-created
# audit.
@@ -228,12 +229,19 @@
end
end
private
- def audited_changes
- all_changes = respond_to?(:changes_to_save) ? changes_to_save : changes
+ def audited_changes(for_touch: false)
+ all_changes = if for_touch
+ previous_changes
+ elsif respond_to?(:changes_to_save)
+ changes_to_save
+ else
+ changes
+ end
+
filtered_changes = \
if audited_options[:only].present?
all_changes.slice(*self.class.audited_columns)
else
all_changes.except(*self.class.non_audited_columns)
@@ -322,10 +330,17 @@
write_audit(action: "update", audited_changes: changes,
comment: audit_comment)
end
end
+ def audit_touch
+ unless (changes = audited_changes(for_touch: true)).empty?
+ write_audit(action: "update", audited_changes: changes,
+ comment: audit_comment)
+ end
+ end
+
def audit_destroy
unless new_record?
write_audit(action: "destroy", audited_changes: audited_attributes,
comment: audit_comment)
end
@@ -472,10 +487,10 @@
protected
def normalize_audited_options
audited_options[:on] = Array.wrap(audited_options[:on])
- audited_options[:on] = [:create, :update, :destroy] if audited_options[:on].empty?
+ audited_options[:on] = [:create, :update, :touch, :destroy] if audited_options[:on].empty?
audited_options[:only] = Array.wrap(audited_options[:only]).map(&:to_s)
audited_options[:except] = Array.wrap(audited_options[:except]).map(&:to_s)
max_audits = audited_options[:max_audits] || Audited.max_audits
audited_options[:max_audits] = Integer(max_audits).abs if max_audits
end