flipper-activerecord 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/flipper/activerecord/feature.rb +0 -1
- data/lib/flipper/adapters/activerecord.rb +35 -47
- data/lib/flipper/adapters/activerecord/version.rb +1 -1
- data/lib/generators/flipper/templates/migration.rb +2 -1
- data/spec/activerecord_spec.rb +1 -4
- data/spec/helper.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ec2097f8dbc9a98ef38c03221acee5ac110f2ba
|
4
|
+
data.tar.gz: da9f1e5cabe70e31c723d8e99f3bf86380ff1fb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1d30fd53b36dde9f18a0ee96a0f77f1c8e4e253765f2a2fc14881c1225ba62a9731d6cbd413b9de0dda210ac89adf151a7b845ccfacd6631f6cc658582d9f96
|
7
|
+
data.tar.gz: d0160bd0df31d7ef1605186092bde0e207eec478bf019e93ea62cbbc1e74a4da3a08cc16d2069a66d492996bc6fca35084f3552bf479bb968ae8723734b3c14d
|
data/Gemfile.lock
CHANGED
@@ -13,8 +13,7 @@ module Flipper
|
|
13
13
|
# Public: The name of the adapter.
|
14
14
|
attr_reader :name
|
15
15
|
|
16
|
-
def initialize
|
17
|
-
@collection = collection
|
16
|
+
def initialize
|
18
17
|
@name = :activerecord
|
19
18
|
end
|
20
19
|
|
@@ -25,7 +24,7 @@ module Flipper
|
|
25
24
|
|
26
25
|
# Public: Adds a feature to the set of known features.
|
27
26
|
def add(feature)
|
28
|
-
Flipper::ActiveRecord::Feature.
|
27
|
+
Flipper::ActiveRecord::Feature.find_or_create_by!(name: feature.name.to_s)
|
29
28
|
true
|
30
29
|
end
|
31
30
|
|
@@ -45,7 +44,7 @@ module Flipper
|
|
45
44
|
# Returns a Hash of Flipper::Gate#key => value.
|
46
45
|
def get(feature)
|
47
46
|
result = {}
|
48
|
-
f = Flipper::ActiveRecord::Feature.find_by(name: feature.key)
|
47
|
+
f = Flipper::ActiveRecord::Feature.eager_load(:gates).find_by(name: feature.key)
|
49
48
|
|
50
49
|
feature.gates.each do |gate|
|
51
50
|
result[gate.key] = case gate.data_type
|
@@ -76,23 +75,34 @@ module Flipper
|
|
76
75
|
#
|
77
76
|
# Returns true.
|
78
77
|
def enable(feature, gate, thing)
|
79
|
-
|
80
|
-
|
78
|
+
case gate.data_type
|
79
|
+
when :boolean, :integer
|
80
|
+
g = Flipper::ActiveRecord::Gate.joins(:feature).
|
81
|
+
where(flipper_features: {name: feature.key}).
|
82
|
+
find_or_initialize_by({
|
83
|
+
name: gate.key.to_s,
|
84
|
+
})
|
85
|
+
g.value = thing.value.to_s
|
86
|
+
unless g.persisted?
|
87
|
+
g.feature = Flipper::ActiveRecord::Feature.select(:id).find_or_create_by!(name: feature.key)
|
88
|
+
end
|
89
|
+
g.save!
|
90
|
+
when :set
|
91
|
+
g = Flipper::ActiveRecord::Gate.joins(:feature).
|
92
|
+
where(flipper_features: {name: feature.key}).
|
93
|
+
find_or_initialize_by({
|
94
|
+
name: gate.key.to_s,
|
95
|
+
value: thing.value.to_s,
|
96
|
+
})
|
97
|
+
unless g.persisted?
|
98
|
+
g.feature = Flipper::ActiveRecord::Feature.select(:id).find_or_create_by!(name: feature.key)
|
99
|
+
end
|
100
|
+
g.save!
|
101
|
+
else
|
102
|
+
unsupported_data_type gate.data_type
|
103
|
+
end
|
104
|
+
|
81
105
|
true
|
82
|
-
# case gate.data_type
|
83
|
-
# when :boolean, :integer
|
84
|
-
# update feature.key, '$set' => {
|
85
|
-
# gate.key.to_s => thing.value.to_s,
|
86
|
-
# }
|
87
|
-
# when :set
|
88
|
-
# update feature.key, '$addToSet' => {
|
89
|
-
# gate.key.to_s => thing.value.to_s,
|
90
|
-
# }
|
91
|
-
# else
|
92
|
-
# unsupported_data_type gate.data_type
|
93
|
-
# end
|
94
|
-
#
|
95
|
-
# true
|
96
106
|
end
|
97
107
|
|
98
108
|
# Public: Disables a gate for a given thing.
|
@@ -103,18 +113,16 @@ module Flipper
|
|
103
113
|
#
|
104
114
|
# Returns true.
|
105
115
|
def disable(feature, gate, thing)
|
106
|
-
|
116
|
+
scope = Flipper::ActiveRecord::Gate.joins(:feature).where(flipper_features: {name: feature.key})
|
107
117
|
|
108
118
|
g = case gate.data_type
|
109
119
|
when :boolean
|
110
|
-
|
120
|
+
scope.destroy_all
|
111
121
|
when :integer
|
112
|
-
|
113
|
-
|
114
|
-
limit(1).update_all(value: thing.value.to_s)
|
122
|
+
scope.where(name: gate.key.to_s).limit(1).
|
123
|
+
update_all(value: thing.value.to_s)
|
115
124
|
when :set
|
116
|
-
|
117
|
-
Flipper::ActiveRecord::Gate.joins(:feature).where(conditions).destroy_all
|
125
|
+
scope.where(name: gate.key.to_s, value: thing.value.to_s).destroy_all
|
118
126
|
else
|
119
127
|
unsupported_data_type gate.data_type
|
120
128
|
end
|
@@ -127,26 +135,6 @@ module Flipper
|
|
127
135
|
raise "#{data_type} is not supported by this adapter"
|
128
136
|
end
|
129
137
|
|
130
|
-
# Private
|
131
|
-
def find(key)
|
132
|
-
@collection.find_one(criteria(key)) || {}
|
133
|
-
end
|
134
|
-
|
135
|
-
# Private
|
136
|
-
def update(key, updates)
|
137
|
-
options = {:upsert => true}
|
138
|
-
@collection.update criteria(key), updates, options
|
139
|
-
end
|
140
|
-
|
141
|
-
# Private
|
142
|
-
def delete(key)
|
143
|
-
@collection.remove criteria(key)
|
144
|
-
end
|
145
|
-
|
146
|
-
# Private
|
147
|
-
def criteria(key)
|
148
|
-
{:_id => key.to_s}
|
149
|
-
end
|
150
138
|
end
|
151
139
|
end
|
152
140
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
class CreateFlipperTables < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :flipper_features do |t|
|
4
|
-
t.string :name, null: false
|
4
|
+
t.string :name, null: false
|
5
5
|
t.timestamps null: false
|
6
6
|
end
|
7
|
+
add_index :flipper_features, :name, unique: true
|
7
8
|
|
8
9
|
create_table :flipper_gates do |t|
|
9
10
|
t.integer :flipper_feature_id, null: false
|
data/spec/activerecord_spec.rb
CHANGED
@@ -3,10 +3,7 @@ require 'flipper/adapters/activerecord'
|
|
3
3
|
require 'flipper/spec/shared_adapter_specs'
|
4
4
|
|
5
5
|
describe Flipper::Adapters::ActiveRecord do
|
6
|
-
|
7
|
-
# Que.connection = ::ActiveRecord if defined? ::ActiveRecord
|
8
|
-
|
9
|
-
subject { described_class.new(pool) }
|
6
|
+
subject { described_class.new }
|
10
7
|
|
11
8
|
it_should_behave_like 'a flipper adapter'
|
12
9
|
end
|
data/spec/helper.rb
CHANGED
@@ -12,7 +12,7 @@ config = YAML.load(File.read("spec/database.yml"))
|
|
12
12
|
ActiveRecord::Base.establish_connection config["postgresql"]
|
13
13
|
ActiveRecord::Migration.verbose = true
|
14
14
|
|
15
|
-
require 'generators/flipper
|
15
|
+
require 'generators/flipper/templates/migration'
|
16
16
|
ActiveRecord::Schema.define do
|
17
17
|
CreateFlipperTables.up
|
18
18
|
|