Permalink
Browse files

adding a callback for after_audit actions

  • Loading branch information...
nilakanta committed Nov 22, 2011
1 parent 931f0be commit 4057d6381f25053d5afbba9f7cb5c3f747e0972b
Showing with 23 additions and 1 deletion.
  1. +3 −1 lib/acts_as_audited/auditor.rb
  2. +10 −0 spec/acts_as_audited_spec.rb
  3. +10 −0 spec/spec_models.rb
@@ -89,6 +89,8 @@ def acts_as_audited(options = {})
include ActsAsAudited::Auditor::InstanceMethods
self.auditing_enabled = true
+ define_callbacks :audit
+ set_callback :audit, :after, :after_audit, :if => lambda { self.respond_to?(:after_audit) }
end
def has_associated_audits
@@ -214,7 +216,7 @@ def audit_destroy
def write_audit(attrs)
attrs[:associated] = self.send(audit_associated_with) unless audit_associated_with.nil?
self.audit_comment = nil
- self.audits.create attrs if auditing_enabled
+ run_callbacks(:audit) { self.audits.create(attrs) } if auditing_enabled
end
def require_comment
@@ -479,4 +479,14 @@ class Secret < ActiveRecord::Base
end
end
+ describe "after_audit" do
+ let( :user ) { user = UserWithAfterAudit.new }
+
+ it "should invoke after_audit callback on create" do
+ user.bogus_attr.should == nil
+ user.save.should == true
+ user.bogus_attr.should == "do something"
+ end
+ end
+
end
View
@@ -32,6 +32,16 @@ class NoAttributeProtectionUser < ActiveRecord::Base
acts_as_audited
end
+class UserWithAfterAudit < ActiveRecord::Base
+ set_table_name :users
+ acts_as_audited
+ attr_accessor :bogus_attr
+
+ def after_audit
+ self.bogus_attr = "do something"
+ end
+end
+
class Company < ActiveRecord::Base
acts_as_audited
end

0 comments on commit 4057d63

Please sign in to comment.