Permalink
Browse files

Merge branch 'release/0.3.2'

  • Loading branch information...
2 parents 62c1612 + 168d1f1 commit d30b45c378da5e93d397fb084aca6834aadc7c94 bokmann committed Jan 18, 2011
Showing with 29 additions and 4 deletions.
  1. +1 −1 VERSION
  2. +8 −3 lib/sentient_user.rb
  3. +20 −0 test/test_sentient_user.rb
View
@@ -1 +1 @@
-0.3.1
+0.3.2
View
@@ -22,9 +22,14 @@ def current?
def self.do_as(user, &block)
old_user = self.current
- self.current = user
- response = block.call unless block.nil?
- self.current = old_user
+
+ begin
+ self.current = user
+ response = block.call unless block.nil?
+ ensure
+ self.current = old_user
+ end
+
response
end
}
View
@@ -26,4 +26,24 @@ class TestSentientUser < Test::Unit::TestCase
should "allow subclasses of user to be assigned to user.current" do
User.current = AnonymousUser.new
end
+
+ should "allow execution of a block as a specified user" do
+ p, p2 = Person.new, Person.new
+ p.make_current
+ Person.do_as(p2) { assert_equal Person.current, p2 }
+ end
+
+ should "should reset the original user after executing a block as a specified user" do
+ p, p2 = Person.new, Person.new
+ p.make_current
+
+ Person.do_as(p2) { }
+ assert_equal Person.current, p
+
+ begin
+ Person.do_as(p2) { raise "error" }
+ rescue
+ assert_equal Person.current, p
+ end
+ end
end

0 comments on commit d30b45c

Please sign in to comment.