cancancan-system 1.1.2 → 1.1.3
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 +4 -4
- data/LICENSE +21 -21
- data/README.md +330 -345
- data/app/models/concerns/can_can_can/system/ability.rb +155 -156
- data/lib/cancancan-system.rb +5 -5
- data/lib/cancancan-system/engine.rb +10 -8
- data/lib/cancancan-system/version.rb +7 -7
- data/lib/generators/cancancan_system_generator.rb +24 -22
- data/lib/generators/templates/README.md +1 -1
- data/lib/generators/templates/migration.rb.erb +7 -5
- metadata +38 -25
- data/CHANGELOG.md +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a240cafcc8a7e32b63b9b9a3bf3c9cf6c7bffbf3584e14f561570353aa6ccd42
|
4
|
+
data.tar.gz: 672681f772283b523217f833ddf5c743954f9f4e388d791ae07f1424f7402838
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f15ec3f979e1593ce1f74240717e3a6ff2b6b90db5765fe465e37689c7622c44b1520c1d2701fd2c47c8833f1890341c6c0208b26771d0a48cf5624e099e057b
|
7
|
+
data.tar.gz: d830fcb1925a155829c6ea08b4a32f5e64572f4cfb85772c55ec16d1fa256a4be0d36203181fcabe8ead3298536cd12777a5bf64a047ca04aea7bdc860468100
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,345 +1,330 @@
|
|
1
|
-
# CanCanCan System
|
2
|
-
|
3
|
-
[](https://badge.fury.io/rb/cancancan-system)
|
4
|
-
|
5
|
-
Conventions & helpers simplifying the use of CanCanCan in complex Rails applications. CanCanCan System simplifies authorizing collaborations, memberships and more across a complex structure of models.
|
6
|
-
|
7
|
-
To describe complex abilities CanCanCan System relies on two different constructs: ActiveRecord **objects**, and **relationships** of users to those objects.
|
8
|
-
|
9
|
-
CanCanCan System uses two attributes on *objects* to describe abilities:
|
10
|
-
|
11
|
-
* **ability:** Describes the default ability of users without a special relationship with an object.
|
12
|
-
* **visiblity:** Specifies whether an object is visible to other users than the creator.
|
13
|
-
|
14
|
-
CanCanCan System uses one attribute on *relationships* to describe abilities:
|
15
|
-
|
16
|
-
* **ability:** Describes the ability of a user with the related object.
|
17
|
-
|
18
|
-
`ability` can have any CanCanCan permission, `'admin'` (`:manage`), `'user'` (`:modify`) or `'guest'` (`:read`) as value while `visiblity` is limited to `public` and `private`.
|
19
|
-
|
20
|
-
---
|
21
|
-
|
22
|
-
## Table of Contents
|
23
|
-
|
24
|
-
* [Installation](#installation)
|
25
|
-
* [Usage](#usage)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
* [
|
32
|
-
* [
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
Organization.first
|
289
|
-
# =>
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
in the Software without restriction, including without limitation the rights
|
332
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
333
|
-
copies of the Software, and to permit persons to whom the Software is
|
334
|
-
furnished to do so, subject to the following conditions:
|
335
|
-
|
336
|
-
The above copyright notice and this permission notice shall be included in all
|
337
|
-
copies or substantial portions of the Software.
|
338
|
-
|
339
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
340
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
341
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
342
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
343
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
344
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
345
|
-
SOFTWARE.
|
1
|
+
# CanCanCan System
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/cancancan-system) 
|
4
|
+
|
5
|
+
Conventions & helpers simplifying the use of CanCanCan in complex Rails applications. CanCanCan System simplifies authorizing collaborations, memberships and more across a complex structure of models.
|
6
|
+
|
7
|
+
To describe complex abilities CanCanCan System relies on two different constructs: ActiveRecord **objects**, and **relationships** of users to those objects.
|
8
|
+
|
9
|
+
CanCanCan System uses two attributes on *objects* to describe abilities:
|
10
|
+
|
11
|
+
* **ability:** Describes the default ability of users without a special relationship with an object.
|
12
|
+
* **visiblity:** Specifies whether an object is visible to other users than the creator.
|
13
|
+
|
14
|
+
CanCanCan System uses one attribute on *relationships* to describe abilities:
|
15
|
+
|
16
|
+
* **ability:** Describes the ability of a user with the related object.
|
17
|
+
|
18
|
+
`ability` can have any CanCanCan permission, `'admin'` (`:manage`), `'user'` (`:modify`) or `'guest'` (`:read`) as value while `visiblity` is limited to `public` and `private`.
|
19
|
+
|
20
|
+
---
|
21
|
+
|
22
|
+
## Table of Contents
|
23
|
+
|
24
|
+
* [Installation](#installation)
|
25
|
+
* [Usage](#usage)
|
26
|
+
* [Defining abilities](#defining-abilities)
|
27
|
+
* [Public abilities](#public-abilities)
|
28
|
+
* [acts_as_belongable abilities](#acts_as_belongable-abilities)
|
29
|
+
* [Membership abilities](#membership-abilities)
|
30
|
+
* [Get abilities](#get-abilities)
|
31
|
+
* [Testing](#testing)
|
32
|
+
* [To Do](#to-do)
|
33
|
+
* [Contributing](#contributing)
|
34
|
+
* [Semantic versioning](#semantic-versioning)
|
35
|
+
|
36
|
+
---
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
CanCanCan System works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'cancancan-system'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install cancancan-system
|
53
|
+
|
54
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
gem 'cancancan-system', github: 'jonhue/cancancan-system'
|
58
|
+
```
|
59
|
+
|
60
|
+
Now run the generator:
|
61
|
+
|
62
|
+
$ rails g cancancan_system
|
63
|
+
|
64
|
+
To wrap things up, migrate the changes to your database:
|
65
|
+
|
66
|
+
$ rails db:migrate
|
67
|
+
|
68
|
+
---
|
69
|
+
|
70
|
+
## Usage
|
71
|
+
|
72
|
+
To get started add CanCanCan System to your `Ability` class (`app/models/ability.rb`) and add the required `:modify` alias:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Ability
|
76
|
+
include CanCan::Ability
|
77
|
+
include CanCanCan::System::Ability
|
78
|
+
|
79
|
+
def initialize(user)
|
80
|
+
modify([:create, :read, :update, :destroy])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
**Note:** The aliases (`:create, :read, :update, :destroy`) can be custom.
|
86
|
+
|
87
|
+
You should add the `ability` attribute to ActiveRecord models you want to [define abilities](#defining-abilities) for:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
add_column :users, :ability, :string, default: 'guest'
|
91
|
+
```
|
92
|
+
|
93
|
+
And you should add a `visiblity` attribute to ActiveRecord models you want to define [public abilities](#public-abilities) for:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
add_column :users, :visiblity, :string, default: 'public'
|
97
|
+
```
|
98
|
+
|
99
|
+
### Defining Abilities
|
100
|
+
|
101
|
+
CanCanCan System makes an `abilities` method available which simplifies setting up common abilities:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
def initialize(user)
|
105
|
+
abilities(Post, user)
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
This is equivalent to:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
def initialize(user)
|
113
|
+
public_abilities(Post)
|
114
|
+
can(:manage, Post, user_id: user.id) if user
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
You can also use the `abilities` method with custom column names and polymorphic associations. This comes in handy when using the [NotificationsRails gem](https://github.com/jonhue/notifications-rails):
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
def initialize(user)
|
122
|
+
abilities(Notification, user, column: 'target', polymorphic: true, public_abilities: false)
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
**Note:** Set `column` to `nil` or `''` to use the `id` attribute.
|
127
|
+
|
128
|
+
This is equivalent to:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
def initialize(user)
|
132
|
+
can(:manage, Notification, target_id: user.id, target_type: user.class.name) if user
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
Learn more about the `public_abilities` method [here](#public-abilities).
|
137
|
+
|
138
|
+
#### Public abilities
|
139
|
+
|
140
|
+
The `public_abilities` method defines the object-abilities without a `user` being present:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
def initialize(user)
|
144
|
+
public_abilities(Post)
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
This is equivalent to:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
def initialize(user)
|
152
|
+
can(:manage, Post, ability: 'admin', visibility: 'public')
|
153
|
+
can(:modify, Post, ability: 'user', visibility: 'public')
|
154
|
+
can(:read, Post, ability: 'guest', visibility: 'public')
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
#### acts_as_belongable abilities
|
159
|
+
|
160
|
+
CanCanCan System integrates with the [acts_as_belongable gem](https://github.com/jonhue/acts_as_belongable) to make defining abilities for relationships dead simple.
|
161
|
+
|
162
|
+
Let's say our users can be a member of multiple organizations:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
class User < ApplicationRecord
|
166
|
+
acts_as_belongable
|
167
|
+
belongable :member_of_organizations, 'Organization', scope: :membership
|
168
|
+
has_many :organizations
|
169
|
+
end
|
170
|
+
|
171
|
+
class Organization < ApplicationRecord
|
172
|
+
acts_as_belonger
|
173
|
+
belonger :members, 'User', scope: :membership
|
174
|
+
belongs_to :user
|
175
|
+
end
|
176
|
+
```
|
177
|
+
|
178
|
+
We would then just do:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
def initialize(user)
|
182
|
+
abilities(Organization, user) do
|
183
|
+
belonger_abilities(Organization, user, scope: :membership)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
**Note:** This can be done in the same way with `belongable_abilities` for `belongable` relationships.
|
189
|
+
|
190
|
+
Now we are able to add members to our organizations and set their abilities:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
Organization.first.add_belongable(User.first, scope: :membership, ability: 'admin')
|
194
|
+
```
|
195
|
+
|
196
|
+
**Note:** The `scope` option is optional. If omitted, the defined abilities will apply to all belongings regardless of their scope.
|
197
|
+
|
198
|
+
#### Membership abilities
|
199
|
+
|
200
|
+
Now, let us assume that we have another model: `Post`.
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
class User < ApplicationRecord
|
204
|
+
acts_as_belongable
|
205
|
+
belongable :member_of_organizations, 'Organization', scope: :membership
|
206
|
+
has_many :posts
|
207
|
+
has_many :organizations
|
208
|
+
end
|
209
|
+
|
210
|
+
class Organization < ApplicationRecord
|
211
|
+
acts_as_belonger
|
212
|
+
belonger :members, 'User', scope: :membership
|
213
|
+
has_many :posts
|
214
|
+
belongs_to :user
|
215
|
+
end
|
216
|
+
|
217
|
+
class Post < ApplicationRecord
|
218
|
+
belongs_to :user
|
219
|
+
belongs_to :organization
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
You want the posts of an organization to be accessible for its members. It doesn't get any simpler than this:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
def initialize(user)
|
227
|
+
abilities(Post, user) do
|
228
|
+
membership_abilities('Organization', Post, user, scope: :membership)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
```
|
232
|
+
|
233
|
+
**Note:** The `scope` option is optional. If omitted, the defined abilities will apply to all belongings regardless of their scope.
|
234
|
+
|
235
|
+
You are also able to perform some customization:
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
class Post < ApplicationRecord
|
239
|
+
belongs_to :user
|
240
|
+
belongs_to :object, polymorphic: true
|
241
|
+
end
|
242
|
+
```
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
def initialize(user)
|
246
|
+
abilities(Post, user) do
|
247
|
+
membership_abilities('Organization', Post, user, scope: :membership, column: 'object', polymorphic: true)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
```
|
251
|
+
|
252
|
+
Another option is to use the [acts_as_belongable gem](https://github.com/jonhue/acts_as_belongable) to associate posts with organizations:
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
class Organization < ApplicationRecord
|
256
|
+
acts_as_belonger
|
257
|
+
belonger :members, 'User', scope: :membership
|
258
|
+
belonger :posts, 'Post'
|
259
|
+
has_many :posts
|
260
|
+
belongs_to :user
|
261
|
+
end
|
262
|
+
|
263
|
+
class Post < ApplicationRecord
|
264
|
+
acts_as_belongable
|
265
|
+
belongable :organizations, 'Organization'
|
266
|
+
belongs_to :user
|
267
|
+
end
|
268
|
+
```
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
def initialize(user)
|
272
|
+
abilities(Post, user) do
|
273
|
+
organization_abilities(Post, user, scope: :membership, acts_as_belongable: true)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
```
|
277
|
+
|
278
|
+
**Note:** If your `acts_as_belongable` association in the `Post` model is not following the CanCanCan System naming convention, you can override it by passing the `column` option.
|
279
|
+
|
280
|
+
### Get abilities
|
281
|
+
|
282
|
+
You can use the `ability` method to get the ability of an ActiveRecord object:
|
283
|
+
|
284
|
+
```ruby
|
285
|
+
Organization.first.ability
|
286
|
+
# => 'guest'
|
287
|
+
|
288
|
+
ability(Organization.first)
|
289
|
+
# => :read
|
290
|
+
```
|
291
|
+
|
292
|
+
It returns a symbol or `nil`.
|
293
|
+
|
294
|
+
---
|
295
|
+
|
296
|
+
## Testing
|
297
|
+
|
298
|
+
1. Fork this repository
|
299
|
+
2. Clone your forked git locally
|
300
|
+
3. Install dependencies
|
301
|
+
|
302
|
+
`$ bundle install`
|
303
|
+
|
304
|
+
4. Run specs
|
305
|
+
|
306
|
+
`$ bundle exec rspec`
|
307
|
+
|
308
|
+
5. Run RuboCop
|
309
|
+
|
310
|
+
`$ bundle exec rubocop`
|
311
|
+
|
312
|
+
---
|
313
|
+
|
314
|
+
## To Do
|
315
|
+
|
316
|
+
We use [GitHub projects](https://github.com/jonhue/cancancan-system/projects/1) to coordinate the work on this project.
|
317
|
+
|
318
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/cancancan-system/issues/new).
|
319
|
+
|
320
|
+
---
|
321
|
+
|
322
|
+
## Contributing
|
323
|
+
|
324
|
+
We hope that you will consider contributing to CanCanCan System. Please read this short overview for some information about how to get started:
|
325
|
+
|
326
|
+
[Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
|
327
|
+
|
328
|
+
### Semantic Versioning
|
329
|
+
|
330
|
+
CanCanCan System follows Semantic Versioning 2.0 as defined at http://semver.org.
|