ffe 0.1.0 → 1.0.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: e4a4f8d7608dddb3df97d2e03b69b815cfd23662ef1f3eb83a934f5336a63b73
4
- data.tar.gz: fb39e4b322a411d8e93f29b0cdaefc27b12fc3c418efde0a74198e5301f51806
3
+ metadata.gz: 8714db1c7883a34c1d3d855f04531060fe1dfe11b568bee0508570f4e4c1942a
4
+ data.tar.gz: e781c9895aeeb7ccc047b684805216d81ee4e3adc1eece5e7ee3fe9078f4a715
5
5
  SHA512:
6
- metadata.gz: d3cdfbfc53a7015ceefed64e5c6ac7bfe364beea2f33e874ee946b0245ca7f90bc198c45a17fa77ab7be87b8b0e89395b91f6e73f2c2258077669942ea7758cb
7
- data.tar.gz: 6a7060e5583b18bf30e218663ea19bd7cd6f93fba0541c39d2a0015be3847b57d608befd54313482d3f3e54e0184a6181caa40970334fbd40bcb2712bca30fb0
6
+ metadata.gz: 5ca29b54bfe343aa8dd8fee221a0921bf8ce349b177e759d65efecdabe69d8c824a439d507b687d7bfd42bf72ab0492ed38365cad0febc90ef0c4f7239908002
7
+ data.tar.gz: 792f322fe595ec922daf165dee955ee7550a02ab6ef0354ef007fdb60ac44248f48b1c878ee0d324161a43b1b5d43c3a6e7dd42eabbefd45322be00734158f9b
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  A minimalistic Feature Flag engine, cause i was bored of unleash and other overloaded ones.
4
4
 
5
5
  ## Installation
6
+
6
7
  Add this line to your application's Gemfile:
7
8
 
8
9
  ```ruby
@@ -18,11 +19,23 @@ Copy over the initializer with:
18
19
  ```bash
19
20
  $ bin/rails g ffe:install
20
21
  ```
21
- and adjust it to your needs.
22
+ and adjust it to your needs.
23
+ Defaults are:
24
+ ```rb
25
+ config.bitlength = 4
26
+ config.milieus = { development: 0, staging: 1, production: 2 }
27
+ config.env_variable = 'RAILS_ENV'
28
+ # actual supported: :solid_queue, :sidekiq
29
+ config.queue_adapter = :solid_queue
30
+ ```
31
+
32
+ It also adds a loader for `feature_flags.json`, so you can easily load feature flags from a file.
33
+ But to this later under `Scenarios`.
22
34
 
23
35
  Install the migration with:
24
36
  ```bash
25
37
  $ bin/rails ffe:install:migrations
38
+ $ bin/rails db:migrate
26
39
  ```
27
40
  (have an eye on the length of the `milieu` bit mask)
28
41
 
@@ -31,12 +44,65 @@ Mount the engine in your application's `config/routes.rb` file:
31
44
  mount Ffe::Engine => "/feature_flags"
32
45
  ```
33
46
 
