Skip to content

Commit 6790228

Browse files
committed
Add deprecation warning for render :text
We've started on discouraging the usage of `render :text` in rails#12374. This is a follow-up commit to make sure that we print out the deprecation warning.
1 parent 2c79122 commit 6790228

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

actionpack/lib/action_controller/metal/rendering.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'active_support/deprecation'
2+
require 'active_support/core_ext/string/filters'
3+
14
module ActionController
25
module Rendering
36
extend ActiveSupport::Concern
@@ -74,6 +77,17 @@ def _normalize_args(action=nil, options={}, &blk) #:nodoc:
7477
def _normalize_options(options) #:nodoc:
7578
_normalize_text(options)
7679

80+
if options[:text]
81+
ActiveSupport::Deprecation.warn <<-WARNING.squish
82+
`render :text` is deprecated because it does not actually render a
83+
`text/plain` response. Switch to `render plain: 'plain text'` to
84+
render as `text/plain`, `render html: '<strong>HTML</strong>'` to
85+
render as `text/html`, or `render body: 'raw'` to match the deprecated
86+
behavior and render with the default Content-Type, which is
87+
`text/plain`.
88+
WARNING
89+
end
90+
7791
if options[:html]
7892
options[:html] = ERB::Util.html_escape(options[:html])
7993
end

actionpack/test/controller/new_base/render_text_test.rb

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'abstract_unit'
2+
require 'active_support/deprecation'
23

34
module RenderText
45
class MinimalController < ActionController::Metal
@@ -73,7 +74,10 @@ def with_ivar_in_layout
7374

7475
class RenderTextTest < Rack::TestCase
7576
test "rendering text from a minimal controller" do
76-
get "/render_text/minimal/index"
77+
ActiveSupport::Deprecation.silence do
78+
get "/render_text/minimal/index"
79+
end
80+
7781
assert_body "Hello World!"
7882
assert_status 200
7983
end
@@ -82,7 +86,10 @@ class RenderTextTest < Rack::TestCase
8286
with_routing do |set|
8387
set.draw { get ':controller', action: 'index' }
8488

85-
get "/render_text/simple"
89+
ActiveSupport::Deprecation.silence do
90+
get "/render_text/simple"
91+
end
92+
8693
assert_body "hello david"
8794
assert_status 200
8895
end
@@ -92,67 +99,91 @@ class RenderTextTest < Rack::TestCase
9299
with_routing do |set|
93100
set.draw { get ':controller', action: 'index' }
94101

95-
get "/render_text/with_layout"
102+
ActiveSupport::Deprecation.silence do
103+
get "/render_text/with_layout"
104+
end
96105

97106
assert_body "hello david"
98107
assert_status 200
99108
end
100109
end
101110

102111
test "rendering text, while also providing a custom status code" do
103-
get "/render_text/with_layout/custom_code"
112+
ActiveSupport::Deprecation.silence do
113+
get "/render_text/with_layout/custom_code"
114+
end
104115

105116
assert_body "hello world"
106117
assert_status 404
107118
end
108119

109120
test "rendering text with nil returns an empty body" do
110-
get "/render_text/with_layout/with_nil"
121+
ActiveSupport::Deprecation.silence do
122+
get "/render_text/with_layout/with_nil"
123+
end
111124

112125
assert_body ""
113126
assert_status 200
114127
end
115128

116129
test "Rendering text with nil and custom status code returns an empty body and the status" do
117-
get "/render_text/with_layout/with_nil_and_status"
130+
ActiveSupport::Deprecation.silence do
131+
get "/render_text/with_layout/with_nil_and_status"
132+
end
118133

119134
assert_body ""
120135
assert_status 403
121136
end
122137

123138
test "rendering text with false returns the string 'false'" do
124-
get "/render_text/with_layout/with_false"
139+
ActiveSupport::Deprecation.silence do
140+
get "/render_text/with_layout/with_false"
141+
end
125142

126143
assert_body "false"
127144
assert_status 200
128145
end
129146

130147
test "rendering text with layout: true" do
131-
get "/render_text/with_layout/with_layout_true"
148+
ActiveSupport::Deprecation.silence do
149+
get "/render_text/with_layout/with_layout_true"
150+
end
132151

133152
assert_body "hello world, I'm here!"
134153
assert_status 200
135154
end
136155

137156
test "rendering text with layout: 'greetings'" do
138-
get "/render_text/with_layout/with_custom_layout"
157+
ActiveSupport::Deprecation.silence do
158+
get "/render_text/with_layout/with_custom_layout"
159+
end
139160

140161
assert_body "hello world, I wish thee well."
141162
assert_status 200
142163
end
143164

144165
test "rendering text with layout: false" do
145-
get "/render_text/with_layout/with_layout_false"
166+
ActiveSupport::Deprecation.silence do
167+
get "/render_text/with_layout/with_layout_false"
168+
end
146169

147170
assert_body "hello world"
148171
assert_status 200
149172
end
150173

151174
test "rendering text with layout: nil" do
152-
get "/render_text/with_layout/with_layout_nil"
175+
ActiveSupport::Deprecation.silence do
176+
get "/render_text/with_layout/with_layout_nil"
177+
end
153178

154179
assert_body "hello world"
155180
assert_status 200
156181
end
182+
183+
test "rendering text displays deprecation warning" do
184+
assert_deprecated do
185+
get "/render_text/with_layout/with_layout_nil"
186+
end
187+
end
157188
end
158189
end

0 commit comments

Comments
 (0)