Class: RuboCop::Cop::Rails::RenderPlainText

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/render_plain_text.rb

Overview

Identifies places where ‘render text:` can be replaced with `render plain:`.

Examples:

# bad - explicit MIME type to `text/plain`
render text: 'Ruby!', content_type: 'text/plain'

# good - short and precise
render plain: 'Ruby!'

# good - explicit MIME type not to `text/plain`
render text: 'Ruby!', content_type: 'text/html'

ContentTypeCompatibility: true (default)

# good - sets MIME type to `text/html`
render text: 'Ruby!'

ContentTypeCompatibility: false

# bad - sets MIME type to `text/html`
render text: 'Ruby!'

Constant Summary collapse

MSG =
'Prefer `render plain:` over `render text:`.'
RESTRICT_ON_SEND =
%i[render].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/rails/render_plain_text.rb', line 37

def on_send(node)
  render_plain_text?(node) do |options_node, option_node, option_value|
    content_type_node = find_content_type(options_node)
    return unless compatible_content_type?(content_type_node)

    add_offense(node) do |corrector|
      rest_options = options_node.pairs - [option_node, content_type_node].compact

      corrector.replace(node, replacement(rest_options, option_value))
    end
  end
end