Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Finished implementing render :text in Base2

  • Loading branch information...
commit 4f68311685831b940936130203c240fa23525c84 1 parent 1b45991
Yehuda Katz + Carl Lerche authored
View
13 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 @@ def _action_view
end
def render(options = {})
+ unless response_body.nil?
+ raise AbstractController::DoubleRenderError, "OMG"
+ end
+
self.response_body = render_to_body(options)
end
View
8 actionpack/lib/action_controller/new_base/base.rb
@@ -40,23 +40,19 @@ def self.call(env)
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
2  actionpack/lib/action_controller/new_base/renderer.rb
@@ -17,7 +17,7 @@ def render(action, options = {})
_process_options(options)
- self.response_body = render_to_body(options)
+ super(options)
end
def render_to_body(options)
View
17 actionpack/test/controller/render_test.rb
@@ -129,6 +129,7 @@ def render_text_hello_world
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 @@ def render_json_with_render_to_string
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 @@ def render_custom_code_rjs
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 @@ def greeting
# let's just rely on the template
end
+ # :ported:
def blank_response
render :text => ' '
end
@@ -405,6 +411,7 @@ def render_with_explicit_template_with_locals
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 @@ def action_talk_to_layout
# 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 @@ def test_render_in_top_directory_with_slash
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 @@ def test_render_action_with_symbol
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 @@ def test_render_custom_code_rjs
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 @@ def test_render_with_explicit_string_template
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 @@ def test_action_talk_to_layout
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
2  actionpack/test/new_base/base_test.rb
@@ -84,6 +84,6 @@ def test_controller_path
def test_controller_name
assert_equal 'empty', EmptyController.controller_name
assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
- end
+ end
end
end
View
20 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
Please sign in to comment.
Something went wrong with that request. Please try again.