flipper-active_record 0.16.0 → 0.18.0
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 +1 -2
- data/flipper-active_record.gemspec +1 -2
- data/lib/flipper-active_record.rb +2 -0
- data/lib/flipper/adapters/active_record.rb +31 -31
- 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: 976145403b3ed3058baca13ec4fba16b12d8f58ac0013782e5fed5dce324f074
|
4
|
+
data.tar.gz: 711d3a894b652d8d10b515bcabc43f2c70eeac1bbad0441f6794db87aa645d9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51be50553347b5880d78bec7980668ea0e65c2487aca9b09af4f68c37cb9ddb0d76246bd41e19c96ec2f6a86706bd1778e60d160537e9f16f48a86366219cf88
|
7
|
+
data.tar.gz: eafc203fc9db9f2adce2f73014f2bd176b82d2441a0f7405f3399101347de4139e19bf47786795a040cb32ff06b027c212e425aa2f1f4df5ba7740fb67a33cfa
|
@@ -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,19 +54,23 @@ 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
|
|
66
70
|
# Public: Removes a feature from the set of known features.
|
67
71
|
def remove(feature)
|
68
72
|
@feature_class.transaction do
|
69
|
-
@feature_class.where(key: feature.key).
|
73
|
+
@feature_class.where(key: feature.key).destroy_all
|
70
74
|
clear(feature)
|
71
75
|
end
|
72
76
|
true
|
@@ -74,7 +78,7 @@ module Flipper
|
|
74
78
|
|
75
79
|
# Public: Clears the gate values for a feature.
|
76
80
|
def clear(feature)
|
77
|
-
@gate_class.where(feature_key: feature.key).
|
81
|
+
@gate_class.where(feature_key: feature.key).destroy_all
|
78
82
|
true
|
79
83
|
end
|
80
84
|
|
@@ -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,20 +150,9 @@ 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
|
-
).delete_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
|
-
@gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).
|
155
|
+
@gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).destroy_all
|
161
156
|
else
|
162
157
|
unsupported_data_type gate.data_type
|
163
158
|
end
|
@@ -172,15 +167,19 @@ 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
|
177
|
-
|
173
|
+
clear(feature) if clear_feature
|
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
|
180
177
|
g.key = gate.key
|
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.18.0
|
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-06-25 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.18.0
|
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.18.0
|
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
|