aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2012-01-13 18:40:34 -0200
committerSantiago Pastorino <santiago@wyeworks.com>2012-01-13 19:44:44 -0200
commitc159b01b85ac3955c53cd6b8a62d5d90ee973cfb (patch)
tree09c50c90942e50d27b6f05ffc07eec9f1600a9d4 /actionpack
parent4ca3592f86c01f265633038887af9c3c511b4022 (diff)
downloadrails-c159b01b85ac3955c53cd6b8a62d5d90ee973cfb.tar.gz
rails-c159b01b85ac3955c53cd6b8a62d5d90ee973cfb.tar.bz2
rails-c159b01b85ac3955c53cd6b8a62d5d90ee973cfb.zip
Use a BodyProxy instead of including a Module that responds to close.
Closes #4441 if Active Record is disabled assets are delivered correctly
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG.md4
-rw-r--r--actionpack/lib/action_dispatch/middleware/body_proxy.rb28
-rw-r--r--actionpack/lib/action_dispatch/middleware/reloader.rb17
-rw-r--r--actionpack/test/dispatch/reloader_test.rb13
4 files changed, 47 insertions, 15 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 42bd1c6d57..8a4be5667e 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 3.2.0 (unreleased) ##
+* Use a BodyProxy instead of including a Module that responds to
+ close. Closes #4441 if Active Record is disabled assets are delivered
+ correctly *Santiago Pastorino*
+
* Rails initialization with initialize_on_precompile = false should set assets_dir *Santiago Pastorino*
* Add font_path helper method *Santiago Pastorino*
diff --git a/actionpack/lib/action_dispatch/middleware/body_proxy.rb b/actionpack/lib/action_dispatch/middleware/body_proxy.rb
new file mode 100644
index 0000000000..867afe3b48
--- /dev/null
+++ b/actionpack/lib/action_dispatch/middleware/body_proxy.rb
@@ -0,0 +1,28 @@
+# Keep this file meanwhile https://github.com/rack/rack/pull/313 is not released
+module ActionDispatch
+ class BodyProxy
+ def initialize(body, &block)
+ @body, @block, @closed = body, block, false
+ end
+
+ def respond_to?(*args)
+ super or @body.respond_to?(*args)
+ end
+
+ def close
+ return if @closed
+ @closed = true
+ @body.close if @body.respond_to? :close
+ ensure
+ @block.call
+ end
+
+ def closed?
+ @closed
+ end
+
+ def method_missing(*args, &block)
+ @body.__send__(*args, &block)
+ end
+ end
+end
diff --git a/actionpack/lib/action_dispatch/middleware/reloader.rb b/actionpack/lib/action_dispatch/middleware/reloader.rb
index 4f48f1c974..7a5aff214d 100644
--- a/actionpack/lib/action_dispatch/middleware/reloader.rb
+++ b/actionpack/lib/action_dispatch/middleware/reloader.rb
@@ -1,3 +1,5 @@
+require 'action_dispatch/middleware/body_proxy'
+
module ActionDispatch
# ActionDispatch::Reloader provides prepare and cleanup callbacks,
# intended to assist with code reloading during development.
@@ -61,7 +63,7 @@ module ActionDispatch
@validated = @condition.call
prepare!
response = @app.call(env)
- response[2].extend(module_hook)
+ response[2] = ActionDispatch::BodyProxy.new(response[2]) { cleanup! }
response
rescue Exception
cleanup!
@@ -83,18 +85,5 @@ module ActionDispatch
def validated? #:nodoc:
@validated
end
-
- def module_hook #:nodoc:
- middleware = self
- Module.new do
- define_method :close do
- begin
- super() if defined?(super)
- ensure
- middleware.cleanup!
- end
- end
- end
- end
end
end
diff --git a/actionpack/test/dispatch/reloader_test.rb b/actionpack/test/dispatch/reloader_test.rb
index 3411bd14ea..df66e88cb1 100644
--- a/actionpack/test/dispatch/reloader_test.rb
+++ b/actionpack/test/dispatch/reloader_test.rb
@@ -43,6 +43,16 @@ class ReloaderTest < Test::Unit::TestCase
assert_respond_to body, :close
end
+ def test_returned_body_object_always_responds_to_close_even_if_called_twice
+ body = call_and_return_body
+ assert_respond_to body, :close
+ body.close
+
+ body = call_and_return_body
+ assert_respond_to body, :close
+ body.close
+ end
+
def test_condition_specifies_when_to_reload
i, j = 0, 0, 0, 0
Reloader.to_prepare { |*args| i += 1 }
@@ -154,7 +164,8 @@ class ReloaderTest < Test::Unit::TestCase
private
def call_and_return_body(&block)
- @reloader ||= Reloader.new(block || proc {[200, {}, 'response']})
+ @response ||= 'response'
+ @reloader ||= Reloader.new(block || proc {[200, {}, @response]})
@reloader.call({'rack.input' => StringIO.new('')})[2]
end
end