From d2e7c1b97d1c2152eeb0669fe0b88989a087246c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 10 Jan 2010 20:21:08 +0100 Subject: Raise an error if respond_with is invoked and no format is declared. --- .../lib/action_controller/metal/mime_responds.rb | 9 +- actionpack/test/controller/mime_responds_test.rb | 130 ++++++++++----------- .../fixtures/respond_with/using_defaults.html.erb | 1 - .../fixtures/respond_with/using_defaults.js.rjs | 1 - .../using_defaults_with_type_list.js.rjs | 1 - .../using_defaults_with_type_list.xml.builder | 1 - .../using_resource_with_block.html.erb | 1 + 7 files changed, 69 insertions(+), 75 deletions(-) delete mode 100644 actionpack/test/fixtures/respond_with/using_defaults.html.erb delete mode 100644 actionpack/test/fixtures/respond_with/using_defaults.js.rjs delete mode 100644 actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs delete mode 100644 actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder create mode 100644 actionpack/test/fixtures/respond_with/using_resource_with_block.html.erb (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 468c5f4fae..4c02677729 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -215,7 +215,10 @@ module ActionController #:nodoc: # a proc to it. # def respond_with(*resources, &block) - if response = retrieve_response_from_mimes([], &block) + raise "In order to use respond_with, first you need to declare the formats your " << + "controller responds to in the class level" if mimes_for_respond_to.empty? + + if response = retrieve_response_from_mimes(&block) options = resources.extract_options! options.merge!(:default_response => response) (options.delete(:responder) || responder).call(self, resources, options) @@ -246,9 +249,9 @@ module ActionController #:nodoc: # Collects mimes and return the response for the negotiated format. Returns # nil if :not_acceptable was sent to the client. # - def retrieve_response_from_mimes(mimes, &block) + def retrieve_response_from_mimes(mimes=nil, &block) collector = Collector.new { default_render } - mimes = collect_mimes_from_class_level if mimes.empty? + mimes ||= collect_mimes_from_class_level mimes.each { |mime| collector.send(mime) } block.call(collector) if block_given? diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 6b9cace9cd..ba2347e4e2 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -461,31 +461,27 @@ end class RespondWithController < ActionController::Base respond_to :html, :json - respond_to :xml, :except => :using_defaults - respond_to :js, :only => [ :using_defaults, :using_resource ] + respond_to :xml, :except => :using_resource_with_block + respond_to :js, :only => [ :using_resource_with_block, :using_resource ] - def using_defaults - respond_to do |format| - format.csv { render :text => "CSV" } - end + def using_resource + respond_with(resource) end - def using_defaults_with_type_list - respond_to(:js, :xml) + def using_resource_with_block + respond_with(resource) do |format| + format.csv { render :text => "CSV" } + end end - def default_overwritten - respond_to do |format| + def using_resource_with_overwrite_block + respond_with(resource) do |format| format.html { render :text => "HTML" } end end - def using_resource - respond_with(Customer.new("david", 13)) - end - def using_resource_with_collection - respond_with([Customer.new("david", 13), Customer.new("jamis", 9)]) + respond_with([resource, Customer.new("jamis", 9)]) end def using_resource_with_parent @@ -493,16 +489,16 @@ class RespondWithController < ActionController::Base end def using_resource_with_status_and_location - respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created) + respond_with(resource, :location => "http://test.host/", :status => :created) end def using_resource_with_responder responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" } - respond_with(Customer.new("david", 13), :responder => responder) + respond_with(resource, :responder => responder) end def using_resource_with_action - respond_with(Customer.new("david", 13), :action => :foo) do |format| + respond_with(resource, :action => :foo) do |format| format.html { raise ActionView::MissingTemplate.new([], "method") } end end @@ -511,11 +507,15 @@ class RespondWithController < ActionController::Base responder = Class.new(ActionController::Responder) do def respond; @controller.render :text => "respond #{format}"; end end - respond_with(Customer.new("david", 13), :responder => responder) + respond_with(resource, :responder => responder) end protected + def resource + Customer.new("david", 13) + end + def _render_js(js, options) self.content_type ||= Mime::JS self.response_body = js.respond_to?(:to_js) ? js.to_js : js @@ -527,12 +527,18 @@ class InheritedRespondWithController < RespondWithController respond_to :xml, :json def index - respond_with(Customer.new("david", 13)) do |format| + respond_with(resource) do |format| format.json { render :text => "JSON" } end end end +class EmptyRespondWithController < ActionController::Base + def index + respond_with(Customer.new("david", 13)) + end +end + class RespondWithControllerTest < ActionController::TestCase tests RespondWithController @@ -547,56 +553,54 @@ class RespondWithControllerTest < ActionController::TestCase ActionController::Base.use_accept_header = false end - def test_using_defaults + def test_using_resource + @request.accept = "text/javascript" + get :using_resource + assert_equal "text/javascript", @response.content_type + assert_equal '$("body").visualEffect("highlight");', @response.body + + @request.accept = "application/xml" + get :using_resource + assert_equal "application/xml", @response.content_type + assert_equal "david", @response.body + + @request.accept = "application/json" + assert_raise ActionView::MissingTemplate do + get :using_resource + end + end + + def test_using_resource_with_block @request.accept = "*/*" - get :using_defaults + get :using_resource_with_block assert_equal "text/html", @response.content_type assert_equal 'Hello world!', @response.body @request.accept = "text/csv" - get :using_defaults + get :using_resource_with_block assert_equal "text/csv", @response.content_type assert_equal "CSV", @response.body - @request.accept = "text/javascript" - get :using_defaults - assert_equal "text/javascript", @response.content_type - assert_equal '$("body").visualEffect("highlight");', @response.body - end - - def test_using_defaults_with_type_list - @request.accept = "*/*" - get :using_defaults_with_type_list - assert_equal "text/javascript", @response.content_type - assert_equal '$("body").visualEffect("highlight");', @response.body - @request.accept = "application/xml" - get :using_defaults_with_type_list + get :using_resource assert_equal "application/xml", @response.content_type - assert_equal "

Hello world!

\n", @response.body + assert_equal "david", @response.body end - def test_default_overwritten - get :default_overwritten + def test_using_resource_with_overwrite_block + get :using_resource_with_overwrite_block assert_equal "text/html", @response.content_type assert_equal "HTML", @response.body end - def test_using_resource - @request.accept = "text/javascript" - get :using_resource - assert_equal "text/javascript", @response.content_type - assert_equal '$("body").visualEffect("highlight");', @response.body - + def test_not_acceptable @request.accept = "application/xml" - get :using_resource - assert_equal "application/xml", @response.content_type - assert_equal "david", @response.body + get :using_resource_with_block + assert_equal 406, @response.status - @request.accept = "application/json" - assert_raise ActionView::MissingTemplate do - get :using_resource - end + @request.accept = "text/javascript" + get :using_resource_with_overwrite_block + assert_equal 406, @response.status end def test_using_resource_for_post_with_html_redirects_on_success @@ -831,22 +835,12 @@ class RespondWithControllerTest < ActionController::TestCase RespondWithController.responder = ActionController::Responder end - def test_not_acceptable - @request.accept = "application/xml" - get :using_defaults - assert_equal 406, @response.status - - @request.accept = "text/html" - get :using_defaults_with_type_list - assert_equal 406, @response.status - - @request.accept = "application/json" - get :using_defaults_with_type_list - assert_equal 406, @response.status - - @request.accept = "text/javascript" - get :default_overwritten - assert_equal 406, @response.status + def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called + @controller = EmptyRespondWithController.new + @request.accept = "*/*" + assert_raise RuntimeError do + get :index + end end private diff --git a/actionpack/test/fixtures/respond_with/using_defaults.html.erb b/actionpack/test/fixtures/respond_with/using_defaults.html.erb deleted file mode 100644 index 6769dd60bd..0000000000 --- a/actionpack/test/fixtures/respond_with/using_defaults.html.erb +++ /dev/null @@ -1 +0,0 @@ -Hello world! \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_with/using_defaults.js.rjs b/actionpack/test/fixtures/respond_with/using_defaults.js.rjs deleted file mode 100644 index 469fcd8e15..0000000000 --- a/actionpack/test/fixtures/respond_with/using_defaults.js.rjs +++ /dev/null @@ -1 +0,0 @@ -page[:body].visual_effect :highlight \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs b/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs deleted file mode 100644 index 469fcd8e15..0000000000 --- a/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.js.rjs +++ /dev/null @@ -1 +0,0 @@ -page[:body].visual_effect :highlight \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder b/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder deleted file mode 100644 index 598d62e2fc..0000000000 --- a/actionpack/test/fixtures/respond_with/using_defaults_with_type_list.xml.builder +++ /dev/null @@ -1 +0,0 @@ -xml.p "Hello world!" \ No newline at end of file diff --git a/actionpack/test/fixtures/respond_with/using_resource_with_block.html.erb b/actionpack/test/fixtures/respond_with/using_resource_with_block.html.erb new file mode 100644 index 0000000000..6769dd60bd --- /dev/null +++ b/actionpack/test/fixtures/respond_with/using_resource_with_block.html.erb @@ -0,0 +1 @@ +Hello world! \ No newline at end of file -- cgit v1.2.3