flipper-active_record 0.20.4 → 0.22.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64603c99add7840b0cbb3583e665de4e1d60f3b2a8c26f844521002188c7e414
4
- data.tar.gz: 69786113e31245190f91e98b33b5e8d64111cfb4112fd46bd87a3bb3ff0556a1
3
+ metadata.gz: 4d6c72b579bf974d75846d8b6e149f0790736f16848fe4b0e54a7fa2907262c8
4
+ data.tar.gz: de4029f2a4a2e6c8a869cfc1c2eb15b65d97ae627844b22fdc0274c4c9bf8e83
5
5
  SHA512:
6
- metadata.gz: 125595ff73002af3c0c3a0e6e571cc81c1c9aa6637fe4352c49fed96d53cc89f15226e9a32b1ac68ef7547a376d0f2c50776d98b8a345a80c2f4a574d95d4945
7
- data.tar.gz: 282136e3f700b8d2232faaa6505d3293cd8e19a7e87fa7a0acfc6987938d3f3857a642d609c4c040577fb4394bb5c27f936eb9413f108f4724960fcb5b8a5b0d
6
+ metadata.gz: cf9efccca1b72b1ef7c99b822ad6507a8fb5040f1460b6a2ed4eacc4d62278f72c1419db54a05e0c2ad3348b6dde4d76f011c314b41bc6ffb9f35e07e1f08a18
7
+ data.tar.gz: b864b6658ef23db29a0f3007fb24b7f641f26d59be006473102b39c2c42e6905ec776df28e2a47e5ab6e8d780f41862aae1a3b7ef9f8a8ac80a693beb7000501
@@ -23,30 +23,26 @@ Or install it yourself with:
23
23
 
24
24
  ## Usage
25
25
 
26
- For your convenience a migration generator is provided to create the necessary migrations for using the active record adapter. By default this generates a migration that will create two database tables - flipper_features and flipper_gates.
26
+ For your convenience a migration generator is provided to create the necessary migrations for using the active record adapter. By default this generates a migration that will create two database tables - `flipper_features` and `flipper_gates`.
27
27
 
28
28
  $ rails g flipper:active_record
29
29
 
30
- Once you have created and executed the migration, you can use the active record adapter like so:
30
+ 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.
31
+
32
+ Flipper will be configured to use the ActiveRecord adapter when `flipper-active_record` is loaded. But **if you need to customize the adapter**, you can add this to an initializer:
31
33
 
32
34
  ```ruby
33
35
  require 'flipper/adapters/active_record'
34
- adapter = Flipper::Adapters::ActiveRecord.new
35
- flipper = Flipper.new(adapter)
36
- # profit...
36
+ Flipper.configure do |config|
37
+ config.adapter { Flipper::Adapters::ActiveRecord.new }
38
+ end
37
39
  ```
38
40
 
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
-
41
41
  ## Internals
42
42
 
43
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.
44
44
 
