1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow for respond_to(:html, :js, :xml) (closes #4277) [Caio Chassot]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3919 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2006-03-18 06:43:37 +00:00
parent 7e46571115
commit 9840dc84f1
2 changed files with 22 additions and 2 deletions

View file

@ -5,9 +5,11 @@ module ActionController #:nodoc:
end end
module InstanceMethods module InstanceMethods
def respond_to(&block) def respond_to(*types, &block)
raise ArgumentError, "respond_to takes either types or a block, never bot" unless types.any? ^ block
block ||= lambda { |responder| types.each { |type| responder.send(type) } }
responder = Responder.new(block.binding) responder = Responder.new(block.binding)
yield responder block.call(responder)
responder.respond responder.respond
end end
end end

View file

@ -40,6 +40,10 @@ class RespondToController < ActionController::Base
end end
end end
def using_defaults_with_type_list
respond_to(:html, :js, :xml)
end
def using_argument_defaults def using_argument_defaults
person_in_xml = { :name => "David" }.to_xml(:root => "person") person_in_xml = { :name => "David" }.to_xml(:root => "person")
respond_to do |type| respond_to do |type|
@ -155,6 +159,20 @@ class MimeControllerTest < Test::Unit::TestCase
assert_equal "<p>Hello world!</p>\n", @response.body assert_equal "<p>Hello world!</p>\n", @response.body
end end
def test_using_defaults_with_type_list
@request.env["HTTP_ACCEPT"] = "*/*"
get :using_defaults_with_type_list
assert_equal 'Hello world!', @response.body
@request.env["HTTP_ACCEPT"] = "text/javascript"
get :using_defaults_with_type_list
assert_equal '$("body").visualEffect("highlight");', @response.body
@request.env["HTTP_ACCEPT"] = "application/xml"
get :using_defaults_with_type_list
assert_equal "<p>Hello world!</p>\n", @response.body
end
def test_using_argument_defaults def test_using_argument_defaults
@request.env["HTTP_ACCEPT"] = "application/xml" @request.env["HTTP_ACCEPT"] = "application/xml"
get :using_argument_defaults get :using_argument_defaults