Permalink
Browse files

Noop update audit records without audited changes. closes #3

  • Loading branch information...
1 parent 8d313a2 commit dfff4a12a472702c09f809b6afd91fe61919a241 @kunklejr committed Jul 5, 2011
Showing with 15 additions and 4 deletions.
  1. +6 −4 lib/auditor/recorder.rb
  2. +9 −0 spec/auditable_spec.rb
View
@@ -24,8 +24,11 @@ def audit(model, action)
audit = Audit.new
audit.auditable_id = model.id
audit.auditable_type = model.class.name
- audit.audited_changes = prepare_changes(model.changes) if changes_available?(action)
+ audit.audited_changes = prepare_changes(model.changes) if model.changed?
audit.action = action
+
+ return if noop?(audit)
+
audit.comment = @blk.call(model, user, action) if @blk
without_auditing do
@@ -44,9 +47,8 @@ def prepare_changes(changes)
chg.empty? ? nil : chg
end
- def changes_available?(action)
- [:create, :update].include?(action)
+ def noop?(audit)
+ audit.action == :update && !audit.audited_changes.present?
end
-
end
end
View
@@ -97,6 +97,15 @@
m.attributes_at(ts4).should == {'name' => '2', 'id' => m.id}
end
+ it 'should not save an update record that does not have any audited changes' do
+ redefine_model { audit!(:update, :except => :name) }
+
+ lambda {
+ m = Model.create(:name => 'new')
+ m.update_attributes({:name => 'newer'})
+ }.should_not change(Audit, :count)
+ end
+
def verify_audit(audit, model, changes=nil)
audit.should_not be_nil
audit.auditable.should == model unless audit.action == 'destroy'

0 comments on commit dfff4a1

Please sign in to comment.