Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a couple more tests to respond_with.

Signed-off-by: Yehuda Katz <wycats@gmail.com>
  • Loading branch information...
commit fa0cf663fe6a6393a3ba117505703e587da4ddc5 1 parent 09de34c
@josevalim josevalim authored wycats committed
View
19 actionpack/lib/action_controller/base/mime_responds.rb
@@ -4,7 +4,7 @@ module MimeResponds #: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 ClassMethods
#
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 @@ def respond_to(*mimes, &block)
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 @@ def respond_to_block_or_template_or_resource(format, resource, options)
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
View
38 actionpack/test/controller/mime_responds_test.rb
@@ -508,6 +508,12 @@ def using_resource_with_options
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 @@ def _render_js(js, options)
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 @@ def test_using_resource_with_options
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
Please sign in to comment.
Something went wrong with that request. Please try again.