Skip to content

Commit 1e25bd8

Browse files
authored
Merge pull request #924 from flippercloud/lazy-load-warning
ActiveRecord Loading Stuff
2 parents c46f433 + 6164a7a commit 1e25bd8

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

lib/flipper/adapters/active_record.rb

+33-30
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,33 @@ module Adapters
88
class ActiveRecord
99
include ::Flipper::Adapter
1010

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
1614

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
2422

25-
has_many :gates, foreign_key: "feature_key", primary_key: "key"
23+
has_many :gates, foreign_key: "feature_key", primary_key: "key"
2624

27-
validates :key, presence: true
28-
end
25+
validates :key, presence: true
26+
end
2927

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
3735

38-
validates :feature_key, presence: true
39-
validates :key, presence: true
40-
end
36+
validates :feature_key, presence: true
37+
validates :key, presence: true
4138
end
4239

4340
VALUE_TO_TEXT_WARNING = <<-EOS
@@ -59,10 +56,8 @@ class Gate < Model
5956
# can roll your own tables and what not, if you so desire.
6057
def initialize(options = {})
6158
@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 }
6661
end
6762

6863
# Public: The set of known features.
@@ -289,15 +284,23 @@ def result_for_gates(feature, gates)
289284
# Check if value column is text instead of string
290285
# See https://github.com/flippercloud/flipper/pull/692
291286
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
293290
rescue ::ActiveRecord::ActiveRecordError => error
294291
# If the table doesn't exist, the column doesn't exist either
295292
warn "#{error.message}. You likely need to run `rails g flipper:active_record` and/or `rails db:migrate`."
296293
end
297294

298295
def with_connection(model = @feature_class, &block)
296+
warn VALUE_TO_TEXT_WARNING if !warned_about_value_not_text? && value_not_text?
299297
model.connection_pool.with_connection(&block)
300298
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
301304
end
302305
end
303306
end

0 commit comments

Comments
 (0)