aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2012-03-06 00:49:47 -0800
committerJosé Valim <jose.valim@gmail.com>2012-03-06 00:49:47 -0800
commitf97e3ed1117940f1ec85348ab9a7a092316fea29 (patch)
tree085a49b31139cb0fcab5b47159017430107e0a10
parent9beb0b6afc5559ff75c3134db3a8a7884de90e2e (diff)
parent284041cf91a3b2c2d041e71e86fec467080ea9be (diff)
downloadrails-f97e3ed1117940f1ec85348ab9a7a092316fea29.tar.gz
rails-f97e3ed1117940f1ec85348ab9a7a092316fea29.tar.bz2
rails-f97e3ed1117940f1ec85348ab9a7a092316fea29.zip
Merge pull request #5298 from sikachu/master-fix-responder
Always passing a respond block from to responder
-rw-r--r--actionpack/lib/action_controller/metal/mime_responds.rb12
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb13
-rw-r--r--actionpack/test/controller/mime_responds_test.rb36
3 files changed, 49 insertions, 12 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb
index cb59af4f85..b423807f91 100644
--- a/actionpack/lib/action_controller/metal/mime_responds.rb
+++ b/actionpack/lib/action_controller/metal/mime_responds.rb
@@ -235,16 +235,8 @@ module ActionController #:nodoc:
if collector = retrieve_collector_from_mimes(&block)
options = resources.size == 1 ? {} : resources.extract_options!
-
- if defined_response = collector.response
- if action = options.delete(:action)
- render :action => action
- else
- defined_response.call
- end
- else
- (options.delete(:responder) || self.class.responder).call(self, resources, options)
- end
+ options[:default_response] = collector.response
+ (options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index ccda01ed44..1e8990495c 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -130,6 +130,7 @@ module ActionController #:nodoc:
@resources = resources
@options = options
@action = options.delete(:action)
+ @default_response = options.delete(:default_response)
end
delegate :head, :render, :redirect_to, :to => :controller
@@ -172,7 +173,7 @@ module ActionController #:nodoc:
# responds to :to_format and display it.
#
def to_format
- if get? || !has_errors?
+ if get? || !has_errors? || response_overridden?
default_render
else
display_errors
@@ -226,7 +227,11 @@ module ActionController #:nodoc:
# controller.
#
def default_render
- controller.default_render(options)
+ if @default_response
+ @default_response.call(options)
+ else
+ controller.default_render(options)
+ end
end
# Display is just a shortcut to render a resource with the current format.
@@ -274,5 +279,9 @@ module ActionController #:nodoc:
def json_resource_errors
{:errors => resource.errors}
end
+
+ def response_overridden?
+ @default_response.present?
+ end
end
end
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index ae368842b5..f618d4889d 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -1183,3 +1183,39 @@ class MimeControllerLayoutsTest < ActionController::TestCase
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
end
end
+
+class FlashResponder < ActionController::Responder
+ def initialize(controller, resources, options={})
+ super
+ end
+
+ def to_html
+ controller.flash[:notice] = 'Success'
+ super
+ end
+end
+
+class FlashResponderController < ActionController::Base
+ self.responder = FlashResponder
+ respond_to :html
+
+ def index
+ respond_with Object.new do |format|
+ format.html { render :text => 'HTML' }
+ end
+ end
+end
+
+class FlashResponderControllerTest < ActionController::TestCase
+ tests FlashResponderController
+
+ def test_respond_with_block_executed
+ get :index
+ assert_equal 'HTML', @response.body
+ end
+
+ def test_flash_responder_executed
+ get :index
+ assert_equal 'Success', flash[:notice]
+ end
+end