@@ -8,36 +8,33 @@ module Adapters
8
8
class ActiveRecord
9
9
include ::Flipper ::Adapter
10
10
11
- ActiveSupport . on_load ( :active_record ) do
12
- # Abstract base class for internal models
13
- class Model < ::ActiveRecord ::Base
14
- self . abstract_class = true
15
- end
11
+ class Model < ::ActiveRecord ::Base
12
+ self . abstract_class = true
13
+ end
16
14
17
- # Private: Do not use outside of this adapter.
18
- class Feature < Model
19
- self . table_name = [
20
- Model . table_name_prefix ,
21
- "flipper_features" ,
22
- Model . table_name_suffix ,
23
- ] . join
15
+ # Private: Do not use outside of this adapter.
16
+ class Feature < Model
17
+ self . table_name = [
18
+ Model . table_name_prefix ,
19
+ "flipper_features" ,
20
+ Model . table_name_suffix ,
21
+ ] . join
24
22
25
- has_many :gates , foreign_key : "feature_key" , primary_key : "key"
23
+ has_many :gates , foreign_key : "feature_key" , primary_key : "key"
26
24
27
- validates :key , presence : true
28
- end
25
+ validates :key , presence : true
26
+ end
29
27
30
- # Private: Do not use outside of this adapter.
31
- class Gate < Model
32
- self . table_name = [
33
- Model . table_name_prefix ,
34
- "flipper_gates" ,
35
- Model . table_name_suffix ,
36
- ] . join
28
+ # Private: Do not use outside of this adapter.
29
+ class Gate < Model
30
+ self . table_name = [
31
+ Model . table_name_prefix ,
32
+ "flipper_gates" ,
33
+ Model . table_name_suffix ,
34
+ ] . join
37
35
38
- validates :feature_key , presence : true
39
- validates :key , presence : true
40
- end
36
+ validates :feature_key , presence : true
37
+ validates :key , presence : true
41
38
end
42
39
43
40
VALUE_TO_TEXT_WARNING = <<-EOS
@@ -59,10 +56,8 @@ class Gate < Model
59
56
# can roll your own tables and what not, if you so desire.
60
57
def initialize ( options = { } )
61
58
@name = options . fetch ( :name , :active_record )
62
- @feature_class = options . fetch ( :feature_class ) { Feature }
63
- @gate_class = options . fetch ( :gate_class ) { Gate }
64
-
65
- warn VALUE_TO_TEXT_WARNING if value_not_text?
59
+ @feature_class = options . fetch ( :feature_class ) { Flipper ::Adapters ::ActiveRecord ::Feature }
60
+ @gate_class = options . fetch ( :gate_class ) { Flipper ::Adapters ::ActiveRecord ::Gate }
66
61
end
67
62
68
63
# Public: The set of known features.
@@ -289,15 +284,23 @@ def result_for_gates(feature, gates)
289
284
# Check if value column is text instead of string
290
285
# See https://github.com/flippercloud/flipper/pull/692
291
286
def value_not_text?
292
- @gate_class . column_for_attribute ( :value ) . type != :text
287
+ with_connection ( @gate_class ) do |connection |
288
+ @gate_class . column_for_attribute ( :value ) . type != :text
289
+ end
293
290
rescue ::ActiveRecord ::ActiveRecordError => error
294
291
# If the table doesn't exist, the column doesn't exist either
295
292
warn "#{ error . message } . You likely need to run `rails g flipper:active_record` and/or `rails db:migrate`."
296
293
end
297
294
298
295
def with_connection ( model = @feature_class , &block )
296
+ warn VALUE_TO_TEXT_WARNING if !warned_about_value_not_text? && value_not_text?
299
297
model . connection_pool . with_connection ( &block )
300
298
end
299
+
300
+ def warned_about_value_not_text?
301
+ return @warned_about_value_not_text if defined? ( @warned_about_value_not_text )
302
+ @warned_about_value_not_text = true
303
+ end
301
304
end
302
305
end
303
306
end
0 commit comments