bullet_train-roles 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c87ff4b45f3eed3dc5e3b2d7e6239160d4f83c3fee07483647b960f10e22ff1
4
+ data.tar.gz: 9302b17d61bbf57210ba56edacb75a5b4e4b00e4568ac67fe157fe9872cfcff5
5
+ SHA512:
6
+ metadata.gz: f766e94d3a385fa76603f50d462e1606d9fc7038b17855fa5300f227a81160c79fc489ad201e457c8e35ee1a364d27617e8d8b839fd94071bb581dd0b7d91dce
7
+ data.tar.gz: cd8e636d008364f3e6169dd67e0839fbb362167ce4cf19fce135d35020a81143573072848f5062683c94fb696a4c69020c83e72f4339733d5e4d212c6cc1e184
@@ -0,0 +1,110 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.1.2
4
+
5
+ aliases:
6
+ - &restore_bundler_cache
7
+ name: Restore Bundler cache
8
+ keys:
9
+ - gem-cache-v1-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
10
+ - gem-cache-v1-{{ .Branch }}-
11
+ - gem-cache-v1-
12
+ - &restore_yarn_cache
13
+ name: Restore Yarn cache
14
+ keys:
15
+ - yarn-packages-v1-{{ .Branch }}-{{ checksum "yarn.lock" }}
16
+ - yarn-packages-v1-{{ .Branch }}-
17
+ - yarn-packages-
18
+ - &save_bundle_cache
19
+ name: Save Bundle cache
20
+ key: gem-cache-v1-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
21
+ paths:
22
+ - vendor/bundle
23
+ - &save_yarn_cache
24
+ name: Save Yarn cache
25
+ key: yarn-packages-v1-{{ .Branch }}-{{ checksum "yarn.lock" }}
26
+ paths:
27
+ - node_modules
28
+
29
+ jobs:
30
+ 'Standard Ruby':
31
+ docker:
32
+ # normal ruby image is also sufficient here but we are using node-browsers so that we can make use of caching and speed up the process since other jobs are also using the same image
33
+ - image: circleci/ruby:3.0.2-node-browsers
34
+ steps:
35
+ - checkout
36
+
37
+ # Restore dependency caches
38
+ - restore_cache: *restore_bundler_cache
39
+ - restore_cache: *restore_yarn_cache
40
+
41
+ # Install dependencies
42
+ - ruby/bundle-install
43
+ - run: bundle clean --force
44
+ - run: yarn install
45
+
46
+ # Save dependency caches
47
+ # We only do this as part of this job, because it's time consuming and we don't want it to slow down test runners.
48
+ - save_cache: *save_bundle_cache
49
+ - save_cache: *save_yarn_cache
50
+
51
+ - run:
52
+ name: Check Standard Ruby
53
+ command: bundle exec standardrb
54
+
55
+ 'Minitest':
56
+ docker:
57
+ - image: circleci/ruby:3.0.2-node-browsers
58
+ environment:
59
+ PGHOST: localhost
60
+ PGUSER: bullet_train
61
+ RAILS_ENV: test
62
+ - image: circleci/postgres
63
+ environment:
64
+ POSTGRES_HOST_AUTH_METHOD: trust
65
+ POSTGRES_DB: bullet_train_test
66
+ POSTGRES_USER: bullet_train
67
+ executor: ruby/default
68
+ parallelism: 16
69
+ steps:
70
+ - checkout
71
+ - restore_cache: *restore_bundler_cache
72
+ - restore_cache: *restore_yarn_cache
73
+
74
+ # Install dependencies
75
+ - ruby/bundle-install
76
+ - run: bundle clean --force
77
+
78
+ # We run this because the DB might not be available for a while due to a race condition.
79
+ - run: dockerize -wait tcp://localhost:5432 -timeout 1m
80
+
81
+ - run:
82
+ name: Run tests with Knapsack Pro
83
+ command: |
84
+ export RAILS_ENV=test
85
+ bundle exec rails "knapsack_pro:queue:minitest[--verbose]"
86
+ environment:
87
+ KNAPSACK_PRO_CI_NODE_TOTAL: 16
88
+
89
+ # If you don't want to use Knapsack Pro, then use this configuration:
90
+ #
91
+ # - run:
92
+ # name: Run unit tests
93
+ # command: bundle exec rails test
94
+ # - run:
95
+ # name: Run system tests
96
+ # command: bundle exec rails test:system
97
+ #
98
+ # If you want to gather test results in CircleCI when not running tests in parallel,
99
+ # include `minitest-ci` in your Gemfile and uncomment the following step.
100
+ # You can access the test results via the "Tests" tab within each build in CircleCI.
101
+ #
102
+ # - store_test_results:
103
+ # path: test/reports
104
+
105
+ workflows:
106
+ version: 2
107
+ build:
108
+ jobs:
109
+ - 'Standard Ruby'
110
+ - 'Minitest'
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .idea/
11
+
12
+ test/dummy/tmp/*
13
+ test/dummy/log/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-12-15
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at probnpoudel@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bullet_train-roles.gemspec
6
+ gemspec
7
+
8
+ # TODO: move to gemspec when using this gem from rubygems, gemspec doesn't support git urls so it has to be here for now
9
+ gem "active_hash", github: "bullet-train-co/active_hash", branch: "fixes/address-keyword-argument-issue"
data/Gemfile.lock ADDED
@@ -0,0 +1,190 @@
1
+ GIT
2
+ remote: https://github.com/bullet-train-co/active_hash.git
3
+ revision: 25034270b31fec6ef48bba1e43be02760451d3c2
4
+ branch: fixes/address-keyword-argument-issue
5
+ specs:
6
+ active_hash (3.1.0)
7
+ activesupport (>= 5.0.0)
8
+
9
+ PATH
10
+ remote: .
11
+ specs:
12
+ bullet_train-roles (0.1.0)
13
+ active_hash
14
+ activesupport
15
+ cancancan
16
+
17
+ GEM
18
+ remote: https://rubygems.org/
19
+ specs:
20
+ actioncable (7.0.0)
21
+ actionpack (= 7.0.0)
22
+ activesupport (= 7.0.0)
23
+ nio4r (~> 2.0)
24
+ websocket-driver (>= 0.6.1)
25
+ actionmailbox (7.0.0)
26
+ actionpack (= 7.0.0)
27
+ activejob (= 7.0.0)
28
+ activerecord (= 7.0.0)
29
+ activestorage (= 7.0.0)
30
+ activesupport (= 7.0.0)
31
+ mail (>= 2.7.1)
32
+ actionmailer (7.0.0)
33
+ actionpack (= 7.0.0)
34
+ actionview (= 7.0.0)
35
+ activejob (= 7.0.0)
36
+ activesupport (= 7.0.0)
37
+ mail (~> 2.5, >= 2.5.4)
38
+ rails-dom-testing (~> 2.0)
39
+ actionpack (7.0.0)
40
+ actionview (= 7.0.0)
41
+ activesupport (= 7.0.0)
42
+ rack (~> 2.0, >= 2.2.0)
43
+ rack-test (>= 0.6.3)
44
+ rails-dom-testing (~> 2.0)
45
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
46
+ actiontext (7.0.0)
47
+ actionpack (= 7.0.0)
48
+ activerecord (= 7.0.0)
49
+ activestorage (= 7.0.0)
50
+ activesupport (= 7.0.0)
51
+ globalid (>= 0.6.0)
52
+ nokogiri (>= 1.8.5)
53
+ actionview (7.0.0)
54
+ activesupport (= 7.0.0)
55
+ builder (~> 3.1)
56
+ erubi (~> 1.4)
57
+ rails-dom-testing (~> 2.0)
58
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
59
+ activejob (7.0.0)
60
+ activesupport (= 7.0.0)
61
+ globalid (>= 0.3.6)
62
+ activemodel (7.0.0)
63
+ activesupport (= 7.0.0)
64
+ activerecord (7.0.0)
65
+ activemodel (= 7.0.0)
66
+ activesupport (= 7.0.0)
67
+ activestorage (7.0.0)
68
+ actionpack (= 7.0.0)
69
+ activejob (= 7.0.0)
70
+ activerecord (= 7.0.0)
71
+ activesupport (= 7.0.0)
72
+ marcel (~> 1.0)
73
+ mini_mime (>= 1.1.0)
74
+ activesupport (7.0.0)
75
+ concurrent-ruby (~> 1.0, >= 1.0.2)
76
+ i18n (>= 1.6, < 2)
77
+ minitest (>= 5.1)
78
+ tzinfo (~> 2.0)
79
+ ast (2.4.2)
80
+ builder (3.2.4)
81
+ byebug (11.1.3)
82
+ cancancan (3.3.0)
83
+ concurrent-ruby (1.1.9)
84
+ crass (1.0.6)
85
+ erubi (1.10.0)
86
+ factory_bot (6.2.0)
87
+ activesupport (>= 5.0.0)
88
+ factory_bot_rails (6.2.0)
89
+ factory_bot (~> 6.2.0)
90
+ railties (>= 5.0.0)
91
+ globalid (1.0.0)
92
+ activesupport (>= 5.0)
93
+ i18n (1.8.11)
94
+ concurrent-ruby (~> 1.0)
95
+ knapsack_pro (3.1.3)
96
+ rake
97
+ loofah (2.13.0)
98
+ crass (~> 1.0.2)
99
+ nokogiri (>= 1.5.9)
100
+ mail (2.7.1)
101
+ mini_mime (>= 0.1.1)
102
+ marcel (1.0.2)
103
+ method_source (1.0.0)
104
+ mini_mime (1.1.2)
105
+ minitest (5.15.0)
106
+ nio4r (2.5.8)
107
+ nokogiri (1.12.5-arm64-darwin)
108
+ racc (~> 1.4)
109
+ parallel (1.21.0)
110
+ parser (3.0.3.2)
111
+ ast (~> 2.4.1)
112
+ pg (1.2.3)
113
+ racc (1.6.0)
114
+ rack (2.2.3)
115
+ rack-test (1.1.0)
116
+ rack (>= 1.0, < 3)
117
+ rails (7.0.0)
118
+ actioncable (= 7.0.0)
119
+ actionmailbox (= 7.0.0)
120
+ actionmailer (= 7.0.0)
121
+ actionpack (= 7.0.0)
122
+ actiontext (= 7.0.0)
123
+ actionview (= 7.0.0)
124
+ activejob (= 7.0.0)
125
+ activemodel (= 7.0.0)
126
+ activerecord (= 7.0.0)
127
+ activestorage (= 7.0.0)
128
+ activesupport (= 7.0.0)
129
+ bundler (>= 1.15.0)
130
+ railties (= 7.0.0)
131
+ rails-dom-testing (2.0.3)
132
+ activesupport (>= 4.2.0)
133
+ nokogiri (>= 1.6)
134
+ rails-html-sanitizer (1.4.2)
135
+ loofah (~> 2.3)
136
+ railties (7.0.0)
137
+ actionpack (= 7.0.0)
138
+ activesupport (= 7.0.0)
139
+ method_source
140
+ rake (>= 12.2)
141
+ thor (~> 1.0)
142
+ zeitwerk (~> 2.5)
143
+ rainbow (3.0.0)
144
+ rake (13.0.6)
145
+ regexp_parser (2.2.0)
146
+ rexml (3.2.5)
147
+ rubocop (1.23.0)
148
+ parallel (~> 1.10)
149
+ parser (>= 3.0.0.0)
150
+ rainbow (>= 2.2.2, < 4.0)
151
+ regexp_parser (>= 1.8, < 3.0)
152
+ rexml
153
+ rubocop-ast (>= 1.12.0, < 2.0)
154
+ ruby-progressbar (~> 1.7)
155
+ unicode-display_width (>= 1.4.0, < 3.0)
156
+ rubocop-ast (1.15.0)
157
+ parser (>= 3.0.1.1)
158
+ rubocop-performance (1.12.0)
159
+ rubocop (>= 1.7.0, < 2.0)
160
+ rubocop-ast (>= 0.4.0)
161
+ ruby-progressbar (1.11.0)
162
+ standard (1.5.0)
163
+ rubocop (= 1.23.0)
164
+ rubocop-performance (= 1.12.0)
165
+ thor (1.1.0)
166
+ tzinfo (2.0.4)
167
+ concurrent-ruby (~> 1.0)
168
+ unicode-display_width (2.1.0)
169
+ websocket-driver (0.7.5)
170
+ websocket-extensions (>= 0.1.0)
171
+ websocket-extensions (0.1.5)
172
+ zeitwerk (2.5.1)
173
+
174
+ PLATFORMS
175
+ arm64-darwin-20
176
+
177
+ DEPENDENCIES
178
+ active_hash!
179
+ bullet_train-roles!
180
+ byebug (~> 11.1.0)
181
+ factory_bot_rails (~> 6.2.0)
182
+ knapsack_pro (~> 3.1.0)
183
+ minitest (~> 5.0)
184
+ pg (~> 1.2.0)
185
+ rails (~> 7.0.0)
186
+ rake (~> 13.0)
187
+ standard (~> 1.5.0)
188
+
189
+ BUNDLED WITH
190
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Bullet Train, Inc.
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,164 @@
1
+ # Bullet Train Roles
2
+
3
+ This Ruby Gem provides a Yaml-based configuration layer on top of CanCanCan's ability file. You can use this configuration file to simplify the definition of the most common types of permissions, while still implementing more complicated permissions in CanCanCan's traditional `app/model/ability.rb`.
4
+
5
+ ## Example Domain Model
6
+
7
+ For the sake of this document, we're going to assume the following example modeling around users and teams:
8
+
9
+ - A `User` belongs to a `Team` via a `Membership`.
10
+ - A `User` only has one `Membership` per team.
11
+ - A `Membership` can have zero, one, or many `Role`s assigned.
12
+ - A `Membership` without a `Role` is just a default team member.
13
+
14
+ You don't have to name your models the same thing in order to use this Ruby Gem, but it does depend on having a similar structure.
15
+
16
+ > If you're interested in reading more about how and why Bullet Train implements this structure, you can [read about it on our blog](https://blog.bullettrain.co/teams-should-be-an-mvp-feature/).
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem "bullet_train-roles"
24
+ ```
25
+
26
+ And then execute the following in your shell:
27
+
28
+ ```
29
+ bundle install
30
+ ```
31
+
32
+ Finally, run the installation generator:
33
+
34
+ ```
35
+ rails generate bullet_train:roles:install User Membership Team
36
+ ```
37
+
38
+ This will:
39
+
40
+ - stub out a configuration file in `config/models/roles.yml`.
41
+ - create a database migration to add `role_ids:jsonb` to `Membership`.
42
+ - add `include Role::Support` to `app/models/membership.rb`.
43
+ - add a basic `permit` call in `app/models/ability.rb`.
44
+
45
+ ### Limitations
46
+
47
+ The generators currently assume you're using PostgreSQL and `jsonb` will be available when generating a `role_ids` column. If you're using MySQL, you can edit these migrations and use `json` instead, although you won't be able to set a default value and you'll need to take care of this in the model.
48
+
49
+ ## Usage
50
+
51
+ The provided `Role` model is backed (via [ActiveHash](https://github.com/active-hash/active_hash)) by a Yaml configuration in `config/models/roles.yml`.
52
+
53
+ To help explain this configuration and it's options, we'll provide the following hypothetical example:
54
+
55
+ ```
56
+ default:
57
+ models:
58
+ Project: read
59
+ Billing::Subscription: read
60
+
61
+ editor:
62
+ manageable_roles:
63
+ - editor
64
+ models:
65
+ Project: manage
66
+
67
+ billing:
68
+ manageable_roles:
69
+ - billing
70
+ models:
71
+ Billing::Subscription: manage
72
+
73
+ admin:
74
+ includes:
75
+ - editor
76
+ - billing
77
+ manageable_roles:
78
+ - admin
79
+ ```
80
+
81
+ Here's a breakdown of the structure of the configuration file:
82
+
83
+ - `default` represents all permissions that are granted to any active member on a team.
84
+ - `editor`, `billing`, and `admin` represent additional roles that can be assigned to a membership.
85
+ - `models` provides a list of resources that members with a specific role will be granted.
86
+ - `manageable_roles` provides a list of roles that can be assigned to other users by members that have the role being defined.
87
+ - `includes` provides a list of other roles whose permissions should also be made available to members with the role being defined.
88
+ - `manage`, `read`, etc. are all CanCanCan-defined actions that can be granted.
89
+
90
+ The following things are true given the example configuration above:
91
+
92
+ - By default, users on a team are read-only participants.
93
+ - Users with the `editor` role:
94
+ - can give other users the `editor` role.
95
+ - can modify project details.
96
+ - Users with the `billing` role:
97
+ - can give other users the `billing` role.
98
+ - can create and update billing subscriptions.
99
+ - Users with the `admin` role:
100
+ - inherit all the privileges of the `editor` and `billing` roles.
101
+ - can give other users the `editor`, `billing`, or `admin` role. (The ability to grant `editor` and `billing` privileges is inherited from the other roles listed in `includes`.)
102
+
103
+ ### Assigning Multiple Actions per Resource
104
+
105
+ You can also grant more granular permissions by supplying a list of the specific actions per resource, like so:
106
+
107
+ ```
108
+ editor:
109
+ models:
110
+ project:
111
+ - read
112
+ - update
113
+ ```
114
+
115
+ ## Applying Configuration
116
+
117
+ All of these definitions are interpreted and translated into CanCanCan directives when we invoke the following Bullet Train helper in `app/models/ability.rb`:
118
+
119
+ ```
120
+ permit user, through: :memberships, parent: :team
121
+ ```
122
+
123
+ In the example above:
124
+
125
+ - `through` should reference a collection on `User` where access to a resource is granted. The most common example is the `memberships` association, which grants a `User` access to a `Team`. **In the context of `permit` discussions, we refer to the `Membership` model in this example as "the grant model".**
126
+ - `parent` should indicate which level the models in `through` will grant a user access at. In the case of a `Membership`, this is `team`.
127
+
128
+ ## Additional Grant Models
129
+
130
+ To illustrate the flexibility of this approach, consider that you may want to grant non-administrative team members different permissions for different `Project` objects on a `Team`. In that case, `permit` actually allows us to re-use the same role definitions to assign permissions that are scoped by a specific resource, like this:
131
+
132
+ ```
133
+ permit user, through: :projects_collaborators, parent: :project
134
+ ```
135
+
136
+ In this example, `permit` is smart enough to only apply the permissions granted by a `Projects::Collaborator` record at the level of the `Project` it belongs to. You can turn any model into a grant model by adding `include Roles::Support` and adding a `role_ids:jsonb` attribute. You can look at `Scaffolding::AbsolutelyAbstract::CreativeConcepts::Collaborator` for an example.
137
+
138
+ ## Debugging
139
+ If you want to see what CanCanCan directives are being created by your permit calls, you can add the `debug: true` option to your `permit` statement in `app/models/ability.rb`.
140
+
141
+ Likewise, to see what abilities are being added for a certain user, you can run the following on the Rails console:
142
+
143
+ ```
144
+ user = User.first
145
+ Ability.new(user).permit user, through: :projects_collaborators, parent: :project, debug: true
146
+ ```
147
+
148
+ ## Development
149
+
150
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
151
+
152
+ 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).
153
+
154
+ ## Contributing
155
+
156
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bullet-train-co/bullet_train-roles. 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/bullet-train-co/bullet_train-roles/blob/master/CODE_OF_CONDUCT.md).
157
+
158
+ ## License
159
+
160
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
161
+
162
+ ## Code of Conduct
163
+
164
+ Everyone interacting in the BulletTrain::Roles project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bullet-train-co/bullet_train-roles/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ t.warning = false
11
+ end
12
+
13
+ task default: %i[test]
14
+
15
+ KnapsackPro.load_tasks if defined?(KnapsackPro)
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bullet_train/roles"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here