Browse files

Use block syntax in IdentityMap middleware.

  • Loading branch information...
1 parent 3a34ae0 commit 08c37b7ed8281bc3e3c1eca98a4859017c89157a @miloops miloops committed Oct 15, 2010
Showing with 16 additions and 3 deletions.
  1. +8 −3 activerecord/lib/active_record/identity_map.rb
  2. +8 −0 activerecord/test/cases/identity_map_test.rb
View
11 activerecord/lib/active_record/identity_map.rb
@@ -27,7 +27,12 @@ def repository
end
def use
+ old, self.enabled = self.enabled, true
+
yield if block_given?
+ ensure
+ self.enabled = old
+ ActiveRecord::IdentityMap.clear
end
def without
@@ -93,9 +98,9 @@ def initialize(app)
end
def call(env)
- @app.call(env)
- ensure
- ActiveRecord::IdentityMap.clear
+ ActiveRecord::IdentityMap.use do
+ @app.call(env)
+ end
end
end
end
View
8 activerecord/test/cases/identity_map_test.rb
@@ -39,6 +39,14 @@ def test_find_id_without_identity_map
end
end
+ def test_find_id_use_identity_map
+ ActiveRecord::IdentityMap.enabled = false
+ ActiveRecord::IdentityMap.use do
+ assert_same(Client.find(3), Client.find(3))
+ end
+ ActiveRecord::IdentityMap.enabled = true
+ end
+
def test_find_pkey
assert_same(
Subscriber.find('swistak'),

0 comments on commit 08c37b7

Please sign in to comment.