flipper-active_record 0.16.2 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/docs/active_record/README.md +3 -2
- data/flipper-active_record.gemspec +1 -2
- data/lib/flipper-active_record.rb +2 -0
- data/lib/flipper/adapters/active_record.rb +27 -27
- data/lib/flipper/version.rb +1 -1
- data/lib/generators/flipper/active_record_generator.rb +3 -3
- data/spec/flipper/adapters/active_record_requires_spec.rb +5 -0
- data/test_rails/generators/flipper/active_record_generator_test.rb +43 -0
- metadata +13 -13
- data/test/generators/flipper/active_record_generator_test.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3e08a444f2dc77250d2778470880417655887189faca0140f7943bb19898c6f8
|
4
|
+
data.tar.gz: 70a469cb0c4ca84d51278efbe882f54bb9c2e77853897cab31412f5726221d39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7654b4e38f19571c52d1d0aac2bb36d13212d4c73d303b571043c5e3294de151ca8e99ca6a61782200999f068b8b0211fdf8c58a61fdd8748ffe5b0bda7261c0
|
7
|
+
data.tar.gz: a7a36c4533c0a066a828ac2db5e4a666a13661c3f7b5cee46413432fad81129db70f1ab40feec3e7022db0078d40fcba879964bab66f17a29c7c6a1def4d39a0
|
@@ -4,9 +4,8 @@ An ActiveRecord adapter for [Flipper](https://github.com/jnunemaker/flipper).
|
|
4
4
|
|
5
5
|
Supported Active Record versions:
|
6
6
|
|
7
|
-
* 3.2.x
|
8
|
-
* 4.2.x
|
9
7
|
* 5.0.x
|
8
|
+
* 6.0.x
|
10
9
|
|
11
10
|
## Installation
|
12
11
|
|
@@ -37,6 +36,8 @@ flipper = Flipper.new(adapter)
|
|
37
36
|
# profit...
|
38
37
|
```
|
39
38
|
|
39
|
+
Note that the active record adapter requires the database tables to be created in order to work; failure to run the migration first will cause an exception to be raised when attempting to initialize the active record adapter.
|
40
|
+
|
40
41
|
## Internals
|
41
42
|
|
42
43
|
Each feature is stored as a row in a features table. Each gate is stored as a row in a gates table, related to the feature by the feature's key.
|
@@ -10,7 +10,6 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.authors = ['John Nunemaker']
|
11
11
|
gem.email = ['nunemaker@gmail.com']
|
12
12
|
gem.summary = 'ActiveRecord adapter for Flipper'
|
13
|
-
gem.description = 'ActiveRecord adapter for Flipper'
|
14
13
|
gem.license = 'MIT'
|
15
14
|
gem.homepage = 'https://github.com/jnunemaker/flipper'
|
16
15
|
|
@@ -26,5 +25,5 @@ Gem::Specification.new do |gem|
|
|
26
25
|
gem.metadata = Flipper::METADATA
|
27
26
|
|
28
27
|
gem.add_dependency 'flipper', "~> #{Flipper::VERSION}"
|
29
|
-
gem.add_dependency 'activerecord', '>=
|
28
|
+
gem.add_dependency 'activerecord', '>= 5.0', '< 7'
|
30
29
|
end
|
@@ -54,12 +54,16 @@ module Flipper
|
|
54
54
|
# Public: Adds a feature to the set of known features.
|
55
55
|
def add(feature)
|
56
56
|
# race condition, but add is only used by enable/disable which happen
|
57
|
-
# super rarely, so it shouldn't matter in practice
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
# super rarely, so it shouldn't matter in practice
|
58
|
+
@feature_class.transaction do
|
59
|
+
unless @feature_class.where(key: feature.key).first
|
60
|
+
begin
|
61
|
+
@feature_class.create! { |f| f.key = feature.key }
|
62
|
+
rescue ::ActiveRecord::RecordNotUnique
|
63
|
+
end
|
64
|
+
end
|
62
65
|
end
|
66
|
+
|
63
67
|
true
|
64
68
|
end
|
65
69
|
|
@@ -97,7 +101,7 @@ module Flipper
|
|
97
101
|
end
|
98
102
|
|
99
103
|
def get_all
|
100
|
-
rows = ::ActiveRecord::Base.connection.select_all <<-SQL
|
104
|
+
rows = ::ActiveRecord::Base.connection.select_all <<-SQL.tr("\n", ' ')
|
101
105
|
SELECT ff.key AS feature_key, fg.key, fg.value
|
102
106
|
FROM #{@feature_class.table_name} ff
|
103
107
|
LEFT JOIN #{@gate_class.table_name} fg ON ff.key = fg.feature_key
|
@@ -121,8 +125,10 @@ module Flipper
|
|
121
125
|
# Returns true.
|
122
126
|
def enable(feature, gate, thing)
|
123
127
|
case gate.data_type
|
124
|
-
when :boolean
|
125
|
-
|
128
|
+
when :boolean
|
129
|
+
set(feature, gate, thing, clear: true)
|
130
|
+
when :integer
|
131
|
+
set(feature, gate, thing)
|
126
132
|
when :set
|
127
133
|
enable_multi(feature, gate, thing)
|
128
134
|
else
|
@@ -144,18 +150,7 @@ module Flipper
|
|
144
150
|
when :boolean
|
145
151
|
clear(feature)
|
146
152
|
when :integer
|
147
|
-
|
148
|
-
@gate_class.where(
|
149
|
-
feature_key: feature.key,
|
150
|
-
key: gate.key
|
151
|
-
).destroy_all
|
152
|
-
|
153
|
-
@gate_class.create! do |g|
|
154
|
-
g.feature_key = feature.key
|
155
|
-
g.key = gate.key
|
156
|
-
g.value = thing.value.to_s
|
157
|
-
end
|
158
|
-
end
|
153
|
+
set(feature, gate, thing)
|
159
154
|
when :set
|
160
155
|
@gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).destroy_all
|
161
156
|
else
|
@@ -172,8 +167,10 @@ module Flipper
|
|
172
167
|
|
173
168
|
private
|
174
169
|
|
175
|
-
def
|
170
|
+
def set(feature, gate, thing, options = {})
|
171
|
+
clear_feature = options.fetch(:clear, false)
|
176
172
|
@gate_class.transaction do
|
173
|
+
clear(feature) if clear_feature
|
177
174
|
@gate_class.where(feature_key: feature.key, key: gate.key).destroy_all
|
178
175
|
@gate_class.create! do |g|
|
179
176
|
g.feature_key = feature.key
|
@@ -181,6 +178,8 @@ module Flipper
|
|
181
178
|
g.value = thing.value.to_s
|
182
179
|
end
|
183
180
|
end
|
181
|
+
|
182
|
+
nil
|
184
183
|
end
|
185
184
|
|
186
185
|
def enable_multi(feature, gate, thing)
|
@@ -189,9 +188,10 @@ module Flipper
|
|
189
188
|
g.key = gate.key
|
190
189
|
g.value = thing.value.to_s
|
191
190
|
end
|
191
|
+
|
192
|
+
nil
|
192
193
|
rescue ::ActiveRecord::RecordNotUnique
|
193
|
-
|
194
|
-
raise unless error.message =~ /unique/i
|
194
|
+
# already added so no need move on with life
|
195
195
|
end
|
196
196
|
|
197
197
|
def result_for_feature(feature, db_gates)
|
@@ -201,12 +201,12 @@ module Flipper
|
|
201
201
|
result[gate.key] =
|
202
202
|
case gate.data_type
|
203
203
|
when :boolean
|
204
|
-
if
|
205
|
-
|
204
|
+
if detected_db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s }
|
205
|
+
detected_db_gate.value
|
206
206
|
end
|
207
207
|
when :integer
|
208
|
-
if
|
209
|
-
|
208
|
+
if detected_db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s }
|
209
|
+
detected_db_gate.value
|
210
210
|
end
|
211
211
|
when :set
|
212
212
|
db_gates.select { |db_gate| db_gate.key == gate.key.to_s }.map(&:value).to_set
|
data/lib/flipper/version.rb
CHANGED
@@ -13,11 +13,11 @@ module Flipper
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.migration_version
|
16
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if
|
16
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if requires_migration_number?
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.
|
20
|
-
Rails.
|
19
|
+
def self.requires_migration_number?
|
20
|
+
Rails::VERSION::MAJOR.to_i >= 5
|
21
21
|
end
|
22
22
|
|
23
23
|
def create_migration_file
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'active_record'
|
3
|
+
require 'rails/generators/test_case'
|
4
|
+
require 'generators/flipper/active_record_generator'
|
5
|
+
|
6
|
+
class FlipperActiveRecordGeneratorTest < Rails::Generators::TestCase
|
7
|
+
tests Flipper::Generators::ActiveRecordGenerator
|
8
|
+
destination File.expand_path('../../../../tmp', __FILE__)
|
9
|
+
setup :prepare_destination
|
10
|
+
|
11
|
+
def test_generates_migration
|
12
|
+
run_generator
|
13
|
+
migration_version = if Rails::VERSION::MAJOR.to_i < 5
|
14
|
+
""
|
15
|
+
else
|
16
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
17
|
+
end
|
18
|
+
assert_migration 'db/migrate/create_flipper_tables.rb', <<~MIGRATION
|
19
|
+
class CreateFlipperTables < ActiveRecord::Migration#{migration_version}
|
20
|
+
def self.up
|
21
|
+
create_table :flipper_features do |t|
|
22
|
+
t.string :key, null: false
|
23
|
+
t.timestamps null: false
|
24
|
+
end
|
25
|
+
add_index :flipper_features, :key, unique: true
|
26
|
+
|
27
|
+
create_table :flipper_gates do |t|
|
28
|
+
t.string :feature_key, null: false
|
29
|
+
t.string :key, null: false
|
30
|
+
t.string :value
|
31
|
+
t.timestamps null: false
|
32
|
+
end
|
33
|
+
add_index :flipper_gates, [:feature_key, :key, :value], unique: true
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.down
|
37
|
+
drop_table :flipper_gates
|
38
|
+
drop_table :flipper_features
|
39
|
+
end
|
40
|
+
end
|
41
|
+
MIGRATION
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flipper-active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.19.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: flipper
|
@@ -16,35 +16,35 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.19.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.19.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '5.0'
|
34
34
|
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: '
|
36
|
+
version: '7'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '
|
43
|
+
version: '5.0'
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
47
|
-
description:
|
46
|
+
version: '7'
|
47
|
+
description:
|
48
48
|
email:
|
49
49
|
- nunemaker@gmail.com
|
50
50
|
executables: []
|
@@ -61,9 +61,10 @@ files:
|
|
61
61
|
- lib/flipper/version.rb
|
62
62
|
- lib/generators/flipper/active_record_generator.rb
|
63
63
|
- lib/generators/flipper/templates/migration.erb
|
64
|
+
- spec/flipper/adapters/active_record_requires_spec.rb
|
64
65
|
- spec/flipper/adapters/active_record_spec.rb
|
65
66
|
- test/adapters/active_record_test.rb
|
66
|
-
-
|
67
|
+
- test_rails/generators/flipper/active_record_generator_test.rb
|
67
68
|
homepage: https://github.com/jnunemaker/flipper
|
68
69
|
licenses:
|
69
70
|
- MIT
|
@@ -84,12 +85,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
- !ruby/object:Gem::Version
|
85
86
|
version: '0'
|
86
87
|
requirements: []
|
87
|
-
|
88
|
-
rubygems_version: 2.4.5.4
|
88
|
+
rubygems_version: 3.0.3
|
89
89
|
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: ActiveRecord adapter for Flipper
|
92
92
|
test_files:
|
93
|
+
- spec/flipper/adapters/active_record_requires_spec.rb
|
93
94
|
- spec/flipper/adapters/active_record_spec.rb
|
94
95
|
- test/adapters/active_record_test.rb
|
95
|
-
- test/generators/flipper/active_record_generator_test.rb
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'active_record'
|
3
|
-
require 'rails/generators/test_case'
|
4
|
-
require 'generators/flipper/active_record_generator'
|
5
|
-
|
6
|
-
class FlipperActiveRecordGeneratorTest < Rails::Generators::TestCase
|
7
|
-
tests Flipper::Generators::ActiveRecordGenerator
|
8
|
-
destination File.expand_path('../../../../tmp', __FILE__)
|
9
|
-
setup :prepare_destination
|
10
|
-
|
11
|
-
def test_generates_migration
|
12
|
-
run_generator
|
13
|
-
migration_version = if Rails::VERSION::MAJOR.to_i < 5
|
14
|
-
""
|
15
|
-
else
|
16
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
17
|
-
end
|
18
|
-
assert_migration 'db/migrate/create_flipper_tables.rb', <<-EOM
|
19
|
-
class CreateFlipperTables < ActiveRecord::Migration#{migration_version}
|
20
|
-
def self.up
|
21
|
-
create_table :flipper_features do |t|
|
22
|
-
t.string :key, null: false
|
23
|
-
t.timestamps null: false
|
24
|
-
end
|
25
|
-
add_index :flipper_features, :key, unique: true
|
26
|
-
|
27
|
-
create_table :flipper_gates do |t|
|
28
|
-
t.string :feature_key, null: false
|
29
|
-
t.string :key, null: false
|
30
|
-
t.string :value
|
31
|
-
t.timestamps null: false
|
32
|
-
end
|
33
|
-
add_index :flipper_gates, [:feature_key, :key, :value], unique: true
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.down
|
37
|
-
drop_table :flipper_gates
|
38
|
-
drop_table :flipper_features
|
39
|
-
end
|
40
|
-
end
|
41
|
-
EOM
|
42
|
-
end
|
43
|
-
end
|