From fa0cf663fe6a6393a3ba117505703e587da4ddc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 29 Jul 2009 14:32:56 +0200 Subject: Add a couple more tests to respond_with. Signed-off-by: Yehuda Katz --- .../lib/action_controller/base/mime_responds.rb | 19 +++++------ actionpack/test/controller/mime_responds_test.rb | 38 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_controller/base/mime_responds.rb b/actionpack/lib/action_controller/base/mime_responds.rb index f7c1b071e7..5a70931941 100644 --- a/actionpack/lib/action_controller/base/mime_responds.rb +++ b/actionpack/lib/action_controller/base/mime_responds.rb @@ -4,7 +4,7 @@ module ActionController #:nodoc: included do class_inheritable_reader :mimes_for_respond_to - respond_to # Set mimes_for_respond_to hash + clear_respond_to end module ClassMethods @@ -31,25 +31,22 @@ module ActionController #:nodoc: # def respond_to(*mimes) options = mimes.extract_options! - mimes_hash = {} only_actions = Array(options.delete(:only)) except_actions = Array(options.delete(:except)) mimes.each do |mime| mime = mime.to_sym - mimes_hash[mime] = {} - mimes_hash[mime][:only] = only_actions unless only_actions.empty? - mimes_hash[mime][:except] = except_actions unless except_actions.empty? + mimes_for_respond_to[mime] = {} + mimes_for_respond_to[mime][:only] = only_actions unless only_actions.empty? + mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty? end - - write_inheritable_hash(:mimes_for_respond_to, mimes_hash) end # Clear all mimes in respond_to. # - def clear_respond_to! - mimes_for_respond_to.each { |k,v| mimes[k] = { :only => [] } } + def clear_respond_to + write_inheritable_attribute(:mimes_for_respond_to, ActiveSupport::OrderedHash.new) end end @@ -185,10 +182,10 @@ module ActionController #:nodoc: resource = options.delete(:with) responder = Responder.new - block.call(responder) if block_given? mimes = collect_mimes_from_class_level if mimes.empty? mimes.each { |mime| responder.send(mime) } + block.call(responder) if block_given? if format = request.negotiate_mime(responder.order) respond_to_block_or_template_or_resource(format, resource, @@ -276,7 +273,7 @@ module ActionController #:nodoc: begin default_render rescue ActionView::MissingTemplate => e - if resource && resource.respond_to?("to_#{format.to_sym}") + if resource && resource.respond_to?(:"to_#{format.to_sym}") render options.merge(format.to_sym => resource) else raise e diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 369d683d23..558fc47695 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -508,6 +508,12 @@ class RespondWithController < ActionController::Base end end + def default_overwritten + respond_to do |format| + format.html { render :text => "HTML" } + end + end + protected def _render_js(js, options) @@ -516,6 +522,17 @@ protected end end +class InheritedRespondWithController < RespondWithController + clear_respond_to + respond_to :xml, :json + + def index + respond_with(RespondResource.new) do |format| + format.json { render :text => "JSON" } + end + end +end + class RespondWithControllerTest < ActionController::TestCase tests RespondWithController @@ -590,6 +607,27 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal "JS", @response.body end + def test_default_overwritten + get :default_overwritten + assert_equal "text/html", @response.content_type + assert_equal "HTML", @response.body + end + + def test_clear_respond_to + @controller = InheritedRespondWithController.new + @request.accept = "text/html" + get :index + assert_equal 406, @response.status + end + + def test_first_in_respond_to_has_higher_priority + @controller = InheritedRespondWithController.new + @request.accept = "*/*" + get :index + assert_equal "application/xml", @response.content_type + assert_equal "XML", @response.body + end + def test_not_acceptable @request.accept = "application/xml" get :using_defaults -- cgit v1.2.3