47
+ ### Views
48
+
49
+ The engine provides base views for operating on FeatureFlags.
50
+
51
+ But I recommend implement your own ones, as it is usually easier to customize. See: [views/ffe/feature_flags](https://github.com/LeFnord/ffe/tree/master/app/views/ffe/feature_flags).
52
+
34
53
  ## Usage
35
54
 
36
- coming soon
55
+ Run your app and go to [http://localhost:3000/ffe/feature_flags](http://localhost:3000/ffe/feature_flags) to create your first feature flag.
56
+
57
+ 1. general usage, respecting only the environment
58
+ ```rb
59
+ Ffe::FeatureFlag.enabled?(:flag)
60
+ # or
61
+ Ffe::FeatureFlag.disabled?(:flag)
62
+ ```
63
+
64
+
65
+ 2. check if a user is allowed
66
+ ```rb
67
+ Ffe::FeatureFlag.enabled_for?(:flag, user: current_user)
68
+ ```
69
+
70
+ ## Usage Scenarios
71
+
72
+
73
+ **Use Case**: Cleaning-Up -- The functionality is available to everyone.
74
+
75
+ > Please don't forget to remove feature flags after its use isn't needed anymore.
76
+
77
+ > Every unused feature flag, makes the code noisy and uncomfortable to read and understand.
78
+
79
+ 1. Clean up the code after the feature flag has been disabled, or doesn't changed over a long time!
80
+
81
+ **Use Case**: A new functionality is to be developed that is accessible to specific users and/or environments via a feature flag.
82
+
83
+ 1. Create a feature flag in production
84
+ 2. Dump the flags, then replace the `config/feature_flags.json` file locally and commit it.
85
+ 3. Continue developing against the feature as before.
86
+
87
+ **Use Case**: Functionality is available to only a small number of users.
88
+
89
+ 1. Create a feature flag in production, and add allowed users.
90
+ 2. Dump the flags, then replace the `config/feature_flags.json` file locally and commit it.
91
+ 3. Edit the flag locally and specify a corresponding user.
92
+ 4. Continue developing against the feature as before.
93
+
94
+ **Use Case**: Set a feature flag for an announcement, or something similar, for a specific period of time.
95
+
96
+ > This requires the use of ActiveJob; currently, only `SolidQueue` and `Sidekiq` are supported. But feel free to add more.
97
+
98
+ 1. Create a feature flag in production, but without setting the expires date.
99
+ 2. Dump the flags, then replace the `config/feature_flags.json` file locally and commit it.
100
+ 3. Continue developing against the feature as before.
101
+ 4. After deployment to production, set the expiration date on the flag -- the flag will be automatically disabled after the expiration date.
37
102
 
38
103
 
39
104
  ## Contributing
105
+
40
106
  Contribution directions go here.
41
107
 
42
108
  ## License
@@ -5,19 +5,19 @@ module Ffe
5
5
  before_action :set_ffe, only: %i[show edit update destroy]
6
6
 
7
7
  def index
8
- @feature_flags = ::Ffe::FeatureFlag.order(created_at: :desc)
8
+ @feature_flags = Ffe::FeatureFlag.order(created_at: :desc)
9
9
  end
10
10
 
11
11
  def show; end
12
12
 
13
13
  def new
14
- @feature_flag = ::Ffe::FeatureFlag.new
14
+ @feature_flag = Ffe::FeatureFlag.new
15
15
  end
16
16
 
17
17
  def edit; end
18
18
 
19
19
  def create
20
- @feature_flag = ::Ffe::FeatureFlag.new(feature_flag_params.except(:milieus))
20
+ @feature_flag = Ffe::FeatureFlag.new(feature_flag_params.except(:milieus, :clear_expires_at))
21
21
 
22
22
  if @feature_flag.save
23
23
  redirect_to feature_flag_path(@feature_flag), notice: 'FFE wurde erstellt.'
@@ -27,7 +27,7 @@ module Ffe
27
27
  end
28
28
 
29
29
  def update
30
- if @feature_flag.update(feature_flag_params.except(:milieus))
30
+ if @feature_flag.update(feature_flag_params.except(:milieus, :clear_expires_at))
31
31
  redirect_to feature_flag_path(@feature_flag), notice: 'FFE wurde aktualisiert.'
32
32
  else
33
33
  render :edit, status: :unprocessable_content
@@ -39,15 +39,23 @@ module Ffe
39
39
  redirect_to feature_flags_path, notice: 'FFE wurde gelöscht.'
40
40
  end
41
41
 
42
+ def dump
43
+ render json: Ffe::FeatureFlag.order(:name).map { |ff| ff.attributes.slice('name', 'description', 'enabled', 'milieu', 'expires_at', 'percentage') }
44
+ end
45
+
42
46
  private
43
47
 
44
48
  def set_ffe
45
- @feature_flag = ::Ffe::FeatureFlag.find(params.expect(:id))
49
+ @feature_flag = Ffe::FeatureFlag.find(params.expect(:id))
46
50
  end
47
51
 
48
52
  def feature_flag_params
49
- params.expect(feature_flag: [:name, :description, :enabled, :expires_at, { milieus: {} }]).tap do |params|
53
+ params.expect(
54
+ feature_flag: [:name, :description, :enabled, :expires_at, :clear_expires_at, { user_ids: [], milieus: {} }]
55
+ ).tap do |params|
50
56
  params[:milieu] = params[:milieus].values.join.ljust(Ffe.config.bitlength, '0') if params[:milieus].present?
57
+ params[:expires_at] = nil if params[:clear_expires_at] == '1'
58
+ params[:user_ids]&.delete_if(&:blank?)
51
59
  end
52
60
  end
53
61
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ffe
4
+ class ExpiredHandlingJob < ApplicationJob
5
+ queue_as :ffe
6
+
7
+ def perform(flag)
8
+ feature_flag = FeatureFlag.find_by(name: flag)
9
+ feature_flag.update!(enabled: false)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqAdapter
4
+ extend ActiveSupport::Concern
5
+
6
+ def adapter_job
7
+ Sidekiq::ScheduledSet.new.find do |job|
8
+ job.item.dig('args', 0, 'job_id') == job_id
9
+ end
10
+ end
11
+
12
+ def adapter_update_job
13
+ destroy_job
14
+ create_job
15
+ end
16
+
17
+ def adapter_destroy_job
18
+ job.presence&.delete
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidQueueAdapter
4
+ extend ActiveSupport::Concern
5
+
6
+ def adapter_job
7
+ SolidQueue::Job.scheduled.find_by(active_job_id: job_id)
8
+ end
9
+
10
+ def adapter_update_job
11
+ job.update(scheduled_at: expires_at)
12
+ se = SolidQueue::ScheduledExecution.find_by(job_id: job.id)
13
+ se.update(scheduled_at: expires_at)
14
+ end
15
+
16
+ def adapter_destroy_job
17
+ job.presence&.discard
18
+ end
19
+ end
@@ -2,10 +2,22 @@
2
2
 
3
3
  module Ffe
4
4
  class FeatureFlag < ApplicationRecord
5
+ if Ffe.config.queue_adapter == :solid_queue
6
+ include SolidQueueAdapter
7
+ elsif Ffe.config.queue_adapter == :sidekiq
8
+ include SidekiqAdapter
9
+ end
10
+
5
11
  self.table_name = 'feature_flags'
6
12
 
7
- validates :name, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z_]+\z/ }
8
- validates :expires_at, comparison: { greater_than: Time.current.end_of_day }, if: -> { expires_at.present? }
13
+ # validations
14
+ #
15
+ validates :name, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z_\d]+\z/ }
16
+ validates :expires_at, comparison: { greater_than: Time.current.end_of_day }, if: -> { expires_at.present? && expires_at_changed? } # rubocop:disable Layout/LineLength
17
+
18
+ # callbacks
19
+ after_save_commit :handle_change_job, unless: -> { Rails.env.test? }
20
+ before_destroy :destroy_job
9
21
 
