diff options
Diffstat (limited to 'activerecord/lib/active_record/identity_map.rb')
-rw-r--r-- | activerecord/lib/active_record/identity_map.rb | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/identity_map.rb b/activerecord/lib/active_record/identity_map.rb index d18b2b0a54..9eb47ad99f 100644 --- a/activerecord/lib/active_record/identity_map.rb +++ b/activerecord/lib/active_record/identity_map.rb @@ -49,8 +49,18 @@ module ActiveRecord end def get(klass, primary_key) - obj = repository[klass.symbolized_base_class][primary_key] - obj.is_a?(klass) ? obj : nil + record = repository[klass.symbolized_base_class][primary_key] + + if record.is_a?(klass) + ActiveSupport::Notifications.instrument("identity.active_record", + :line => "From Identity Map (id: #{primary_key})", + :name => "#{klass} Loaded", + :connection_id => object_id) + + record + else + nil + end end def add(record) @@ -88,14 +98,33 @@ module ActiveRecord end class Middleware + class Body #:nodoc: + def initialize(target, original) + @target = target + @original = original + end + + def each(&block) + @target.each(&block) + end + + def close + @target.close if @target.respond_to?(:close) + ensure + IdentityMap.enabled = @original + IdentityMap.clear + end + end + def initialize(app) @app = app end def call(env) - ActiveRecord::IdentityMap.use do - @app.call(env) - end + enabled = IdentityMap.enabled + IdentityMap.enabled = true + status, headers, body = @app.call(env) + [status, headers, Body.new(body, enabled)] end end end |