Skip to content

Commit

Permalink
Merge branch 'reset_audited_user_when_yield_raises_exception' of http…
Browse files Browse the repository at this point in the history
…s:https://github.com/flugsio/audited into flugsio-reset_audited_user_when_yield_raises_exception

* 'reset_audited_user_when_yield_raises_exception' of https://github.com/flugsio/audited:
  Reset audited_user when yield raises exception
  • Loading branch information
bryckbost committed May 1, 2013
2 parents 1f3c82c + 8a52b0f commit 5845910
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ def audited_classes
# for background operations that require audit information.
def as_user(user, &block)
Thread.current[:audited_user] = user

yieldval = yield

yield
ensure
Thread.current[:audited_user] = nil

yieldval
end

# @private
Expand Down
9 changes: 9 additions & 0 deletions spec/audited/adapters/active_record/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ class Models::ActiveRecord::CustomUserSubclass < Models::ActiveRecord::CustomUse
end.should == 42
end

it "should reset audited_user when the yield block raises an exception" do
expect {
Audited.audit_class.as_user('foo') do
raise StandardError
end
}.to raise_exception
Thread.current[:audited_user].should be_nil
end

end

describe "mass assignment" do
Expand Down
9 changes: 9 additions & 0 deletions spec/audited/adapters/mongo_mapper/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ class Models::MongoMapper::CustomUserSubclass < Models::MongoMapper::CustomUser
end.should == 42
end

it "should reset audited_user when the yield block raises an exception" do
expect {
Audited.audit_class.as_user('foo') do
raise StandardError
end
}.to raise_exception
Thread.current[:audited_user].should be_nil
end

end

end

0 comments on commit 5845910

Please sign in to comment.