diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index afe9a4e6bcb481edd0d9277cda693427c3d7b165..ff2315fc3c7b1d64970b9746174a3367ac7261c2 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -7,7 +7,7 @@ @posts = Post.find :all 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.js { render :action => "index.rjs" } end diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb index 42b6e242050343e99b9e3ca808047b2bd4b2d1d3..e8d1ba089bda91c100174db968f3ccee577f46db 100644 --- a/actionpack/lib/action_controller/mime_responds.rb +++ b/actionpack/lib/action_controller/mime_responds.rb @@ -13,6 +13,13 @@ def respond_to(&block) end 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) @block_binding = block_binding @mime_type_priority = eval("request.accepts", block_binding) @@ -22,9 +29,19 @@ def initialize(block_binding) for mime_type in %w( all html js xml rss atom yaml ) eval <<-EOT - def #{mime_type}(&block) + def #{mime_type}(argument = nil, &block) @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 EOT end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index d78feb25e9ea7887164b3971694f1aae3be6fc2e..f2aa45a168d8f1d3d0ffab89d77bd6ce4b58a6e3 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -31,12 +31,30 @@ def just_xml type.xml { render :text => "XML" } 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) raise unless ActionController::MissingTemplate === e end end +RespondToController.template_root = File.dirname(__FILE__) + "/../fixtures/" + class MimeControllerTest < Test::Unit::TestCase def setup @request = ActionController::TestRequest.new @@ -99,4 +117,24 @@ def test_js_or_anything get :just_xml assert_equal 'XML', @response.body 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 "

Hello world!

\n", @response.body + end + + def test_using_argument_defaults + @request.env["HTTP_ACCEPT"] = "application/xml" + get :using_argument_defaults + assert_equal "\n\n David\n\n", @response.body + end end \ No newline at end of file diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 8359e190a071a02a098e17b9c559bcb8873a1ae6..5bdb8ec7f8b0c997eb9ef1e5c505003ad7ae6b07 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -184,6 +184,14 @@ def @template.name() nil end render :action => "potential_conflicts" 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 do def rjs_helper_method(value) @@ -560,4 +568,13 @@ def test_yield_content_for get :yield_content_for assert_equal "Putting stuff in the title!\n\nGreat stuff!\n", @response.body end -end + + + def test_overwritting_rendering_relative_file_with_extension + get :hello_world_from_rxml_using_template + assert_equal "\n

Hello

\n\n", @response.body + + get :hello_world_from_rxml_using_action + assert_equal "\n

Hello

\n\n", @response.body + end +end \ No newline at end of file diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index db6f71acc9a9152ca0b086d5fedf9aa8c8816201..1ee77a549d23534e2d66dbfad7af64dbd05435b5 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -18,14 +18,6 @@ class TestController < ActionController::Base def hello_world 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 render "test/hello_world" end @@ -251,12 +243,4 @@ def test_accessing_local_assigns_in_inline_template_with_string_keys get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David" assert_equal "Goodbye, Local David", @response.body end - - def test_overwritting_rendering_relative_file_with_extension - get :hello_world_from_rxml_using_template - assert_equal "\n

Hello

\n\n", @response.body - - get :hello_world_from_rxml_using_action - assert_equal "\n

Hello

\n\n", @response.body - end end diff --git a/actionpack/test/fixtures/respond_to/using_defaults.rhtml b/actionpack/test/fixtures/respond_to/using_defaults.rhtml new file mode 100644 index 0000000000000000000000000000000000000000..6769dd60bdf536a83c9353272157893043e9f7d0 --- /dev/null +++ b/actionpack/test/fixtures/respond_to/using_defaults.rhtml @@ -0,0 +1 @@ +Hello world! \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_to/using_defaults.rjs b/actionpack/test/fixtures/respond_to/using_defaults.rjs new file mode 100644 index 0000000000000000000000000000000000000000..469fcd8e15ba81b98a5276da9e0e6dacc5993a63 --- /dev/null +++ b/actionpack/test/fixtures/respond_to/using_defaults.rjs @@ -0,0 +1 @@ +page[:body].visual_effect :highlight \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_to/using_defaults.rxml b/actionpack/test/fixtures/respond_to/using_defaults.rxml new file mode 100644 index 0000000000000000000000000000000000000000..598d62e2fca55941642ce03450df98f31b756565 --- /dev/null +++ b/actionpack/test/fixtures/respond_to/using_defaults.rxml @@ -0,0 +1 @@ +xml.p "Hello world!" \ No newline at end of file