Skip to content

Commit d4a4ea7

Browse files
authored
Merge pull request #1325 from Earlopain/render-plain-text-error
Fix an error for `Rails/RenderPlainText` when the content type is passed as a constant
2 parents b8c4126 + eecc2bb commit d4a4ea7

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1325](https://github.com/rubocop/rubocop-rails/pull/1325): Fix an error for `Rails/RenderPlainText` when the content type is passed as a constant. ([@earlopain][])

lib/rubocop/cop/rails/render_plain_text.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ def find_content_type(node)
5353
node.pairs.find { |p| p.key.value.to_sym == :content_type }
5454
end
5555

56-
def compatible_content_type?(node)
57-
(node && node.value.value == 'text/plain') ||
58-
(!node && !cop_config['ContentTypeCompatibility'])
56+
def compatible_content_type?(pair_node)
57+
if pair_node.nil?
58+
!cop_config['ContentTypeCompatibility']
59+
elsif pair_node.value.respond_to?(:value)
60+
pair_node.value.value == 'text/plain'
61+
end
5962
end
6063

6164
def replacement(rest_options, option_value)

spec/rubocop/cop/rails/render_plain_text_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
RUBY
2020
end
2121

22+
it 'does not register an offense when `content_type` is a constant' do
23+
expect_no_offenses(<<~RUBY)
24+
render text: 'Ruby!', content_type: Foo
25+
RUBY
26+
end
27+
2228
it 'does not register an offense when using `render plain:`' do
2329
expect_no_offenses(<<~RUBY)
2430
render plain: 'Ruby!'

0 commit comments

Comments
 (0)