Browse files

Support Rails streaming in middleware.

[ fix #2445 ]
  • Loading branch information...
1 parent 26637a1 commit c63140b882620f9e2868ac17a4edf9b0faf7a8e7 @durran durran committed Oct 22, 2012
View
3 CHANGELOG.md
@@ -138,6 +138,9 @@ For instructions on upgrading to newer versions, visit
* \#2454 `Model#as_document` properly allows changes from having a relation to
the relation being removed. (James Almond)
+* \#2445 Mongoid middleware now properly supports both normal and streamed
+ responses and properly clears the identity map for either.
+
## 3.0.9
### Resolved Issues
View
13 lib/rack/mongoid/middleware/identity_map.rb
@@ -4,13 +4,14 @@ module Mongoid
module Middleware
# This middleware contains the behaviour needed to properly use the
- # identity map in Rack based applications.
+ # identity map in Rack based applications. This middleware will properly
+ # handle Rails or Rack streaming responses.
class IdentityMap
# Initialize the new middleware.
#
# @example Init the middleware.
- # IdentityMap.new(app)
+ # IdentityMap.new(app)
#
# @param [ Object ] app The application.
#
@@ -22,15 +23,19 @@ def initialize(app)
# Make the request with the provided environment.
#
# @example Make the request.
- # identity_map.call(env)
+ # identity_map.call(env)
#
# @param [ Object ] env The environment.
#
# @return [ Array ] The status, headers, and response.
#
# @since 2.1.0
def call(env)
- ::Mongoid.unit_of_work { @app.call(env) }
+ response = @app.call(env)
+ response[2] = ::Rack::BodyProxy.new(response[2]) do
+ ::Mongoid::IdentityMap.clear
+ end
+ response
end
end
end
View
44 lib/rack/mongoid/middleware/streaming/identity_map.rb
@@ -1,44 +0,0 @@
-# encoding: utf-8
-module Rack
- module Mongoid
- module Middleware
- module Streaming
-
- # This middleware contains the behaviour needed to properly use the
- # identity map in Rack based applications.
- class IdentityMap
-
- # Initialize the new middleware.
- #
- # @example Init the middleware.
- # IdentityMap.new(app)
- #
- # @param [ Object ] app The application.
- #
- # @since 2.1.0
- def initialize(app)
- @app = app
- end
-
- # Make the request with the provided environment.
- #
- # @example Make the request.
- # identity_map.call(env)
- #
- # @param [ Object ] env The environment.
- #
- # @return [ Array ] The status, headers, and response.
- #
- # @since 2.1.0
- def call(env)
- response = @app.call(env)
- response[2] = ::Rack::BodyProxy.new(response[2]) do
- ::Mongoid::IdentityMap.clear
- end
- response
- end
- end
- end
- end
- end
-end
View
12 spec/rack/mongoid/middleware/identity_map_spec.rb
@@ -20,6 +20,14 @@
describe "#call" do
+ module Rack
+ class BodyProxy
+ def initialize(response, &block)
+ @response, @block = response, block
+ end
+ end
+ end
+
let(:middleware) do
klass.new(app)
end
@@ -42,8 +50,8 @@
middleware.call(env)
end
- it "returns the call" do
- result.should be_empty
+ it "returns the call with the body proxy" do
+ result[2].should be_a(Rack::BodyProxy)
end
it "clears out the identity map" do

0 comments on commit c63140b

Please sign in to comment.