bullet_train-roles 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -7
- data/bin/setup +1 -1
- data/lib/bullet_train/roles/version.rb +1 -1
- data/lib/generators/bullet_train/roles/install/install_generator.rb +0 -15
- data/lib/models/role.rb +5 -7
- data/lib/roles/support.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f03a796a2c17954ad4c25e313caea68ba0b60b7e8901e7c1acf83d869e100592
|
4
|
+
data.tar.gz: 131e6518cefb00c6ea280d4ac4bf492961cb6f45b6d0ef3d695b66de9366d906
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7df53d71fb2d393e84a30a5efb1fa9f351781653b40e1cc31e5cf565d4432228b3a78b6924d021908c5d463695dfdeb0dff834a0f1c63a26438774e8159f4438
|
7
|
+
data.tar.gz: 56a0c8b76f5105959d828db3f619c863cc917fd3caf1e8dc838151268da68da1bce455ebd3378f68dfa7bf78d804d673df311f6351cfd0a47a2b4f43a6c68c07
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Bullet Train Roles
|
2
2
|
|
3
|
-
Bullet Train Roles provides a Yaml-based configuration layer on top of [CanCanCan](https://github.com/CanCanCommunity/cancancan). You can use this configuration file to simplify the definition of many common permissions, while still implementing more complicated permissions in CanCanCan's traditional `app/model/ability.rb`.
|
3
|
+
Bullet Train Roles provides a Yaml-based configuration layer on top of [CanCanCan](https://github.com/CanCanCommunity/cancancan). You can use this configuration file to simplify the definition of many common permissions, while still implementing more complicated permissions in CanCanCan's traditional `app/model/ability.rb`.
|
4
4
|
|
5
5
|
Additionally, Bullet Train Roles makes it trivial to assign the same roles and associated permissions at different levels in your application. For example, you can assign someone administrative privileges at a team level, or only at a project level.
|
6
6
|
|
7
7
|
Bullet Train Roles was created by [Andrew Culver](http://twitter.com/andrewculver) and [Adam Pallozzi](https://twitter.com/adampallozzi).
|
8
8
|
|
9
|
-
## Example Domain Model
|
9
|
+
## Example Domain Model
|
10
10
|
|
11
11
|
For the sake of this document, we're going to assume the following example modeling around users and teams:
|
12
12
|
|
@@ -15,7 +15,7 @@ For the sake of this document, we're going to assume the following example model
|
|
15
15
|
- A `Membership` can have zero, one, or many `Role`s assigned.
|
16
16
|
- A `Membership` without a `Role` is just a default team member.
|
17
17
|
|
18
|
-
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.
|
18
|
+
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.
|
19
19
|
|
20
20
|
> 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/).
|
21
21
|
|
@@ -52,10 +52,6 @@ The installer will:
|
|
52
52
|
- add a basic `permit` call in `app/models/ability.rb`.
|
53
53
|
|
54
54
|
|
55
|
-
### Limitations
|
56
|
-
|
57
|
-
The generators currently assume you're using PostgreSQL and `jsonb` will be available when generating a `role_ids` column. If you're using MySQL, the generator will 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.
|
58
|
-
|
59
55
|
## Usage
|
60
56
|
|
61
57
|
The provided `Role` model is backed by a Yaml configuration in `config/models/roles.yml`.
|
data/bin/setup
CHANGED
@@ -58,11 +58,6 @@ module BulletTrain
|
|
58
58
|
adapter_name
|
59
59
|
end
|
60
60
|
|
61
|
-
def db_adapter_supports_defaults?
|
62
|
-
supported_db_adapters = %w[postgresql]
|
63
|
-
supported_db_adapters.include?(db_adapter)
|
64
|
-
end
|
65
|
-
|
66
61
|
def find_json_data_type_identifier
|
67
62
|
adapter_name = db_adapter
|
68
63
|
|
@@ -95,14 +90,6 @@ module BulletTrain
|
|
95
90
|
File.write(file_location, update_file_content.join)
|
96
91
|
end
|
97
92
|
|
98
|
-
def add_default_value_to_migration(file_name, table_name)
|
99
|
-
file_location = Dir["db/migrate/*_#{file_name}.rb"].last
|
100
|
-
line_to_match = "add_column :#{table_name.downcase}, :role_ids"
|
101
|
-
content_to_add = ", default: []\n"
|
102
|
-
|
103
|
-
add_in_file(file_location, line_to_match, content_to_add)
|
104
|
-
end
|
105
|
-
|
106
93
|
def migration_file_exists?(file_name)
|
107
94
|
file_location = Dir["db/migrate/*_#{file_name}.rb"].last
|
108
95
|
|
@@ -124,8 +111,6 @@ module BulletTrain
|
|
124
111
|
|
125
112
|
generate "migration", "#{migration_file_name} role_ids:#{json_data_type_identifier}"
|
126
113
|
|
127
|
-
add_default_value_to_migration(migration_file_name, top_level_model_table_name) if db_adapter_supports_defaults?
|
128
|
-
|
129
114
|
puts("Success 🎉🎉\n\n")
|
130
115
|
end
|
131
116
|
|
data/lib/models/role.rb
CHANGED
@@ -109,23 +109,21 @@ class Role < ActiveYaml::Base
|
|
109
109
|
class Collection < Array
|
110
110
|
def initialize(model, ary)
|
111
111
|
@model = model
|
112
|
-
|
113
112
|
super(ary)
|
114
113
|
end
|
115
114
|
|
116
115
|
def <<(role)
|
117
116
|
return true if include?(role)
|
118
|
-
|
119
|
-
role_ids = @model.role_ids
|
120
|
-
|
117
|
+
role_ids = @model.role_ids || []
|
121
118
|
role_ids << role.id
|
122
|
-
|
123
119
|
@model.update(role_ids: role_ids)
|
124
120
|
end
|
125
121
|
|
126
122
|
def delete(role)
|
127
|
-
@model.
|
128
|
-
|
123
|
+
return @model.save unless include?(role)
|
124
|
+
current_role_ids = @model.role_ids || []
|
125
|
+
new_role_ids = current_role_ids - [role.key]
|
126
|
+
@model.role_ids = new_role_ids
|
129
127
|
@model.save
|
130
128
|
end
|
131
129
|
end
|
data/lib/roles/support.rb
CHANGED
@@ -44,7 +44,7 @@ module Roles
|
|
44
44
|
after_destroy :invalidate_cache
|
45
45
|
|
46
46
|
def validate_roles
|
47
|
-
self.role_ids = role_ids
|
47
|
+
self.role_ids = role_ids&.select(&:present?) || []
|
48
48
|
|
49
49
|
return if @allowed_roles.nil?
|
50
50
|
|
@@ -66,7 +66,7 @@ module Roles
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def roles_without_defaults
|
69
|
-
role_ids
|
69
|
+
role_ids&.map { |role_id| Role.find(role_id) } || []
|
70
70
|
end
|
71
71
|
|
72
72
|
def manageable_roles
|