Skip to content

Commit

Permalink
Fix an error for Rails/RenderPlainText when the content type is pas…
Browse files Browse the repository at this point in the history
…sed as a constant

Or other nodes that don't respond to `value` like local variables
  • Loading branch information
Earlopain committed Aug 12, 2024
1 parent b8c4126 commit eecc2bb
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 eecc2bb

Please sign in to comment.