Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Rails/RenderPlainText when the content type is passed as a constant #1325

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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