-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
auto_filtered_parameters.rb
66 lines (55 loc) · 1.86 KB
/
auto_filtered_parameters.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
# frozen_string_literal: true
module ActiveRecord
module Encryption
class AutoFilteredParameters
def initialize(app)
@app = app
@attributes_by_class = Concurrent::Map.new
@collecting = true
install_collecting_hook
end
def enable
apply_collected_attributes
@collecting = false
end
private
attr_reader :app
def install_collecting_hook
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, attribute|
attribute_was_declared(klass, attribute)
end
end
def attribute_was_declared(klass, attribute)
if collecting?
collect_for_later(klass, attribute)
else
apply_filter(klass, attribute)
end
end
def apply_collected_attributes
@attributes_by_class.each do |klass, attributes|
attributes.each do |attribute|
apply_filter(klass, attribute)
end
end
end
def collecting?
@collecting
end
def collect_for_later(klass, attribute)
@attributes_by_class[klass] ||= Concurrent::Array.new
@attributes_by_class[klass] << attribute
end
def apply_filter(klass, attribute)
filter = [("#{klass.model_name.element}" if klass.name), attribute.to_s].compact.join(".")
unless excluded_from_filter_parameters?(filter)
app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter)
klass.filter_attributes += [ attribute ]
end
end
def excluded_from_filter_parameters?(filter_parameter)
ActiveRecord::Encryption.config.excluded_from_filter_parameters.find { |excluded_filter| excluded_filter.to_s == filter_parameter }
end
end
end
end