Skip to content

Commit

Permalink
Merge pull request #1325 from Earlopain/render-plain-text-error
Browse files Browse the repository at this point in the history
Fix an error for `Rails/RenderPlainText` when the content type is passed as a constant
  • Loading branch information
koic authored Aug 12, 2024
2 parents b8c4126 + eecc2bb commit d4a4ea7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_rails_render_plain_text_constant.md
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
9 changes: 6 additions & 3 deletions lib/rubocop/cop/rails/render_plain_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ def find_content_type(node)
node.pairs.find { |p| p.key.value.to_sym == :content_type }
end

def compatible_content_type?(node)
(node && node.value.value == 'text/plain') ||
(!node && !cop_config['ContentTypeCompatibility'])
def compatible_content_type?(pair_node)
if pair_node.nil?
!cop_config['ContentTypeCompatibility']
elsif pair_node.value.respond_to?(:value)
pair_node.value.value == 'text/plain'
end
end

def replacement(rest_options, option_value)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/render_plain_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
RUBY
end

it 'does not register an offense when `content_type` is a constant' do
expect_no_offenses(<<~RUBY)
render text: 'Ruby!', content_type: Foo
RUBY
end

it 'does not register an offense when using `render plain:`' do
expect_no_offenses(<<~RUBY)
render plain: 'Ruby!'
Expand Down

0 comments on commit d4a4ea7

Please sign in to comment.