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

Inherits:
RuboCop::Cop
  • Object
show all
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

#autocorrect(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/github/rails_controller_render_shorthand.rb', line 27

def autocorrect(node)
  @autocorrect[node]
end

#investigateObject



23
24
25
# File 'lib/rubocop/cop/github/rails_controller_render_shorthand.rb', line 23

def investigate(*)
  @autocorrect = {}
end

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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