diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index a164f72a..88f8a770 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -79,7 +79,7 @@ def set_audit(options) set_audited_options(options) if audited_options[:comment_required] - validate :presence_of_audit_comment + validate :validate_presence_of_audit_comment before_destroy :require_comment if audited_options[:on].include?(:destroy) end @@ -347,11 +347,15 @@ def audits_to(version = nil) end def audit_create + return unless auditing_enabled + write_audit(action: "create", audited_changes: audited_attributes, comment: audit_comment) end def audit_update + return unless auditing_enabled + unless (changes = audited_changes(exclude_readonly_attrs: true)).empty? && (audit_comment.blank? || audited_options[:update_with_comment_only] == false) write_audit(action: "update", audited_changes: changes, comment: audit_comment) @@ -359,6 +363,8 @@ def audit_update end def audit_touch + return unless auditing_enabled + unless (changes = audited_changes(for_touch: true, exclude_readonly_attrs: true)).empty? write_audit(action: "update", audited_changes: changes, comment: audit_comment) @@ -366,6 +372,8 @@ def audit_touch end def audit_destroy + return unless auditing_enabled + unless new_record? write_audit(action: "destroy", audited_changes: audited_attributes, comment: audit_comment) @@ -375,18 +383,18 @@ def audit_destroy def write_audit(attrs) self.audit_comment = nil - if auditing_enabled - attrs[:associated] = send(audit_associated_with) unless audit_associated_with.nil? + attrs[:associated] = send(audit_associated_with) unless audit_associated_with.nil? - run_callbacks(:audit) { - audit = audits.create(attrs) - combine_audits_if_needed if attrs[:action] != "create" - audit - } - end + run_callbacks(:audit) { + audit = audits.create(attrs) + combine_audits_if_needed if attrs[:action] != "create" + audit + } end - def presence_of_audit_comment + def validate_presence_of_audit_comment + return unless auditing_enabled + if comment_required_state? errors.add(:audit_comment, :blank) unless audit_comment.present? end @@ -420,6 +428,8 @@ def evaluate_max_audits end def require_comment + return unless auditing_enabled + if auditing_enabled && audit_comment.blank? errors.add(:audit_comment, :blank) throw(:abort) diff --git a/lib/audited/rspec_matchers.rb b/lib/audited/rspec_matchers.rb index 1999068c..aebe83b9 100644 --- a/lib/audited/rspec_matchers.rb +++ b/lib/audited/rspec_matchers.rb @@ -132,7 +132,7 @@ def only_audit_on_designated_callbacks? def validate_callbacks_include_presence_of_comment? if @options[:comment_required] && audited_on_create_or_update? - callbacks_for(:validate).include?(:presence_of_audit_comment) + callbacks_for(:validate).include?(:validate_presence_of_audit_comment) else true end diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index 15ff2f77..bda162fe 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -972,6 +972,18 @@ def stub_global_max_audits(max_audits) }.to_not change(Audited::Audit, :count) end + it 'should not perform audit-related queries when touching an audited model' do + user = Models::ActiveRecord::User.create!(name: "Brandon") + + subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| + expect(payload[:sql]).to_not include("audits") + end + + Models::ActiveRecord::User.without_auditing { user.touch } + ensure + ActiveSupport::Notifications.unsubscribe(subscription) + end + context "when global audits are disabled" do it "should re-enable class audits after #without_auditing block" do Audited.auditing_enabled = false