-
Notifications
You must be signed in to change notification settings - Fork 3
/
audit-callbacks.rb
executable file
·75 lines (66 loc) · 2.07 KB
/
audit-callbacks.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
# Prints a list of Active Record callbacks for the given models
#
# Usage:
# bundle exec audit-callbacks.rb [--skip-line-num] [Model1 Model2...]
#
skip_line_num = false
if ARGV.first == "--skip-line-num"
ARGV.shift
skip_line_num = true
end
unless ARGV.size > 0
$stderr.puts "No models specified. Try script/audit-callbacks User Issue."
exit 1
end
# Arca must be required after ActiveRecord, but before the environment loads so
# we can install Arca::Collector.
require "active_record"
require "arca"
class ActiveRecord::Base
include Arca::Collector
end
require "config/environment"
# Returns a formatted string of a callback analysis.
#
# Examples:
#
# An unnamed (block) before_destroy callback defined in the same file as the model
# Ability before_destroy app/models/ability.rb external:false block
#
# A after_destroy callback named `dereference_asset` defined in a file outside of the model (external:true)
# Avatar after_destroy app/models/asset_uploadable.rb external:true dereference_asset
#
# A before_save callback defined by a callback class
# IssueComment before_save app/models/referrer.rb external:true Referrer::ReferenceMentionsCallback
#
def format_callback(callback, skip_line_num:)
path = Arca.relative_path(callback.callback_file_path)
path << ":#{callback.callback_line_number}" unless skip_line_num
# Arca outputs object ids which triggers a diff
# e.g. #<Referrer::ReferenceMentionsCallback:0x007f8bca3a1b48>
target = if match = /#<(.+):.+>/.match(callback.target_symbol.to_s)
match[1]
else
callback.target_symbol
end
[
callback.model.name,
callback.callback_symbol,
path,
"external:#{callback.external_callback?}",
target
].map(&:to_s).join(" ")
end
ARGV.each do |model_name|
begin
model_class = model_name.constantize
rescue NameError # test classes
next
end
next unless model_class.ancestors.include?(ActiveRecord::Base)
Arca[model_class].analyzed_callbacks.each do |callback_symbol, callbacks|
callbacks.each do |callback|
puts format_callback(callback, skip_line_num: skip_line_num)
end
end
end