forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_record_override.rb
82 lines (66 loc) · 2.09 KB
/
active_record_override.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks for overriding built-in Active Record methods instead of using
# callbacks.
#
# @example
# # bad
# class Book < ApplicationRecord
# def save
# self.title = title.upcase!
# super
# end
# end
#
# # good
# class Book < ApplicationRecord
# before_save :upcase_title
#
# def upcase_title
# self.title = title.upcase!
# end
# end
#
class ActiveRecordOverride < Cop
MSG =
'Use %<prefer>s callbacks instead of overriding the Active Record ' \
'method `%<bad>s`.'
BAD_METHODS = %i[create destroy save update].freeze
ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base
ActiveRecord::Base].freeze
def on_def(node)
return unless BAD_METHODS.include?(node.method_name)
parent_class_name = find_parent_class_name(node)
return unless active_model?(parent_class_name)
return unless node.descendants.any?(&:zsuper_type?)
add_offense(node, message: message(node.method_name))
end
private
def active_model?(parent_class_name)
ACTIVE_RECORD_CLASSES.include?(parent_class_name)
end
def callback_names(method_name)
names = %w[before_ around_ after_].map do |prefix|
"`#{prefix}#{method_name}`"
end
names[-1] = "or #{names.last}"
names.join(', ')
end
def message(method_name)
format(MSG, prefer: callback_names(method_name), bad: method_name)
end
def find_parent_class_name(node)
return nil unless node
if node.class_type?
parent_class_name = node.node_parts[1]
return nil if parent_class_name.nil?
return parent_class_name.source
end
find_parent_class_name(node.parent)
end
end
end
end
end