10
22
  # the Flag functionality itself
11
23
  #
@@ -39,7 +51,7 @@ module Ffe
39
51
  milieu[pos] == '1'
40
52
  end
41
53
 
42
- def readable_milieus # rubocop:disable Metrics/AbcSize
54
+ def readable_milieus
43
55
  relevant = milieu.to_s.ljust(Ffe.config.milieus.length, '0').chars.first(Ffe.config.milieus.length)
44
56
  return 'all' if relevant.all? { |m| m == '1' }
45
57
  return 'no' if relevant.all? { |m| m == '0' }
@@ -47,5 +59,47 @@ module Ffe
47
59
  inverted = Ffe.config.milieus.invert
48
60
  relevant.each_with_index.filter_map { |m, i| inverted[i] if m == '1' }.join(', ')
49
61
  end
62
+
63
+ # ExpiresAt related methods
64
+ #
65
+ # handles changes and decide to update or to destroy the job
66
+ def handle_change_job
67
+ if expires_at.present? && job_id.blank?
68
+ create_job
69
+ elsif expires_at.present? && job_id.present?
70
+ update_job
71
+ elsif expires_at.blank? && job_id.present?
72
+ destroy_job
73
+ end
74
+ end
75
+
76
+ # 1. create job if expires_at set
77
+ def create_job
78
+ return if expires_at.blank?
79
+
80
+ job = Ffe::ExpiredHandlingJob.set(wait_until: expires_at).perform_later(name)
81
+ update_columns(job_id: job.job_id, touch: true) # rubocop:disable Rails/SkipsModelValidations
82
+ end
83
+
84
+ # 2. update job if expires_at changed
85
+ def update_job
86
+ return unless job_id.present? && expires_at.present?
87
+ return create_job unless job
88
+
89
+ adapter_update_job if job
90
+ end
91
+
92
+ # 3. delete job if expires_at changed to empty, or FF was destroyed
93
+ def destroy_job
94
+ return if job_id.blank?
95
+
96
+ adapter_destroy_job if job
97
+
98
+ update_columns(job_id: nil, touch: true) unless destroyed? # rubocop:disable Rails/SkipsModelValidations
99
+ end
100
+
101
+ def job
102
+ adapter_job.presence
103
+ end
50
104
  end
