Class: RuboCop::Cop::Betterment::NonStandardActions

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

Constant Summary collapse

MSG_GENERAL =
'Use a new controller instead of custom actions.'
MSG_RESOURCE_ONLY =
"Resource route refers to a non-standard action in it's 'only:' param. #{MSG_GENERAL}".freeze
MSG_ROUTE_TO =
"Route goes to a non-standard controller action. #{MSG_GENERAL}".freeze

Instance Method Summary collapse

Instance Method Details

#not_to_or_action?(sym) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rubocop/cop/betterment/non_standard_actions.rb', line 21

def not_to_or_action?(sym)
  !%i(to action).include?(sym)
end

#on_block(node) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubocop/cop/betterment/non_standard_actions.rb', line 33

def on_block(node)
  if routes?(node)
    node.each_descendant(:send) do |descendant_node|
      check_resource_with_only(descendant_node) || check_raw_route(descendant_node)
    end
  end
end

#resource_with_only(node) ⇒ Object



17
18
19
# File 'lib/rubocop/cop/betterment/non_standard_actions.rb', line 17

def_node_matcher :resource_with_only, <<-PATTERN
  (send nil? {:resource :resources} _ (hash <(pair (sym :only) {(array (sym $_)*) (sym $_*)} ) ...> ))
PATTERN

#route_to(node) ⇒ Object



26
27
28
29
30
31
# File 'lib/rubocop/cop/betterment/non_standard_actions.rb', line 26

def_node_matcher :route_to, <<~PATTERN
  (send nil? {:match :get :post :put :patch :delete} ({str sym} $_) (hash {
    <(pair (sym ${:to :action}) ({str sym} $_)) ...>
    (pair (sym $#not_to_or_action?) $_)*
  })?)
PATTERN

#routes?(node) ⇒ Object



12
13
14
# File 'lib/rubocop/cop/betterment/non_standard_actions.rb', line 12

def_node_matcher :routes?, <<-PATTERN
  (block (send (send (send (const nil? :Rails) :application) :routes) :draw) ...)
PATTERN