Skip to content

Commit

Permalink
Merge pull request #522 from james/fix/dont-require-comment-if-not-au…
Browse files Browse the repository at this point in the history
…dited-changes

Don't require comment if no audited_changes present
  • Loading branch information
danielmorrison authored May 31, 2021
2 parents 4185076 + e40cb7c commit 8a194ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def presence_of_audit_comment

def comment_required_state?
auditing_enabled &&
audited_changes.present? &&
((audited_options[:on].include?(:create) && new_record?) ||
(audited_options[:on].include?(:update) && persisted? && changed?))
end
Expand Down
8 changes: 8 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ def stub_global_max_audits(max_audits)
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
Models::ActiveRecord::CommentRequiredUser.enable_auditing
end

it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
expect(Models::ActiveRecord::CommentRequiredUser.new(password: 'Foo')).to be_valid
end
end

describe "on update" do
Expand All @@ -969,6 +973,10 @@ def stub_global_max_audits(max_audits)
expect(user.update(name: "Test")).to eq(true)
Models::ActiveRecord::CommentRequiredUser.enable_auditing
end

it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
expect(user.update(password: 'Test')).to eq(true)
end
end

describe "on destroy" do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class UserRedactedPasswordCustomRedaction < ::ActiveRecord::Base

class CommentRequiredUser < ::ActiveRecord::Base
self.table_name = :users
audited comment_required: true
audited except: :password, comment_required: true
end

class OnCreateCommentRequiredUser < ::ActiveRecord::Base
Expand Down

0 comments on commit 8a194ab

Please sign in to comment.