51
105
  end
@@ -27,7 +27,9 @@
27
27
 
28
28
  <div>
29
29
  <%= form.label :expires_at %>
30
- <%= form.datetime_local_field :expires_at, min: Time.current.tomorrow.beginning_of_day, value: feature_flag.expires_at&.strftime('%Y-%m-%dT%H:%M') %>
30
+ <%= form.datetime_local_field :expires_at %>
31
+ <%= check_box_tag 'feature_flag[clear_expires_at]', "1", false %>
32
+ <%= form.label :clear_expires_at %>
31
33
  </div>
32
34
 
33
35
  <div>
@@ -41,6 +43,11 @@
41
43
  <% end %>
42
44
  </div>
43
45
 
46
+ <div>
47
+ <%= form.label :user_ids %>
48
+ <%= form.collection_select :user_ids, Ffe::User.all, :id, :name, { include_blank: true, multiple: true } %>
49
+ </div>
50
+
44
51
  <div>
45
52
  <%= form.submit %>
46
53
  </div>
@@ -1,6 +1,7 @@
1
1
  <h1>Feature Flags</h1>
2
2
 
3
- <p><%= link_to "Neues Feature Flag anlegen", new_feature_flag_path %></p>
3
+ <div><%= link_to 'Neues Feature Flag anlegen', new_feature_flag_path %></div>
4
+ <div><%= link_to 'dump', dump_feature_flags_path, download: 'feature_flags.json' %></div>
4
5
 
5
6
  <table>
6
7
  <thead>
@@ -22,8 +23,8 @@
22
23
  <td><%= feature_flag.expires_at&.strftime('%Y-%m-%dT%H:%M') %></td>
23
24
  <td><%= feature_flag.readable_milieus %></td>
24
25
  <td>
25
- <%= link_to "Bearbeiten", edit_feature_flag_path(feature_flag) %>
26
- <%= button_to "Löschen", feature_flag_path(feature_flag), method: :delete %>
26
+ <%= link_to 'Bearbeiten', edit_feature_flag_path(feature_flag) %>
27
+ <%= button_to 'Löschen', feature_flag_path(feature_flag), method: :delete %>
27
28
  </td>
28
29
  </tr>
29
30
  <% end %>
@@ -17,5 +17,10 @@
17
17
  <%= @feature_flag.readable_milieus %>
18
18
  </div>
19
19
 
20
+ <div>
21
+ <strong>User IDs:</strong>
22
+ <%= @feature_flag.user_ids %>
23
+ </div>
24
+
20
25
  <p><%= link_to "Bearbeiten", edit_feature_flag_path(@feature_flag) %></p>
21
26
  <p><%= link_to "Zurück", feature_flags_path %></p>
data/config/routes.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  Ffe::Engine.routes.draw do
2
- resources :feature_flags
2
+ resources :feature_flags do
3
+ get :dump, on: :collection
4
+ end
3
5
  end
@@ -5,6 +5,7 @@ class CreateFeatureFlags < ActiveRecord::Migration[8.2]
5
5
  t.string :description
