Class: RuboCop::Cop::GitHub::RailsControllerRenderShorthand

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

Constant Summary collapse

MSG =
"Prefer `render` template shorthand"

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/github/rails_controller_render_shorthand.rb', line 25

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}")

        add_offense(node, message: "Use `#{corrected_source}` instead") do |corrector|
          corrector.replace(node.source_range, corrected_source)
        end
      end
    end
  end
end