class RuboCop::Cop::Rails::RenderPlainText

This cop identifies places where `render text:` can be replaced with `render plain:`.

@example

# 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'

@example ContentTypeCompatibility: true (default)

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

@example ContentTypeCompatibility: false

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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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

Private Instance Methods

compatible_content_type?(node) click to toggle source
# File lib/rubocop/cop/rails/render_plain_text.rb, line 56
def compatible_content_type?(node)
  (node && node.value.value == 'text/plain') ||
    (!node && !cop_config['ContentTypeCompatibility'])
end
find_content_type(node) click to toggle source
# File lib/rubocop/cop/rails/render_plain_text.rb, line 52
def find_content_type(node)
  node.pairs.find { |p| p.key.value.to_sym == :content_type }
end
replacement(rest_options, option_value) click to toggle source
# File lib/rubocop/cop/rails/render_plain_text.rb, line 61
def replacement(rest_options, option_value)
  if rest_options.any?
    "render plain: #{option_value.source}, #{rest_options.map(&:source).join(', ')}"
  else
    "render plain: #{option_value.source}"
  end
end