Skip to content

Commit

Permalink
Merge branch 'hotfix/audit_commit_validations' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkalmer committed Feb 5, 2011
2 parents 89ce32c + a5605c2 commit f9547b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/acts_as_audited/auditor.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions spec/acts_as_audited_spec.rb
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f9547b5

Please sign in to comment.