feature_toggles 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +80 -3
- data/lib/feature_toggles/mechatronic.rb +5 -1
- data/lib/feature_toggles/railtie.rb +32 -0
- data/lib/feature_toggles/version.rb +1 -1
- data/lib/feature_toggles.rb +4 -2
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ced315883f27c7dfa71e43d3b04a2e0c6111d7e3ab24ebb2d96b309880349abc
|
4
|
+
data.tar.gz: 2567a6b58344a5b1b05c2380374ef85e1325119a243af0310003b2ad170838bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3d9058efacee049c3feed1bc5878e3ba301d2e1d424954e8f871e802d2e31f110503a870b6f7dc90a92a84cca9012e02cacc5eb8c3ca52c2af912547fc05e20
|
7
|
+
data.tar.gz: 6f80548653e6f6f6792ef1a0b42c385d9def25c730a91d10d41e3b1d13d49442ecd8e799d7a0d66ac3e591366f207c7723b685b314a2ce670251ef5c0495f9c3
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
This gem provides a mechanism for pending features that take longer than a single release cycle. The basic idea is to have a configuration file that defines a bunch of toggles for various features you have pending. The running application then uses these toggles in order to decide whether or not to show the new feature.
|
7
7
|
|
8
|
-
<a href="https://evilmartians.com/?utm_source=
|
8
|
+
<a href="https://evilmartians.com/?utm_source=feature_toggles">
|
9
9
|
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
10
10
|
|
11
11
|
## Installation
|
@@ -13,7 +13,7 @@ This gem provides a mechanism for pending features that take longer than a singl
|
|
13
13
|
Add this line to your application's Gemfile:
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
gem
|
16
|
+
gem "feature_toggles"
|
17
17
|
```
|
18
18
|
|
19
19
|
And then execute:
|
@@ -24,7 +24,9 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
$ gem install feature_toggles
|
26
26
|
|
27
|
-
##
|
27
|
+
## Framework-agnostic usage
|
28
|
+
|
29
|
+
Features could be defined dynamically
|
28
30
|
|
29
31
|
```ruby
|
30
32
|
features = FeatureToggles.build do
|
@@ -46,6 +48,81 @@ features.enabled?(:bar, user: user)
|
|
46
48
|
features.for(user: user).enabled?(:foo)
|
47
49
|
```
|
48
50
|
|
51
|
+
or loaded from files
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
features = FeatureToggles.build(["/path/to/features.rb"])
|
55
|
+
```
|
56
|
+
|
57
|
+
### Rails usage
|
58
|
+
|
59
|
+
This is step-by-step guide to add `feature_toggles` to Rails application.
|
60
|
+
|
61
|
+
**Step 0. (optional) Add `features` to User model**
|
62
|
+
|
63
|
+
**NOTE**: This is not the part of this gem–you can model you per-user features settings differently.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class AddFeaturesToUsers < ActiveRecord::Migration
|
67
|
+
def change
|
68
|
+
# we use a `features` array column to store user's active features
|
69
|
+
add_column :users, :features, :string, array: true, default: []
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
**Step 1. Define features**
|
75
|
+
|
76
|
+
Features from file `<rails-root-or-engine>/config/initializers/features.rb` are loaded by convention.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# config/initializers/features.rb
|
80
|
+
env "FEATURE"
|
81
|
+
|
82
|
+
feature :chat do |user: nil|
|
83
|
+
user&.features.include?("chat")
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Features will be available at `Rails.features` after the end of application initialization.
|
88
|
+
|
89
|
+
**Step 2. Add `current_features` helper and use it.**
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class ApplicationController < ActionController::Base
|
93
|
+
# ...
|
94
|
+
helper_method :current_features
|
95
|
+
|
96
|
+
def current_features
|
97
|
+
Rails.features.for(user: current_user)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
**Step 3. Use `current_features`.**
|
103
|
+
|
104
|
+
For example, in your navigation template:
|
105
|
+
|
106
|
+
```erb
|
107
|
+
<ul>
|
108
|
+
<% if current_features.enabled?(:chat) %>
|
109
|
+
<li><a href="/chat">Chat</a></li>
|
110
|
+
<% end %>
|
111
|
+
</ul>
|
112
|
+
```
|
113
|
+
|
114
|
+
Or in your controller:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class ChatController < ApplicationController
|
118
|
+
def index
|
119
|
+
unless current_features.enabled?(:chat)
|
120
|
+
return render template: "comming_soon"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
49
126
|
## Development
|
50
127
|
|
51
128
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -7,9 +7,13 @@ module FeatureToggles
|
|
7
7
|
# Which env variables should be considered truthy
|
8
8
|
POSSIBLE_ENABLING_VALUES = %w(true on yes 1).freeze
|
9
9
|
|
10
|
-
def initialize(&block)
|
10
|
+
def initialize(definition_file_paths = nil, &block)
|
11
11
|
@features = {}
|
12
12
|
|
13
|
+
definition_file_paths&.each do |file|
|
14
|
+
instance_eval(File.read(file), file)
|
15
|
+
end
|
16
|
+
|
13
17
|
instance_eval(&block) if block_given?
|
14
18
|
end
|
15
19
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/railtie"
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
class << self
|
7
|
+
delegate :features, to: "application.config"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module FeatureToggles
|
12
|
+
class Railtie < ::Rails::Railtie
|
13
|
+
config.to_prepare do
|
14
|
+
paths = []
|
15
|
+
|
16
|
+
::Rails.application.railties.each do |railtie|
|
17
|
+
next unless railtie.respond_to?(:root)
|
18
|
+
|
19
|
+
file = railtie.root.join("config", "features.rb")
|
20
|
+
next unless file.exist?
|
21
|
+
|
22
|
+
paths << file.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
# Rails-root features must be last to override engines' configuration
|
26
|
+
file = ::Rails.root.join("config", "features.rb")
|
27
|
+
paths << file.to_s if file.exist?
|
28
|
+
|
29
|
+
Rails.application.config.features = FeatureToggles.build(paths)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/feature_toggles.rb
CHANGED
@@ -6,7 +6,9 @@ require "feature_toggles/mechatronic"
|
|
6
6
|
module FeatureToggles
|
7
7
|
module_function
|
8
8
|
|
9
|
-
def build(&block)
|
10
|
-
Mechatronic.new(&block)
|
9
|
+
def build(file_paths = nil, &block)
|
10
|
+
Mechatronic.new(file_paths, &block)
|
11
11
|
end
|
12
12
|
end
|
13
|
+
|
14
|
+
require "feature_toggles/railtie" if defined?(Rails)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: feature_toggles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Merkushin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: combustion
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: rake
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,6 +106,7 @@ files:
|
|
78
106
|
- lib/feature_toggles.rb
|
79
107
|
- lib/feature_toggles/mechatronic.rb
|
80
108
|
- lib/feature_toggles/proxy.rb
|
109
|
+
- lib/feature_toggles/railtie.rb
|
81
110
|
- lib/feature_toggles/version.rb
|
82
111
|
homepage: https://github.com/bibendi/feature_toggles
|
83
112
|
licenses:
|