mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added defaults to respond_to [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3842 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
6e3e5cadfb
commit
de660957a5
8 changed files with 79 additions and 20 deletions
|
@ -7,7 +7,7 @@
|
||||||
@posts = Post.find :all
|
@posts = Post.find :all
|
||||||
|
|
||||||
respond_to do |type|
|
respond_to do |type|
|
||||||
type.html { render } # renders weblog/index.rhtml
|
type.html # using defaults, which will render weblog/index.rhtml
|
||||||
type.xml { render :action => "index.rxml" }
|
type.xml { render :action => "index.rxml" }
|
||||||
type.js { render :action => "index.rjs" }
|
type.js { render :action => "index.rjs" }
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,13 @@ module ActionController #:nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
class Responder #:nodoc:
|
class Responder #:nodoc:
|
||||||
|
DEFAULT_BLOCKS = {
|
||||||
|
:html => 'Proc.new { render }',
|
||||||
|
:js => 'Proc.new { render :action => "#{action_name}.rjs" }',
|
||||||
|
:xml => 'Proc.new { render :action => "#{action_name}.rxml" }',
|
||||||
|
:xml_arg => 'Proc.new { render :xml => __mime_responder_arg__ }'
|
||||||
|
}
|
||||||
|
|
||||||
def initialize(block_binding)
|
def initialize(block_binding)
|
||||||
@block_binding = block_binding
|
@block_binding = block_binding
|
||||||
@mime_type_priority = eval("request.accepts", block_binding)
|
@mime_type_priority = eval("request.accepts", block_binding)
|
||||||
|
@ -22,9 +29,19 @@ module ActionController #:nodoc:
|
||||||
|
|
||||||
for mime_type in %w( all html js xml rss atom yaml )
|
for mime_type in %w( all html js xml rss atom yaml )
|
||||||
eval <<-EOT
|
eval <<-EOT
|
||||||
def #{mime_type}(&block)
|
def #{mime_type}(argument = nil, &block)
|
||||||
@order << Mime::#{mime_type.upcase}
|
@order << Mime::#{mime_type.upcase}
|
||||||
@responses[Mime::#{mime_type.upcase}] = block
|
|
||||||
|
if block_given?
|
||||||
|
@responses[Mime::#{mime_type.upcase}] = block
|
||||||
|
else
|
||||||
|
if argument
|
||||||
|
eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding)
|
||||||
|
@responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[(Mime::#{mime_type.upcase}.to_sym.to_s + "_arg").to_sym], @block_binding)
|
||||||
|
else
|
||||||
|
@responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[Mime::#{mime_type.upcase}.to_sym], @block_binding)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
EOT
|
EOT
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,12 +31,30 @@ class RespondToController < ActionController::Base
|
||||||
type.xml { render :text => "XML" }
|
type.xml { render :text => "XML" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def using_defaults
|
||||||
|
respond_to do |type|
|
||||||
|
type.html
|
||||||
|
type.js
|
||||||
|
type.xml
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def using_argument_defaults
|
||||||
|
person_in_xml = { :name => "David" }.to_xml(:root => "person")
|
||||||
|
respond_to do |type|
|
||||||
|
type.html
|
||||||
|
type.xml(person_in_xml)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def rescue_action(e)
|
def rescue_action(e)
|
||||||
raise unless ActionController::MissingTemplate === e
|
raise unless ActionController::MissingTemplate === e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RespondToController.template_root = File.dirname(__FILE__) + "/../fixtures/"
|
||||||
|
|
||||||
class MimeControllerTest < Test::Unit::TestCase
|
class MimeControllerTest < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@request = ActionController::TestRequest.new
|
@request = ActionController::TestRequest.new
|
||||||
|
@ -99,4 +117,24 @@ class MimeControllerTest < Test::Unit::TestCase
|
||||||
get :just_xml
|
get :just_xml
|
||||||
assert_equal 'XML', @response.body
|
assert_equal 'XML', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_using_defaults
|
||||||
|
@request.env["HTTP_ACCEPT"] = "*/*"
|
||||||
|
get :using_defaults
|
||||||
|
assert_equal 'Hello world!', @response.body
|
||||||
|
|
||||||
|
@request.env["HTTP_ACCEPT"] = "text/javascript"
|
||||||
|
get :using_defaults
|
||||||
|
assert_equal "$('body').visualEffect(\"highlight\");", @response.body
|
||||||
|
|
||||||
|
@request.env["HTTP_ACCEPT"] = "application/xml"
|
||||||
|
get :using_defaults
|
||||||
|
assert_equal "<p>Hello world!</p>\n", @response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_using_argument_defaults
|
||||||
|
@request.env["HTTP_ACCEPT"] = "application/xml"
|
||||||
|
get :using_argument_defaults
|
||||||
|
assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>David</name>\n</person>\n", @response.body
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -184,6 +184,14 @@ class NewRenderTestController < ActionController::Base
|
||||||
render :action => "potential_conflicts"
|
render :action => "potential_conflicts"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def hello_world_from_rxml_using_action
|
||||||
|
render :action => "hello_world.rxml"
|
||||||
|
end
|
||||||
|
|
||||||
|
def hello_world_from_rxml_using_template
|
||||||
|
render :template => "test/hello_world.rxml"
|
||||||
|
end
|
||||||
|
|
||||||
helper NewRenderTestHelper
|
helper NewRenderTestHelper
|
||||||
helper do
|
helper do
|
||||||
def rjs_helper_method(value)
|
def rjs_helper_method(value)
|
||||||
|
@ -560,4 +568,13 @@ EOS
|
||||||
get :yield_content_for
|
get :yield_content_for
|
||||||
assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
|
assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
def test_overwritting_rendering_relative_file_with_extension
|
||||||
|
get :hello_world_from_rxml_using_template
|
||||||
|
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
||||||
|
|
||||||
|
get :hello_world_from_rxml_using_action
|
||||||
|
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
||||||
|
end
|
||||||
|
end
|
|
@ -18,14 +18,6 @@ class TestController < ActionController::Base
|
||||||
def hello_world
|
def hello_world
|
||||||
end
|
end
|
||||||
|
|
||||||
def hello_world_from_rxml_using_action
|
|
||||||
render :action => "hello_world.rxml"
|
|
||||||
end
|
|
||||||
|
|
||||||
def hello_world_from_rxml_using_template
|
|
||||||
render :template => "test/hello_world.rxml"
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_hello_world
|
def render_hello_world
|
||||||
render "test/hello_world"
|
render "test/hello_world"
|
||||||
end
|
end
|
||||||
|
@ -251,12 +243,4 @@ class RenderTest < Test::Unit::TestCase
|
||||||
get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
|
get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
|
||||||
assert_equal "Goodbye, Local David", @response.body
|
assert_equal "Goodbye, Local David", @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_overwritting_rendering_relative_file_with_extension
|
|
||||||
get :hello_world_from_rxml_using_template
|
|
||||||
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
|
||||||
|
|
||||||
get :hello_world_from_rxml_using_action
|
|
||||||
assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
1
actionpack/test/fixtures/respond_to/using_defaults.rhtml
vendored
Normal file
1
actionpack/test/fixtures/respond_to/using_defaults.rhtml
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Hello world!
|
1
actionpack/test/fixtures/respond_to/using_defaults.rjs
vendored
Normal file
1
actionpack/test/fixtures/respond_to/using_defaults.rjs
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
page[:body].visual_effect :highlight
|
1
actionpack/test/fixtures/respond_to/using_defaults.rxml
vendored
Normal file
1
actionpack/test/fixtures/respond_to/using_defaults.rxml
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
xml.p "Hello world!"
|
Loading…
Reference in a new issue