45
45
  ```ruby
46
- require 'flipper/adapters/active_record'
47
- adapter = Flipper::Adapters::ActiveRecord.new
48
- flipper = Flipper.new(adapter)
49
-
50
46
  # Register a few groups.
51
47
  Flipper.register(:admins) { |thing| thing.admin? }
52
48
  Flipper.register(:early_access) { |thing| thing.early_access? }
@@ -54,16 +50,16 @@ Flipper.register(:early_access) { |thing| thing.early_access? }
54
50
  # Create a user class that has flipper_id instance method.
55
51
  User = Struct.new(:flipper_id)
56
52
 
57
- flipper[:stats].enable
58
- flipper[:stats].enable_group :admins
59
- flipper[:stats].enable_group :early_access
60
- flipper[:stats].enable_actor User.new('25')
61
- flipper[:stats].enable_actor User.new('90')
62
- flipper[:stats].enable_actor User.new('180')
63
- flipper[:stats].enable_percentage_of_time 15
64
- flipper[:stats].enable_percentage_of_actors 45
53
+ Flipper.enable :stats
54
+ Flipper.enable_group :stats, :admins
55
+ Flipper.enable_group :stats, :early_access
56
+ Flipper.enable_actor :stats, User.new('25')
57
+ Flipper.enable_actor :stats, User.new('90')
58
+ Flipper.enable_actor :stats, User.new('180')
59
+ Flipper.enable_percentage_of_time :stats, 15
60
+ Flipper.enable_percentage_of_actors :stats, 45
65
61
 
66
- flipper[:search].enable
62
+ Flipper.enable :search
67
63
 
68
64
  puts 'all rows in features table'
69
65
  pp Flipper::Adapters::ActiveRecord::Feature.all
@@ -1,10 +1,4 @@
1
- require 'pathname'
2
- require 'logger'
3
-
4
- root_path = Pathname(__FILE__).dirname.join('..').expand_path
5
- lib_path = root_path.join('lib')
6
- $:.unshift(lib_path)
7
-
1
+ require 'bundler/setup'
8
2
  require 'active_record'
9
3
  ActiveRecord::Base.establish_connection({
10
4
  adapter: 'sqlite3',
@@ -3,20 +3,17 @@ require_relative "./ar_setup"
3
3
  # Requires the flipper-active_record gem to be installed.
4
4
  require 'flipper/adapters/active_record'
5
5
 
6
- adapter = Flipper::Adapters::ActiveRecord.new
7
- flipper = Flipper.new(adapter)
6
+ Flipper[:stats].enable
8
7
 
9
- flipper[:stats].enable
10
-
11
- if flipper[:stats].enabled?
8
+ if Flipper[:stats].enabled?
12
9
  puts "Enabled!"
13
10
  else
14
11
  puts "Disabled!"
15
12
  end
16
13
 
17
- flipper[:stats].disable
14
+ Flipper[:stats].disable
18
15
 
19
- if flipper[:stats].enabled?
16
+ if Flipper[:stats].enabled?
20
17
  puts "Enabled!"
21
18
  else
22
19
  puts "Disabled!"
@@ -2,8 +2,6 @@ require_relative "./ar_setup"
2
2
 
3
3
  # Requires the flipper-active_record gem to be installed.
4
4
  require 'flipper/adapters/active_record'
5
- adapter = Flipper::Adapters::ActiveRecord.new
6
- flipper = Flipper.new(adapter)
7
5
 
8
6
  # Register a few groups.
9
7
  Flipper.register(:admins) { |thing| thing.admin? }
@@ -12,16 +10,16 @@ Flipper.register(:early_access) { |thing| thing.early_access? }
12
10
  # Create a user class that has flipper_id instance method.
13
11
  User = Struct.new(:flipper_id)
14
12
 
15
- flipper[:stats].enable
16
- flipper[:stats].enable_group :admins
17
- flipper[:stats].enable_group :early_access
18
- flipper[:stats].enable_actor User.new('25')
19
- flipper[:stats].enable_actor User.new('90')
20
- flipper[:stats].enable_actor User.new('180')
21
- flipper[:stats].enable_percentage_of_time 15
22
- flipper[:stats].enable_percentage_of_actors 45
13
+ Flipper[:stats].enable
14
+ Flipper[:stats].enable_group :admins
15
+ Flipper[:stats].enable_group :early_access
16
+ Flipper[:stats].enable_actor User.new('25')
17
+ Flipper[:stats].enable_actor User.new('90')
18
+ Flipper[:stats].enable_actor User.new('180')
19
+ Flipper[:stats].enable_percentage_of_time 15
20
+ Flipper[:stats].enable_percentage_of_actors 45
23
21
 
24
- flipper[:search].enable
22
+ Flipper[:search].enable
25
23
 
26
24
  puts 'all rows in features table'
27
25
  pp Flipper::Adapters::ActiveRecord::Feature.all
@@ -105,5 +103,5 @@ pp Flipper::Adapters::ActiveRecord::Gate.all
105
103
  puts
106
104
 
107
105
  puts 'flipper get of feature'
108
- pp adapter.get(flipper[:stats])
106
+ pp Flipper.adapter.get(Flipper[:stats])
109
107
  # flipper get of feature
@@ -25,5 +25,5 @@ Gem::Specification.new do |gem|
25
25
  gem.metadata = Flipper::METADATA
26
26
 
27
27
  gem.add_dependency 'flipper', "~> #{Flipper::VERSION}"
28
- gem.add_dependency 'activerecord', '>= 5.0', '< 7'
28
+ gem.add_dependency 'activerecord', '>= 4.2', '< 7'
29
29
  end
@@ -101,11 +101,12 @@ module Flipper
101
101
  end
102
102
 
103
103
  def get_all
104
- rows = ::ActiveRecord::Base.connection.select_all <<-SQL.tr("\n", ' ')
105
- SELECT ff.key AS feature_key, fg.key, fg.value
106
- FROM #{@feature_class.table_name} ff
107
- LEFT JOIN #{@gate_class.table_name} fg ON ff.key = fg.feature_key
108
- SQL
104
+ features = ::Arel::Table.new(@feature_class.table_name.to_sym)
105
+ gates = ::Arel::Table.new(@gate_class.table_name.to_sym)
106
+ rows_query = features.join(gates, Arel::Nodes::OuterJoin)
107
+ .on(features[:key].eq(gates[:feature_key]))
108
+ .project(features[:key].as('feature_key'), gates[:key], gates[:value])
109
+ rows = ::ActiveRecord::Base.connection.select_all rows_query
109
110
  db_gates = rows.map { |row| Gate.new(row) }
110
111
  grouped_db_gates = db_gates.group_by(&:feature_key)
111
112
  result = Hash.new { |hash, key| hash[key] = default_config }
@@ -219,3 +220,7 @@ module Flipper
219
220
  end
220
221
  end
221
222
  end
223
+
224
+ Flipper.configure do |config|
225
+ config.adapter { Flipper::Adapters::ActiveRecord.new }
226
+ end
@@ -1,3 +1,3 @@
1
1
  module Flipper
2
- VERSION = '0.20.4'.freeze
2
+ VERSION = '0.22.0'.freeze
3
3
  end
@@ -45,4 +45,18 @@ RSpec.describe Flipper::Adapters::ActiveRecord do
45
45
  end
46
46
 
47
47
  it_should_behave_like 'a flipper adapter'
48
+
49
+ context 'requiring "flipper-active_record"' do
50
+ before do
51
+ Flipper.configuration = nil
52
+ Flipper.instance = nil
53
+
54
+ load 'flipper/adapters/active_record.rb'
55
+ ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
56
+ end
57
+
58
+ it 'configures itself' do
59
+ expect(Flipper.adapter.adapter).to be_a(Flipper::Adapters::ActiveRecord)
60
+ end
61
+ end
48
62
  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.20.4
4
+ version: 0.22.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: 2021-03-06 00:00:00.000000000 Z
11
+ date: 2021-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flipper
@@ -16,21 +16,21 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.20.4
19
+ version: 0.22.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.20.4
26
+ version: 0.22.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: '5.0'
33
+ version: '4.2'
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
36
  version: '7'
@@ -40,7 +40,7 @@ dependencies:
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: '5.0'
43
+ version: '4.2'
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '7'
@@ -61,7 +61,6 @@ 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
65
64
  - spec/flipper/adapters/active_record_spec.rb
66
65
  - test/adapters/active_record_test.rb
67
66
  - test_rails/generators/flipper/active_record_generator_test.rb
@@ -90,6 +89,5 @@ signing_key:
90
89
  specification_version: 4
91
90
  summary: ActiveRecord adapter for Flipper
92
91
  test_files:
93
- - spec/flipper/adapters/active_record_requires_spec.rb
94
92
  - spec/flipper/adapters/active_record_spec.rb
95
93
  - test/adapters/active_record_test.rb
@@ -1,5 +0,0 @@
1
- # This nominally-empty spec ensures that the file "flipper-active_record.rb" can be
2
- # required without error, and guards against regressions of the kind fixed in
3
- # https://github.com/jnunemaker/flipper/pull/437.
4
-
5
- require "flipper-active_record.rb"