class RuboCop::Cop::GitHub::RailsControllerRenderShorthand

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/github/rails_controller_render_shorthand.rb, line 27
def autocorrect(node)
  @autocorrect[node]
end
investigate(*) click to toggle source
# File lib/rubocop/cop/github/rails_controller_render_shorthand.rb, line 23
def investigate(*)
  @autocorrect = {}
end
on_send(node) click to toggle source
# File lib/rubocop/cop/github/rails_controller_render_shorthand.rb, line 31
def on_send(node)
  if option_pairs = render_with_options?(node)
    option_pairs.each do |pair|
      if value_node = action_key?(pair)
        comma = option_pairs.length > 1 ? ", " : ""
        corrected_source = node.source
          .sub(/#{pair.source}(,\s*)?/, "")
          .sub("render ", "render \"#{str(value_node)}\"#{comma}")

        @autocorrect[node] = lambda do |corrector|
          corrector.replace(node.source_range, corrected_source)
        end
        add_offense(node, :expression, "Use `#{corrected_source}` instead")
      end
    end
  end
end