togglefy 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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +273 -0
- data/app/models/togglefy/feature.rb +31 -0
- data/app/models/togglefy/feature_assignment.rb +8 -0
- data/lib/generators/togglefy/install_generator.rb +21 -0
- data/lib/generators/togglefy/templates/create_feature_assignments.rb +10 -0
- data/lib/generators/togglefy/templates/create_features.rb +16 -0
- data/lib/togglefy/engine.rb +5 -0
- data/lib/togglefy/feature_manager.rb +27 -0
- data/lib/togglefy/feature_query.rb +55 -0
- data/lib/togglefy/featureable.rb +36 -0
- data/lib/togglefy/version.rb +5 -0
- data/lib/togglefy.rb +67 -0
- data/togglefy.gemspec +40 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7a20296d9c30f3937ab02ace87872c15fe519a2ab6aaef86b1c5a3c4c331e5cf
|
4
|
+
data.tar.gz: da69eba486689763b59df5974c34cdddf8b42f3b93ea54ab3fa52507305ceb1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e655f2ba4eea61ae0ad8311a2d0dab1c10ec997c6e09724f43c17282970a559199c2053f1f7b9a61ebab3a97142338ec9c60641709768a8759e4bdafde6d979
|
7
|
+
data.tar.gz: 515d922552d07a7e4c115d2d225dff3e659eb34daeb6c35ad059a8c798c1cf98f17794641dbaa525bc938f47c24fe0bc437146a97e0e646893b2e0562cfbfe4c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
# Togglefy
|
2
|
+
|
3
|
+
Togglefy is a simple feature management solution to help you control which features an user or a group has access to.
|
4
|
+
|
5
|
+
Togglefy is free, open source and you are welcome to help build it.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
### UNDER CONSTRUCTION BECAUSE GEM STILL NEEDS TO BE UPLOADED TO RUBYGEMS
|
10
|
+
|
11
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
12
|
+
|
13
|
+
Add the gem manually to your Gemfile:
|
14
|
+
|
15
|
+
```gemfile
|
16
|
+
gem "togglefy"
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it and add to the application's Gemfile by executing:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
23
|
+
```
|
24
|
+
|
25
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### First steps
|
34
|
+
|
35
|
+
#### Installing inside the project
|
36
|
+
After adding the gem to your project, you need to run the generate command to add the necessary files:
|
37
|
+
```bash
|
38
|
+
rails generate togglefy:install
|
39
|
+
```
|
40
|
+
|
41
|
+
This command will create the migrations to create the tables inside your project. Please, don't remove/change anything that's there or Togglefy may not work as expected.
|
42
|
+
|
43
|
+
Run the migration to create these in your datase:
|
44
|
+
```bash
|
45
|
+
rails db:migrate
|
46
|
+
```
|
47
|
+
Or if you're using a legacy codebase:
|
48
|
+
```bash
|
49
|
+
rake db:migrate
|
50
|
+
```
|
51
|
+
|
52
|
+
The models are stored inside Togglefy, so you don't need to create them inside your project unless you want to.
|
53
|
+
|
54
|
+
If that's something you want, you can check them following this path: `app/models/togglefy/`.
|
55
|
+
|
56
|
+
After that, the next steps are also pretty simple.
|
57
|
+
|
58
|
+
#### Including inside the Assignable
|
59
|
+
|
60
|
+
Add the following to your model that will have a relation with the features. It can be an `User`, `Account` or something you decide:
|
61
|
+
```ruby
|
62
|
+
include Togglefy::Featureable
|
63
|
+
```
|
64
|
+
|
65
|
+
This will add the relationship between Togglefy's models and yours. Yours will be referred to as **assignable** throughout this documentation. If you want to check it in the source code, you can find it here: `lib/togglefy/featureable.rb` inside de `included` block.
|
66
|
+
|
67
|
+
With that, everything is ready to use **Togglefy**, welcome!
|
68
|
+
|
69
|
+
### Creating Features
|
70
|
+
To create features it's as simple as drinking a nice cold beer after a hard day or drinking the entire bottle of coffee in a span of 1 hour:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Togglefy::Feature.create(
|
74
|
+
name: "Magic",
|
75
|
+
description: "You're a Wizard, Harry"
|
76
|
+
) # To create a simple Feature
|
77
|
+
```
|
78
|
+
|
79
|
+
If you have tenant, groups (or roles), difference between environments, you can do the following:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Togglefy::Feature.create(
|
83
|
+
name: "Super Powers",
|
84
|
+
description: "With great power comes great responsibility",
|
85
|
+
tenant_id: "123abc",
|
86
|
+
group: :admin,
|
87
|
+
environment: :production
|
88
|
+
)
|
89
|
+
```
|
90
|
+
|
91
|
+
You don't have to fill all fields, the only one that is mandatory is the name, because is by using the name that we will create the unique identifier, which is the field we'll use to find, delete and more.
|
92
|
+
|
93
|
+
The identifier is the name, downcased and snake_cased 🐍
|
94
|
+
|
95
|
+
Whenever you create a `Togglefy::Feature`, you can expect something like this:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
id: 1,
|
99
|
+
name: "Super Powers",
|
100
|
+
identifier: "super_powers",
|
101
|
+
description: "With great power comes great responsibility",
|
102
|
+
created_at: "2025-04-12 01:39:10.176561000 +0000",
|
103
|
+
updated_at: "2025-04-12 01:39:46.818928000 +0000",
|
104
|
+
tenant: "123abc",
|
105
|
+
group: "admin",
|
106
|
+
environment: "production",
|
107
|
+
status: "inactive"
|
108
|
+
```
|
109
|
+
|
110
|
+
#### About `Togglefy::Feature` status
|
111
|
+
|
112
|
+
As you can see, the `Togglefy::Feature` also has a status and it is default to `inactive`. You can change this during creation.
|
113
|
+
|
114
|
+
The status holds the `inactive` or `active` values. This status is not to define if a assignable (any model that has the `include Togglefy::Featureable`) either has ou hasn't a feature, but to decide if this feature is available in the entire system.
|
115
|
+
|
116
|
+
It's up to you to define how you will implement it.
|
117
|
+
|
118
|
+
* Is it disabled? Then this feature is likely unrelease
|
119
|
+
* Or maybe if it is disabled, you can see the flag to active to an assignable but can't change the values?
|
120
|
+
|
121
|
+
Again, it's up to you!
|
122
|
+
|
123
|
+
You can change the status by:
|
124
|
+
* Sending a value during creation
|
125
|
+
* Updating the column
|
126
|
+
* Doing a:
|
127
|
+
```ruby
|
128
|
+
feature.active! # To activate
|
129
|
+
feature.inactive! # To inactivate
|
130
|
+
```
|
131
|
+
|
132
|
+
### Managing Assignables <-> Features
|
133
|
+
Now that we know how to create features, let's check how we can manage them.
|
134
|
+
|
135
|
+
An assignable has some direct methods thanks to the `include Togglefy::Featureable`, which are (and let's use an user as an example of an assignable):
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
user.has_feature?(:super_powers) # Checks if user has a single feature
|
139
|
+
user.add_feature(:super_powers) # Adds a feature to user
|
140
|
+
user.remove_feature(:super_powers) # Removes a feature from an user
|
141
|
+
user.clear_features # Clears all features from an user
|
142
|
+
```
|
143
|
+
|
144
|
+
The assignable <-> feature relation is held by the `Togglefy::FeatureAssignment` table/model.
|
145
|
+
|
146
|
+
But there's another way to manage assignables <-> features by using the `FeatureManager`. It's up to you to decide which one.
|
147
|
+
|
148
|
+
Here are the examples:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
Togglefy.for(assignable).has?(:super_powers) # Checks if assignable (user || account || anything) has a single feature
|
152
|
+
Togglefy.for(assignable).enable(:super_powers) # Enables/adds a feature to an assignable
|
153
|
+
Togglefy.for(assignable).disable(:super_powers) # Disables/removes a feature from an assignable
|
154
|
+
Togglefy.for(assignable).clear # Clears all features from an assignable
|
155
|
+
```
|
156
|
+
|
157
|
+
This second method may look strange, but it's the default used by the gem and you will see that right now!
|
158
|
+
|
159
|
+
### Querying Features
|
160
|
+
Remember when I told you a looooong time ago that the strange way is the default using by the gem? If you don't, no worries. It was a really loooong time ago, like `1.minute.ago`.
|
161
|
+
|
162
|
+
It's actually pretty simple. Each line of each code block will show you a way to query to achieve the same result in the context. You **don't** need to use all of options listed in each code block.
|
163
|
+
|
164
|
+
#### Querying Features (plural)
|
165
|
+
|
166
|
+
To query `Togglefy::Feature` from a specific group (the same applies to environment and tenant), you would do something like this:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
Togglefy::Feature.where(group: :dev)
|
170
|
+
Togglefy::Feature.for_group(:dev)
|
171
|
+
Togglefy.for_group(:dev)
|
172
|
+
Togglefy.for_role(:dev)
|
173
|
+
Togglefy.for_filters(filters: {group: :dev})
|
174
|
+
Togglefy.for_filters(filter: {role: :dev})
|
175
|
+
```
|
176
|
+
|
177
|
+
The `for_filters` is recommended when you want to use more than one filter, like:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
Togglefy.for_filters(filters: {group: :admin, environment: :production})
|
181
|
+
```
|
182
|
+
|
183
|
+
This will query me all `Togglefy::Feature`s that belongs to group admin and the production environment.
|
184
|
+
|
185
|
+
You can send `nil` values too, like:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
Togglefy.for_tenant(nil) # This will query me all Togglefy::Features with tenant_id nil
|
189
|
+
|
190
|
+
Togglefy.for_filters(filters: {group: :admin, environment: :nil, tenant_id: nil})
|
191
|
+
```
|
192
|
+
|
193
|
+
There's also another way to filter for `nil` values:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
Togglefy.without_group
|
197
|
+
Togglefy.without_role
|
198
|
+
|
199
|
+
Togglefy.without_environment
|
200
|
+
Togglefy.without_env
|
201
|
+
|
202
|
+
Togglefy.without_tenant
|
203
|
+
```
|
204
|
+
|
205
|
+
#### Querying by Status
|
206
|
+
To query Togglefy::Features by status (the same applies to inactive).
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
Togglefy::Feature.where(status: :active)
|
210
|
+
Togglefy::Feature.active
|
211
|
+
Togglefy.with_status(:active)
|
212
|
+
```
|
213
|
+
|
214
|
+
#### Finding a specific feature
|
215
|
+
```ruby
|
216
|
+
Togglefy.feature(:super_powers)
|
217
|
+
Togglefy::Feature.find_by(identifier: :super_powers)
|
218
|
+
```
|
219
|
+
|
220
|
+
#### Finding a specific feature just to destroy it because you're mean 😈
|
221
|
+
```ruby
|
222
|
+
Togglefy.destroy_feature(:super_powers)
|
223
|
+
Togglefy::Feature.find_by(identifier: :super_powers).destroy
|
224
|
+
```
|
225
|
+
|
226
|
+
#### Querying all features enabled to a klass
|
227
|
+
Let's assume that you have two different assignables: User and Account.
|
228
|
+
|
229
|
+
You want to list all features being used by assignables of User type:
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
Togglefy.for_type(User) # This is return all current FeatureAssignment with a User assignable
|
233
|
+
```
|
234
|
+
|
235
|
+
#### Aliases
|
236
|
+
|
237
|
+
By the way, did you notice that I wrote `group` and `role` to get group?
|
238
|
+
|
239
|
+
There are aliases for both group and environment that can be used outside of `Togglefy::Feature`. If you want to query `Togglefy::Feature` directly, use only the default name.
|
240
|
+
* `group` can be written as `role` outside of `Togglefy::Feature`
|
241
|
+
* `environment` can be written as `env` out side of `Togglefy::Feature`
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
Togglefy.for_group(:dev)
|
245
|
+
Togglefy.for_role(:dev)
|
246
|
+
Togglefy.for_filters(filters: {group: :dev})
|
247
|
+
Togglefy.for_filters(filter: {role: :dev})
|
248
|
+
|
249
|
+
Togglefy.for_environment(:production)
|
250
|
+
Togglefy.for_env(:production)
|
251
|
+
Togglefy.for_filters(filters: {environment: :production})
|
252
|
+
Togglefy.for_filters(filter: {env: :production})
|
253
|
+
```
|
254
|
+
|
255
|
+
## Development
|
256
|
+
|
257
|
+
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.
|
258
|
+
|
259
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
260
|
+
|
261
|
+
## Contributing
|
262
|
+
|
263
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/azeveco/togglefy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/azeveco/togglefy/blob/master/CODE_OF_CONDUCT.md).
|
264
|
+
|
265
|
+
There's a PR Template. Its usa is highly encouraged.
|
266
|
+
|
267
|
+
## License
|
268
|
+
|
269
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
270
|
+
|
271
|
+
## Code of Conduct
|
272
|
+
|
273
|
+
Everyone interacting in the togglefy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/azeveco/togglefy/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Togglefy
|
2
|
+
class Feature < ApplicationRecord
|
3
|
+
enum :status, [ :inactive, :active ]
|
4
|
+
|
5
|
+
has_many :feature_assignments, dependent: :destroy
|
6
|
+
has_many :assignables, through: :feature_assignments, source: :assignable
|
7
|
+
|
8
|
+
before_validation :build_identifier
|
9
|
+
|
10
|
+
scope :for_group, ->(group) { where(group:) }
|
11
|
+
scope :without_group, -> { where(group: nil) }
|
12
|
+
|
13
|
+
scope :for_environment, ->(environment) { where(environment:) }
|
14
|
+
scope :without_environment, -> { where(environment: nil) }
|
15
|
+
|
16
|
+
scope :for_tenant, ->(tenant_id) { where(tenant_id:) }
|
17
|
+
scope :without_tenant, -> { where(tenant_id: nil) }
|
18
|
+
|
19
|
+
scope :inactive, -> { where(status: :inactive) }
|
20
|
+
scope :active, -> { where(status: :active) }
|
21
|
+
scope :with_status, ->(status) { where(status:) }
|
22
|
+
|
23
|
+
validates :name, :identifier, presence: true, uniqueness: true
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def build_identifier
|
28
|
+
self.identifier = self.name.underscore.parameterize(separator: '_')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/migration"
|
3
|
+
|
4
|
+
module Togglefy
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
source_root File.expand_path("templates", __dir__)
|
10
|
+
|
11
|
+
def self.next_migration_number(path)
|
12
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
end
|
14
|
+
|
15
|
+
def copy_migrations
|
16
|
+
migration_template "create_features.rb", "db/migrate/create_togglefy_features.rb"
|
17
|
+
migration_template "create_feature_assignments.rb", "db/migrate/create_togglefy_feature_assignments.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class CreateTogglefyFeatureAssignments < ActiveRecord::Migration[8.0]
|
2
|
+
def change
|
3
|
+
create_table :togglefy_feature_assignments do |t|
|
4
|
+
t.references :feature, null: false, foreign_key: { to_table: :togglefy_features }
|
5
|
+
t.references :assignable, polymorphic: true, null: false
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
add_index :togglefy_feature_assignments, [:feature_id, :assignable_type, :assignable_id], unique: true, name: "index_togglefy_assignments_uniqueness"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateTogglefyFeatures < ActiveRecord::Migration[8.0]
|
2
|
+
def change
|
3
|
+
create_table :togglefy_features do |t|
|
4
|
+
t.string :name, null: false
|
5
|
+
t.string :identifier, null: false
|
6
|
+
t.string :description
|
7
|
+
t.string :tenant
|
8
|
+
t.string :group
|
9
|
+
t.string :environment
|
10
|
+
t.integer :status, default: 0, null: false
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
add_index :togglefy_features, :name, unique: true
|
14
|
+
add_index :togglefy_features, :identifier, unique: true
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Togglefy
|
2
|
+
class FeatureManager
|
3
|
+
def initialize(assignable)
|
4
|
+
@assignable = assignable
|
5
|
+
end
|
6
|
+
|
7
|
+
def enable(feature)
|
8
|
+
assignable.add_feature(feature)
|
9
|
+
end
|
10
|
+
|
11
|
+
def disable(feature)
|
12
|
+
assignable.remove_feature(feature)
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear
|
16
|
+
assignable.clear_features
|
17
|
+
end
|
18
|
+
|
19
|
+
def has?(feature)
|
20
|
+
assignable.has_feature?(feature)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :assignable
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Togglefy
|
2
|
+
class FeatureQuery
|
3
|
+
def feature(identifier)
|
4
|
+
Togglefy::Feature.find_by!(identifier:)
|
5
|
+
end
|
6
|
+
|
7
|
+
def destroy_feature(identifier)
|
8
|
+
feature = Togglefy::Feature.find_by!(identifier:)
|
9
|
+
feature&.destroy
|
10
|
+
end
|
11
|
+
|
12
|
+
def for_type(klass)
|
13
|
+
Togglefy::FeatureAssignment.for_type(klass)
|
14
|
+
end
|
15
|
+
|
16
|
+
def for_group(group)
|
17
|
+
Togglefy::Feature.for_group(group)
|
18
|
+
end
|
19
|
+
alias :for_role :for_group
|
20
|
+
|
21
|
+
def without_group
|
22
|
+
Togglefy::Feature.without_group
|
23
|
+
end
|
24
|
+
alias :without_role :without_group
|
25
|
+
|
26
|
+
def for_environment(environment)
|
27
|
+
Togglefy::Feature.for_environment(environment)
|
28
|
+
end
|
29
|
+
alias :for_env :for_environment
|
30
|
+
|
31
|
+
def without_environment
|
32
|
+
Togglefy::Feature.without_environment
|
33
|
+
end
|
34
|
+
alias :without_env :without_environment
|
35
|
+
|
36
|
+
def for_tenant(tenant_id)
|
37
|
+
Togglefy::Feature.for_tenant(tenant_id)
|
38
|
+
end
|
39
|
+
|
40
|
+
def without_tenant
|
41
|
+
Togglefy::Feature.without_tenant
|
42
|
+
end
|
43
|
+
|
44
|
+
def for_filters(filters)
|
45
|
+
Togglefy::Feature
|
46
|
+
.for_group(filters[:group] || filters[:role])
|
47
|
+
.for_environment(filters[:environment] || filters[:env])
|
48
|
+
.for_tenant(filters[:tenant_id])
|
49
|
+
end
|
50
|
+
|
51
|
+
def with_status(status)
|
52
|
+
Togglefy::Feature.with_status(status)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Togglefy
|
2
|
+
module Featureable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
has_many :feature_assignments, as: :assignable, class_name: "Togglefy::FeatureAssignment"
|
7
|
+
has_many :features, through: :feature_assignments, class_name: "Togglefy::Feature"
|
8
|
+
end
|
9
|
+
|
10
|
+
def has_feature?(identifier)
|
11
|
+
features.exists?(identifier: identifier.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_feature(feature)
|
15
|
+
feature = find_feature!(feature)
|
16
|
+
features << feature unless has_feature?(feature.identifier)
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove_feature(feature)
|
20
|
+
feature = find_feature!(feature)
|
21
|
+
features.destroy(feature) if has_feature?(feature.identifier)
|
22
|
+
end
|
23
|
+
|
24
|
+
def clear_features
|
25
|
+
features.destroy_all
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def find_feature!(feature)
|
31
|
+
return feature if feature.is_a?(Togglefy::Feature)
|
32
|
+
|
33
|
+
Togglefy::Feature.find_by!(identifier: feature.to_s)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/togglefy.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "togglefy/version"
|
4
|
+
require "togglefy/engine"
|
5
|
+
require "togglefy/featureable"
|
6
|
+
require "togglefy/feature_manager"
|
7
|
+
require "togglefy/feature_query"
|
8
|
+
|
9
|
+
module Togglefy
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
def self.feature(identifier)
|
13
|
+
FeatureQuery.new.feature(identifier)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.destroy_feature(identifier)
|
17
|
+
FeatureQuery.new.destroy_feature(identifier)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.for(assignable)
|
21
|
+
FeatureManager.new(assignable)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.for_type(klass)
|
25
|
+
FeatureQuery.new.for_type(klass)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.for_group(group)
|
29
|
+
FeatureQuery.new.for_group(group)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.without_group
|
33
|
+
FeatureQuery.new.without_group
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.for_environment(environment)
|
37
|
+
FeatureQuery.new.for_environment(environment)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.without_environment
|
41
|
+
FeatureQuery.new.without_environment
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.for_tenant(tenant_id)
|
45
|
+
FeatureQuery.new.for_tenant(tenant_id)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.without_tenant
|
49
|
+
FeatureQuery.new.without_tenant
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.for_filters(filters: {})
|
53
|
+
FeatureQuery.new.for_filters(filters)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.with_status(status)
|
57
|
+
FeatureQuery.new.with_status(status)
|
58
|
+
end
|
59
|
+
|
60
|
+
class <<self
|
61
|
+
alias_method :for_role, :for_group
|
62
|
+
alias_method :without_role, :without_group
|
63
|
+
|
64
|
+
alias_method :for_env, :for_environment
|
65
|
+
alias_method :without_env, :without_environment
|
66
|
+
end
|
67
|
+
end
|
data/togglefy.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/togglefy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "togglefy"
|
7
|
+
spec.version = Togglefy::VERSION
|
8
|
+
spec.authors = ["Gabriel Azevedo"]
|
9
|
+
spec.email = ["gazeveco@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Simple and open source Feature Management."
|
12
|
+
spec.description = "Togglefy is a feature management solution to help you control which features an user or a group has access to."
|
13
|
+
spec.homepage = "https://github.com/azeveco/Togglefy"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.4.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/azeveco/Togglefy"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/azeveco/Togglefy/releases"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
gemspec = File.basename(__FILE__)
|
26
|
+
spec.files = Dir.glob("lib/**/*") +
|
27
|
+
Dir.glob("app/**/*") +
|
28
|
+
Dir.glob("config/**/*") +
|
29
|
+
%w[LICENSE.txt README.md togglefy.gemspec]
|
30
|
+
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
# Uncomment to register a new dependency of your gem
|
36
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
37
|
+
|
38
|
+
# For more information and examples about making a new gem, check out our
|
39
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: togglefy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Azevedo
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-12 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: Togglefy is a feature management solution to help you control which features
|
13
|
+
an user or a group has access to.
|
14
|
+
email:
|
15
|
+
- gazeveco@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE.txt
|
21
|
+
- README.md
|
22
|
+
- app/models/togglefy/feature.rb
|
23
|
+
- app/models/togglefy/feature_assignment.rb
|
24
|
+
- lib/generators/togglefy/install_generator.rb
|
25
|
+
- lib/generators/togglefy/templates/create_feature_assignments.rb
|
26
|
+
- lib/generators/togglefy/templates/create_features.rb
|
27
|
+
- lib/togglefy.rb
|
28
|
+
- lib/togglefy/engine.rb
|
29
|
+
- lib/togglefy/feature_manager.rb
|
30
|
+
- lib/togglefy/feature_query.rb
|
31
|
+
- lib/togglefy/featureable.rb
|
32
|
+
- lib/togglefy/version.rb
|
33
|
+
- togglefy.gemspec
|
34
|
+
homepage: https://github.com/azeveco/Togglefy
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/azeveco/Togglefy
|
39
|
+
source_code_uri: https://github.com/azeveco/Togglefy
|
40
|
+
changelog_uri: https://github.com/azeveco/Togglefy/releases
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.4.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.6.2
|
56
|
+
specification_version: 4
|
57
|
+
summary: Simple and open source Feature Management.
|
58
|
+
test_files: []
|