6
6
  t.boolean :enabled, default: false
7
7
  t.datetime :expires_at
8
+ t.string :job_id
8
9
  t.column :milieu, "bit(#{Ffe.config.bitlength})", default: ''.ljust(Ffe.config.bitlength, '0') # only for PG
9
10
  t.text :user_ids, array: true, default: []
10
11
 
@@ -2,12 +2,13 @@
2
2
 
3
3
  module Ffe
4
4
  class Configuration
5
- attr_accessor :bitlength, :env_variable, :milieus
5
+ attr_accessor :bitlength, :env_variable, :milieus, :queue_adapter
6
6
 
7
7
  def initialize
8
8
  @bitlength = 4
9
9
  @env_variable = 'RAILS_ENV'
10
10
  @milieus = { development: 0, staging: 1, production: 2 }
11
+ @queue_adapter = :async
11
12
  end
12
13
  end
13
14
  end
data/lib/ffe/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ffe
4
- VERSION = '0.1.0'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -11,6 +11,7 @@ module Ffe
11
11
 
12
12
  def copy_initializer
13
13
  template 'ffe_initializer.rb.tt', 'config/initializers/ffe.rb'
14
+ template 'feature_flags.json.tt', 'config/feature_flags.json'
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,9 @@
1
+ [
2
+ {
3
+ "name": "x_dummy_testing",
4
+ "description": "A general testing FF.",
5
+ "enabled": true,
6
+ "milieu": "1000",
7
+ "expires_at": null,
8
+ }
9
+ ]
@@ -23,4 +23,25 @@ Ffe.configure do |config|
23
23
 
24
24
  # not all rails apps are using RAILS_ENV variable to set it
25
25
  config.env_variable = 'RAILS_ENV'
26
+
27
+ # set the active job adapter,
28
+ # needed if you want to use the Ffe::ExpiredHandlingJob
29
+ # actual supported: :solid_queue, :sidekiq
30
+ config.queue_adapter = ActiveJob::Base.queue_adapter_name.to_sym
31
+ end
32
+
33
+
34
+ Rails.configuration.after_initialize do
35
+ if ActiveRecord::Base.connection.table_exists?(:feature_flags)
36
+ path = Rails.root.join('config/feature_flags.json')
37
+ next unless File.exist?(path)
38
+
39
+ data = JSON.load_file(path)
40
+ data.each do |flag|
41
+ name = flag.delete('name')
42
+ next if Ffe::FeatureFlag.exists?(name: name)
43
+
44
+ Ffe::FeatureFlag.create_with(flag).find_or_create_by(name: name)
45
+ end
46
+ end
26
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LeFnord
@@ -38,7 +38,10 @@ files:
38
38
  - app/controllers/ffe/feature_flags_controller.rb
39
39
  - app/helpers/ffe/application_helper.rb
40
40
  - app/jobs/ffe/application_job.rb
41
+ - app/jobs/ffe/expired_handling_job.rb
41
42
  - app/mailers/ffe/application_mailer.rb
43
+ - app/models/concerns/sidekiq_adapter.rb
44
+ - app/models/concerns/solid_queue_adapter.rb
42
45
  - app/models/ffe/application_record.rb
43
46
  - app/models/ffe/feature_flag.rb
44
47
  - app/models/ffe/user.rb
@@ -56,6 +59,7 @@ files:
56
59
  - lib/ffe/engine.rb
57
60
  - lib/ffe/version.rb
58
61
  - lib/generators/ffe/install/install_generator.rb
62
+ - lib/generators/ffe/templates/feature_flags.json.tt
59
63
  - lib/generators/ffe/templates/ffe_initializer.rb.tt
60
64
  - lib/tasks/ffe_tasks.rake
61
65
  homepage: https://github.com/LeFnord/ffe
@@ -80,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
84
  - !ruby/object:Gem::Version
81
85
  version: '0'
82
86
  requirements: []
83
- rubygems_version: 4.0.18
87
+ rubygems_version: 4.0.19
84
88
  specification_version: 4
85
89
  summary: A minimal Feature Flag Engine for Rails
86
90
  test_files: []