2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2009-04-29 15:19:17 -04:00
|
|
|
|
|
|
|
module Render
|
2009-05-01 20:27:44 -04:00
|
|
|
class BlankRenderController < ActionController::Base
|
2009-06-17 18:32:55 -04:00
|
|
|
self.view_paths = [ActionView::FixtureResolver.new(
|
2009-05-01 14:40:32 -04:00
|
|
|
"render/blank_render/index.html.erb" => "Hello world!",
|
|
|
|
"render/blank_render/access_request.html.erb" => "The request: <%= request.method.to_s.upcase %>",
|
|
|
|
"render/blank_render/access_action_name.html.erb" => "Action Name: <%= action_name %>",
|
2010-09-17 16:39:14 -04:00
|
|
|
"render/blank_render/access_controller_name.html.erb" => "Controller Name: <%= controller_name %>",
|
2013-03-30 14:17:08 -04:00
|
|
|
"render/blank_render/overridden_with_own_view_paths_appended.html.erb" => "parent content",
|
|
|
|
"render/blank_render/overridden_with_own_view_paths_prepended.html.erb" => "parent content",
|
|
|
|
"render/blank_render/overridden.html.erb" => "parent content",
|
|
|
|
"render/child_render/overridden.html.erb" => "child content"
|
2009-04-29 20:32:39 -04:00
|
|
|
)]
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-04-29 20:32:39 -04:00
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-05-01 14:40:32 -04:00
|
|
|
def access_request
|
2016-08-06 13:35:13 -04:00
|
|
|
render action: "access_request"
|
2009-05-01 14:40:32 -04:00
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-05-01 14:40:32 -04:00
|
|
|
def render_action_name
|
2016-08-06 13:35:13 -04:00
|
|
|
render action: "access_action_name"
|
2009-05-01 14:40:32 -04:00
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2013-03-30 14:17:08 -04:00
|
|
|
def overridden_with_own_view_paths_appended
|
2010-09-17 16:39:14 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:17:08 -04:00
|
|
|
def overridden_with_own_view_paths_prepended
|
2010-09-17 16:39:14 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:17:08 -04:00
|
|
|
def overridden
|
2010-09-17 16:39:14 -04:00
|
|
|
end
|
|
|
|
|
2009-05-20 15:15:06 -04:00
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def secretz
|
|
|
|
render plain: "FAIL WHALE!"
|
|
|
|
end
|
2009-04-29 20:32:39 -04:00
|
|
|
end
|
|
|
|
|
2009-05-01 20:27:44 -04:00
|
|
|
class DoubleRenderController < ActionController::Base
|
2009-04-29 15:19:17 -04:00
|
|
|
def index
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: "hello"
|
|
|
|
render plain: "world"
|
2009-04-29 15:19:17 -04:00
|
|
|
end
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2010-09-17 16:39:14 -04:00
|
|
|
class ChildRenderController < BlankRenderController
|
2013-03-30 14:17:08 -04:00
|
|
|
append_view_path ActionView::FixtureResolver.new("render/child_render/overridden_with_own_view_paths_appended.html.erb" => "child content")
|
|
|
|
prepend_view_path ActionView::FixtureResolver.new("render/child_render/overridden_with_own_view_paths_prepended.html.erb" => "child content")
|
2010-09-17 16:39:14 -04:00
|
|
|
end
|
|
|
|
|
2009-10-04 00:18:32 -04:00
|
|
|
class RenderTest < Rack::TestCase
|
2009-05-20 15:15:06 -04:00
|
|
|
test "render with blank" do
|
2010-08-06 11:57:58 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2016-03-01 03:48:53 -05:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 13:35:13 -04:00
|
|
|
get ":controller", action: "index"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2010-08-06 11:57:58 -04:00
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2010-08-06 11:57:58 -04:00
|
|
|
get "/render/blank_render"
|
|
|
|
|
|
|
|
assert_body "Hello world!"
|
|
|
|
assert_status 200
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "rendering more than once raises an exception" do
|
2010-08-06 11:57:58 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2016-03-01 03:48:53 -05:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 13:35:13 -04:00
|
|
|
get ":controller", action: "index"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2010-08-06 11:57:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(AbstractController::DoubleRenderError) do
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/render/double_render", headers: { "action_dispatch.show_exceptions" => false }
|
2010-08-06 11:57:58 -04:00
|
|
|
end
|
2009-04-29 15:19:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-10-04 00:18:32 -04:00
|
|
|
class TestOnlyRenderPublicActions < Rack::TestCase
|
2011-04-15 17:13:46 -04:00
|
|
|
# Only public methods on actual controllers are callable actions
|
2009-05-01 14:40:32 -04:00
|
|
|
test "raises an exception when a method of Object is called" do
|
2009-04-30 18:13:40 -04:00
|
|
|
assert_raises(AbstractController::ActionNotFound) do
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/render/blank_render/clone", headers: { "action_dispatch.show_exceptions" => false }
|
2009-04-30 18:13:40 -04:00
|
|
|
end
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-05-01 14:40:32 -04:00
|
|
|
test "raises an exception when a private method is called" do
|
|
|
|
assert_raises(AbstractController::ActionNotFound) do
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/render/blank_render/secretz", headers: { "action_dispatch.show_exceptions" => false }
|
2009-05-01 14:40:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-10-04 00:18:32 -04:00
|
|
|
class TestVariousObjectsAvailableInView < Rack::TestCase
|
2009-05-01 14:40:32 -04:00
|
|
|
test "The request object is accessible in the view" do
|
|
|
|
get "/render/blank_render/access_request"
|
|
|
|
assert_body "The request: GET"
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-05-01 14:40:32 -04:00
|
|
|
test "The action_name is accessible in the view" do
|
|
|
|
get "/render/blank_render/render_action_name"
|
|
|
|
assert_body "Action Name: render_action_name"
|
|
|
|
end
|
2009-05-20 15:15:06 -04:00
|
|
|
|
2009-05-01 14:40:32 -04:00
|
|
|
test "The controller_name is accessible in the view" do
|
|
|
|
get "/render/blank_render/access_controller_name"
|
|
|
|
assert_body "Controller Name: blank_render"
|
|
|
|
end
|
2009-04-30 18:13:40 -04:00
|
|
|
end
|
2010-09-17 16:39:14 -04:00
|
|
|
|
|
|
|
class TestViewInheritance < Rack::TestCase
|
|
|
|
test "Template from child controller gets picked over parent one" do
|
2013-03-30 14:17:08 -04:00
|
|
|
get "/render/child_render/overridden"
|
2010-09-17 16:39:14 -04:00
|
|
|
assert_body "child content"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Template from child controller with custom view_paths prepended gets picked over parent one" do
|
2013-03-30 14:17:08 -04:00
|
|
|
get "/render/child_render/overridden_with_own_view_paths_prepended"
|
2010-09-17 16:39:14 -04:00
|
|
|
assert_body "child content"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Template from child controller with custom view_paths appended gets picked over parent one" do
|
2013-03-30 14:17:08 -04:00
|
|
|
get "/render/child_render/overridden_with_own_view_paths_appended"
|
2010-09-17 16:39:14 -04:00
|
|
|
assert_body "child content"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Template from parent controller gets picked if missing in child controller" do
|
|
|
|
get "/render/child_render/index"
|
|
|
|
assert_body "Hello world!"
|
|
|
|
end
|
|
|
|
end
|
2009-09-13 17:30:27 -04:00
|
|
|
end
|