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

Finished implementing render :text in Base2

This commit is contained in:
Yehuda Katz + Carl Lerche 2009-04-29 12:19:17 -07:00
parent 1b45991603
commit 4f68311685
6 changed files with 54 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -17,7 +17,7 @@ module ActionController
_process_options(options)
self.response_body = render_to_body(options)
super(options)
end
def render_to_body(options)

View file

@ -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"]

View file

@ -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

View file

@ -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