Class: RuboCop::Cop::GitHub::RailsRenderObjectCollection

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/github/rails_render_object_collection.rb

Constant Summary collapse

MSG =
"Avoid `render object:`"

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  if option_pairs = render_with_options?(node)
    partial_pair = option_pairs.detect { |pair| partial_key?(pair) }
    object_pair  = option_pairs.detect { |pair| object_key?(pair) }

    if partial_pair && object_pair
      partial_name = partial_key?(partial_pair)
      object_sym, object_node = object_key?(object_pair)

      case object_sym
      when :object
        if partial_name.children[0].is_a?(String)
          suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`"
        end
        add_offense(node, location: :expression, message: "Avoid `render object:`#{suggestion}")
      when :collection, :spacer_template
        add_offense(node, location: :expression, message: "Avoid `render collection:`")
      end
    end
  end
end