From a5605c2d93a721ae872913037aa72d5b117d9283 Mon Sep 17 00:00:00 2001 From: Kenneth Kalmer Date: Sat, 5 Feb 2011 18:01:45 +0200 Subject: [PATCH] Audit comments should be optional when auditing is disabled --- lib/acts_as_audited/auditor.rb | 4 ++-- spec/acts_as_audited_spec.rb | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/acts_as_audited/auditor.rb b/lib/acts_as_audited/auditor.rb index 9d6c2b0f..c7629523 100644 --- a/lib/acts_as_audited/auditor.rb +++ b/lib/acts_as_audited/auditor.rb @@ -67,7 +67,7 @@ def acts_as_audited(options = {}) write_inheritable_attribute :audit_associated_with, options[:associated_with] if options[:comment_required] - validates_presence_of :audit_comment + validates_presence_of :audit_comment, :if => :auditing_enabled before_destroy :require_comment end @@ -215,7 +215,7 @@ def write_audit(attrs) end def require_comment - if audit_comment.blank? + if auditing_enabled && audit_comment.blank? errors.add(:audit_comment, "Comment required before destruction") return false end diff --git a/spec/acts_as_audited_spec.rb b/spec/acts_as_audited_spec.rb index a2f1e134..bd43c935 100644 --- a/spec/acts_as_audited_spec.rb +++ b/spec/acts_as_audited_spec.rb @@ -32,7 +32,7 @@ :activated => true, :suspended_at => yesterday, :logins => 2) - + u.name.should eq('name') u.username.should eq('username') u.password.should eq('password') @@ -182,7 +182,7 @@ owned_company.update_attribute(:name, 'The Auditors') owned_company.audits.last.association.should == owner end - + it "should store the associated object on destroy" do owned_company.destroy owned_company.audits.last.association.should == owner @@ -382,10 +382,16 @@ it "should validate when audit_comment is supplied" do CommentRequiredUser.new( :audit_comment => 'Create').should be_valid end + + it "should validate when audit_comment is not supplied, and auditing is disabled" do + CommentRequiredUser.disable_auditing + CommentRequiredUser.new.should be_valid + CommentRequiredUser.enable_auditing + end end describe "on update" do - let( :user ) { CommentRequiredUser.create!( :audit_comment => 'Create' )} + let( :user ) { CommentRequiredUser.create!( :audit_comment => 'Create' ) } it "should not validate when audit_comment is not supplied" do user.update_attributes(:name => 'Test').should be_false @@ -394,6 +400,12 @@ it "should validate when audit_comment is supplied" do user.update_attributes(:name => 'Test', :audit_comment => 'Update').should be_true end + + it "should validate when audit_comment is not supplied, and auditing is disabled" do + CommentRequiredUser.disable_auditing + user.update_attributes(:name => 'Test').should be_true + CommentRequiredUser.enable_auditing + end end describe "on destroy" do @@ -407,6 +419,12 @@ user.audit_comment = "Destroy" user.destroy.should == user end + + it "should validate when audit_comment is not supplied, and auditing is disabled" do + CommentRequiredUser.disable_auditing + user.destroy.should == user + CommentRequiredUser.enable_auditing + end end end