Class: RuboCop::Cop::Standard::RailsControllerRenderShorthand

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/standard/rails/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/standard/rails/rails_controller_render_shorthand.rb', line 27

def autocorrect(node)
  @autocorrect[node]
end

#investigateObject



23
24
25
# File 'lib/rubocop/cop/standard/rails/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/standard/rails/rails_controller_render_shorthand.rb', line 31

def on_send(node)
  return unless (option_pairs = render_with_options?(node))

  option_pairs.each do |pair|
    next unless (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, location: :expression, message: "Use `#{corrected_source}` instead")
  end
end