Skip to content
Browse files

Use just one repository and keep it in the current thread.

  • Loading branch information...
1 parent 87aa913 commit d13df4c6c44a93a3d51d9e6981d69de71f2e4024 @miloops miloops committed Oct 15, 2010
Showing with 6 additions and 16 deletions.
  1. +6 −15 activerecord/lib/active_record/identity_map.rb
  2. +0 −1 activerecord/lib/active_record/test_case.rb
View
21 activerecord/lib/active_record/identity_map.rb
@@ -20,21 +20,14 @@ module IdentityMap
extend ActiveSupport::Concern
class << self
- attr_accessor :repositories
- attr_accessor :current_repository_name
attr_accessor :enabled
- def current
- repositories[current_repository_name] ||= Hash.new { |h,k| h[k] = ActiveSupport::WeakHash.new }
+ def repository
+ Thread.current[:identity_map] ||= Hash.new { |h,k| h[k] = ActiveSupport::WeakHash.new }
end
def with_repository(name = :default)
- old_repository = self.current_repository_name
- self.current_repository_name = name
-
yield if block_given?
- ensure
- self.current_repository_name = old_repository
end
def without
@@ -46,31 +39,29 @@ def without
end
def get(klass, primary_key)
- if obj = current[klass.symbolized_base_class][primary_key]
+ if obj = repository[klass.symbolized_base_class][primary_key]
return obj if obj.id == primary_key && klass == obj.class
end
nil
end
def add(record)
- current[record.class.symbolized_base_class][record.id] = record
+ repository[record.class.symbolized_base_class][record.id] = record
end
def remove(record)
- current[record.class.symbolized_base_class].delete(record.id)
+ repository[record.class.symbolized_base_class].delete(record.id)
end
def clear
- current.clear
+ repository.clear
end
alias enabled? enabled
alias identity_map= enabled=
end
- self.repositories ||= Hash.new
- self.current_repository_name ||= :default
self.enabled = true
module InstanceMethods
View
1 activerecord/lib/active_record/test_case.rb
@@ -6,7 +6,6 @@ class TestCase < ActiveSupport::TestCase #:nodoc:
setup :cleanup_identity_map
def cleanup_identity_map
- ActiveRecord::IdentityMap.current_repository_name = :test
ActiveRecord::Base.identity_map.clear
end

0 comments on commit d13df4c

Please sign in to comment.
Something went wrong with that request. Please try again.