forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_inline.rb
41 lines (38 loc) · 1.09 KB
/
render_inline.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Looks for inline rendering within controller actions.
#
# @example
# # bad
# class ProductsController < ApplicationController
# def index
# render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>", type: :erb
# end
# end
#
# # good
# # app/views/products/index.html.erb
# # <% products.each do |p| %>
# # <p><%= p.name %></p>
# # <% end %>
#
# class ProductsController < ApplicationController
# def index
# end
# end
#
class RenderInline < Base
MSG = 'Prefer using a template over inline rendering.'
RESTRICT_ON_SEND = %i[render].freeze
def_node_matcher :render_with_inline_option?, <<~PATTERN
(send nil? :render (hash <(pair {(sym :inline) (str "inline")} _) ...>))
PATTERN
def on_send(node)
add_offense(node) if render_with_inline_option?(node)
end
end
end
end
end