Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -347,25 +347,33 @@ 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)
end
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)
end
end

def audit_destroy
return unless auditing_enabled

unless new_record?
write_audit(action: "destroy", audited_changes: audited_attributes,
comment: audit_comment)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/audited/rspec_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down