-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathactive_job_performable.rb
52 lines (42 loc) · 1.22 KB
/
active_job_performable.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
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
module RuboCop
module Cop
module Betterment
class ActiveJobPerformable < Cop
MSG = <<~DOC
Classes that are "performable" should be ActiveJobs
class MyJob < ApplicationJob
def perform
end
end
You can learn more about ActiveJob here:
https://guides.rubyonrails.org/active_job_basics.html
DOC
def_node_matcher :subclasses_application_job?, <<-PATTERN
(class (const ...) (const _ :ApplicationJob) ...)
PATTERN
def_node_matcher :is_perform_method?, <<-PATTERN
(def :perform ...)
PATTERN
def on_class(node)
return if subclasses_application_job?(node)
return unless has_perform_method?(node)
add_offense(node.identifier)
end
private
def has_perform_method?(node)
possible_methods_within(node).any? { |n| is_perform_method?(n) }
end
def possible_methods_within(node)
if node.body.nil?
[]
elsif node.body.begin_type?
node.body.children
else
[node.body]
end
end
end
end
end
end