@@ -22,7 +22,27 @@ def initialize
22
22
end
23
23
end
24
24
25
- class AllowedUrl
25
+ # Base class for core-consumers to implement Safeguards
26
+ #
27
+ class Base
28
+ def run
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def self . inherited ( subclass )
33
+ DatabaseCleaner ::Safeguard . registry << subclass
34
+ DatabaseCleaner ::Safeguard . deprecated_registry . reject! do |const |
35
+ subclass . name . split ( "::" ) . last == const . name . split ( "::" ) . last
36
+ end
37
+ end
38
+ end
39
+
40
+ # Just a marker class for Safeguards implemented by core that are kept for backwards compatibility.
41
+ # Adapters should implement their own Safeguards using Safeguard::Base.
42
+ #
43
+ class Deprecated ; end
44
+
45
+ class AllowedUrl < Deprecated
26
46
def run
27
47
return if skip?
28
48
raise Error ::UrlNotAllowed if database_url_not_allowed?
@@ -39,8 +59,7 @@ def skip?
39
59
end
40
60
end
41
61
42
-
43
- class RemoteDatabaseUrl
62
+ class RemoteDatabaseUrl < Deprecated
44
63
LOCAL = %w( localhost 127.0.0.1 )
45
64
46
65
def run
@@ -73,7 +92,7 @@ def skip?
73
92
end
74
93
end
75
94
76
- class Production
95
+ class Production < Deprecated
77
96
KEYS = %w( ENV APP_ENV RACK_ENV RAILS_ENV )
78
97
79
98
def run
@@ -96,14 +115,30 @@ def skip?
96
115
end
97
116
end
98
117
99
- CHECKS = [
118
+ def run
119
+ self . class . registry . each { |const | const . new . run }
120
+ self . class . deprecated_registry . each { |const | const . new . run }
121
+ end
122
+
123
+ @registry = [ ]
124
+ @deprecated_registry = [
100
125
RemoteDatabaseUrl ,
101
126
Production ,
102
127
AllowedUrl
103
128
]
104
129
105
- def run
106
- CHECKS . each { |const | const . new . run }
130
+ class << self
131
+ attr_reader :registry
132
+ attr_reader :deprecated_registry
133
+
134
+ def reset_registry!
135
+ @registry = [ ]
136
+ @deprecated_registry = [
137
+ RemoteDatabaseUrl ,
138
+ Production ,
139
+ AllowedUrl
140
+ ]
141
+ end
107
142
end
108
143
end
109
144
end
0 commit comments