lib/audited/auditor.rb in audited-4.0.0 vs lib/audited/auditor.rb in audited-4.2.0
- old
+ new
@@ -137,11 +137,11 @@
protected
def revision_with(attributes)
self.dup.tap do |revision|
revision.id = id
- revision.send :instance_variable_set, '@attributes', self.attributes
+ revision.send :instance_variable_set, '@attributes', self.attributes if rails_below?('4.2.0')
revision.send :instance_variable_set, '@new_record', self.destroyed?
revision.send :instance_variable_set, '@persisted', !self.destroyed?
revision.send :instance_variable_set, '@readonly', false
revision.send :instance_variable_set, '@destroyed', false
revision.send :instance_variable_set, '@_destroyed', false
@@ -160,10 +160,14 @@
end
end
end
end
+ def rails_below?(rails_version)
+ Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new(rails_version)
+ end
+
private
def audited_changes
changed_attributes.except(*non_audited_columns).inject({}) do |changes,(attr, old_value)|
changes[attr] = [old_value, self[attr]]
@@ -195,11 +199,11 @@
end
end
def audit_destroy
write_audit(:action => 'destroy', :audited_changes => audited_attributes,
- :comment => audit_comment)
+ :comment => audit_comment) unless self.new_record?
end
def write_audit(attrs)
attrs[:associated] = self.send(audit_associated_with) unless audit_associated_with.nil?
self.audit_comment = nil
@@ -232,13 +236,15 @@
#
# Foo.without_auditing do
# @foo.save
# end
#
- def without_auditing(&block)
+ def without_auditing
auditing_was_enabled = auditing_enabled
disable_auditing
- block.call.tap { enable_auditing if auditing_was_enabled }
+ yield
+ ensure
+ enable_auditing if auditing_was_enabled
end
def disable_auditing
self.auditing_enabled = false
end