diff options
author | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-04-29 12:19:17 -0700 |
---|---|---|
committer | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-05-01 17:31:01 -0700 |
commit | 4f68311685831b940936130203c240fa23525c84 (patch) | |
tree | 5fc1192c04bdef4b198a92c511f2a7a5d0e09bc3 /actionpack | |
parent | 1b459916039259383808625d1758a8bc4a4516d9 (diff) | |
download | rails-4f68311685831b940936130203c240fa23525c84.tar.gz rails-4f68311685831b940936130203c240fa23525c84.tar.bz2 rails-4f68311685831b940936130203c240fa23525c84.zip |
Finished implementing render :text in Base2
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/abstract/renderer.rb | 13 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/base.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/renderer.rb | 2 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 17 | ||||
-rw-r--r-- | actionpack/test/new_base/base_test.rb | 2 | ||||
-rw-r--r-- | actionpack/test/new_base/render_test.rb | 20 |
6 files changed, 54 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index 37da2398ec..c1f420f7b4 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -1,6 +1,15 @@ require "action_controller/abstract/logger" module AbstractController + class AbstractControllerError < StandardError; end + class DoubleRenderError < AbstractControllerError + DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"." + + def initialize(message = nil) + super(message || DEFAULT_MESSAGE) + end + end + module Renderer depends_on AbstractController::Logger @@ -17,6 +26,10 @@ module AbstractController end def render(options = {}) + unless response_body.nil? + raise AbstractController::DoubleRenderError, "OMG" + end + self.response_body = render_to_body(options) end diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 6711cd932a..9f7a148b3c 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -40,23 +40,19 @@ module ActionController controller.call(env).to_rack end - # :api: plugin - def response_body=(body) - @_response.body = body - end - # :api: private def call(env) @_request = ActionDispatch::Request.new(env) @_response = ActionDispatch::Response.new process(@_request.parameters[:action]) + @_response.body = response_body @_response.prepare! self end # :api: private def to_rack - response.to_a + @_response.to_a end end end diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index ed34c46aed..9a965c18e8 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -17,7 +17,7 @@ module ActionController _process_options(options) - self.response_body = render_to_body(options) + super(options) end def render_to_body(options) diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 648e179a15..9149212a2d 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -129,6 +129,7 @@ class TestController < ActionController::Base render :text => "hello world" end + # :ported: def render_text_hello_world_with_layout @variable_for_layout = ", I'm here!" render :text => "hello world", :layout => true @@ -220,6 +221,7 @@ class TestController < ActionController::Base render :json => {:hello => render_to_string(:partial => 'partial')} end + # :ported: def render_custom_code render :text => "hello world", :status => 404 end @@ -230,14 +232,17 @@ class TestController < ActionController::Base end end + # :ported: def render_text_with_nil render :text => nil end + # :ported: def render_text_with_false render :text => false end + # :ported: def render_nothing_with_appendix render :text => "appended" end @@ -272,6 +277,7 @@ class TestController < ActionController::Base # let's just rely on the template end + # :ported: def blank_response render :text => ' ' end @@ -405,6 +411,7 @@ class TestController < ActionController::Base render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' } end + # :ported: def double_render render :text => "hello" render :text => "world" @@ -508,6 +515,7 @@ class TestController < ActionController::Base # Action template sets variable that's picked up by layout end + # :addressed: def render_text_with_assigns @hello = "world" render :text => "foo" @@ -807,6 +815,7 @@ class RenderTest < ActionController::TestCase assert_equal "Elastica", @response.body end + # :ported: def test_render_from_variable get :render_hello_world_from_variable assert_equal "hello david", @response.body @@ -828,16 +837,19 @@ class RenderTest < ActionController::TestCase assert_template "test/hello_world" end + # :ported: def test_render_text get :render_text_hello_world assert_equal "hello world", @response.body end + # :ported: def test_do_with_render_text_and_layout get :render_text_hello_world_with_layout assert_equal "<html>hello world, I'm here!</html>", @response.body end + # :ported: def test_do_with_render_action_and_layout_false get :hello_world_with_layout_false assert_equal 'Hello world!', @response.body @@ -932,17 +944,20 @@ class RenderTest < ActionController::TestCase assert_equal %(Element.replace("foo", "partial html");), @response.body end + # :ported: def test_render_text_with_nil get :render_text_with_nil assert_response 200 assert_equal ' ', @response.body end + # :ported: def test_render_text_with_false get :render_text_with_false assert_equal 'false', @response.body end + # :ported: def test_render_nothing_with_appendix get :render_nothing_with_appendix assert_response 200 @@ -1209,6 +1224,7 @@ class RenderTest < ActionController::TestCase assert_equal "<html>Hello world!</html>", @response.body end + # :ported: def test_double_render assert_raise(ActionController::DoubleRenderError) { get :double_render } end @@ -1237,6 +1253,7 @@ class RenderTest < ActionController::TestCase assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body end + # :addressed: def test_render_text_with_assigns get :render_text_with_assigns assert_equal "world", assigns["hello"] diff --git a/actionpack/test/new_base/base_test.rb b/actionpack/test/new_base/base_test.rb index 95fdfd6b56..27d1a7f026 100644 --- a/actionpack/test/new_base/base_test.rb +++ b/actionpack/test/new_base/base_test.rb @@ -84,6 +84,6 @@ module Dispatching def test_controller_name assert_equal 'empty', EmptyController.controller_name assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name - end + end end end
\ No newline at end of file diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/new_base/render_test.rb new file mode 100644 index 0000000000..9ad79124b8 --- /dev/null +++ b/actionpack/test/new_base/render_test.rb @@ -0,0 +1,20 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module Render + class DoubleRenderController < ActionController::Base2 + def index + render :text => "hello" + render :text => "world" + end + end + + class TestBasic < SimpleRouteCase + describe "Rendering more than once" + + test "raises an exception" do + assert_raises(AbstractController::DoubleRenderError) do + get "/render/double_render" + end + end + end +end
\ No newline at end of file |