roleable 0.0.2 → 0.1.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.
- data/README.md +13 -1
- data/lib/roleable/resource.rb +2 -0
- data/lib/roleable/subject.rb +2 -0
- data/lib/roleable/user_role.rb +5 -4
- data/lib/roleable/version.rb +1 -1
- data/roleable.gemspec +0 -1
- data/spec/roleable/resource_spec.rb +35 -18
- data/spec/roleable/subject_spec.rb +27 -13
- metadata +3 -3
data/README.md
CHANGED
@@ -86,7 +86,13 @@ Find the resources of a given class for which a user has a given role:
|
|
86
86
|
|
87
87
|
```ruby
|
88
88
|
user.resources_with_role(:editor, Page)
|
89
|
-
```
|
89
|
+
```
|
90
|
+
|
91
|
+
Find the resource of a given class for which a user has _any_ of a list of roles:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
user.resources_with_role([:editor, :author], Page)
|
95
|
+
```
|
90
96
|
|
91
97
|
Find a user's roles for a given resource:
|
92
98
|
|
@@ -108,6 +114,12 @@ Find users with a given role:
|
|
108
114
|
page.users_with_role(:editor)
|
109
115
|
```
|
110
116
|
|
117
|
+
Find users matching _any_ of a list of roles:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
page.users_with_role([:editor, :author])
|
121
|
+
```
|
122
|
+
|
111
123
|
For more details check out the [API documentation](http://rubydoc.info/github/mcrowe/roleable/master/frames) on rubydoc.info.
|
112
124
|
|
113
125
|
## Requirements
|
data/lib/roleable/resource.rb
CHANGED
@@ -5,10 +5,12 @@ module Roleable::Resource
|
|
5
5
|
end
|
6
6
|
|
7
7
|
# Return a list of users that have the given role for this resource.
|
8
|
+
# If a list of role names is given, return users with any of those roles for this resource.
|
8
9
|
#
|
9
10
|
# ==== Examples
|
10
11
|
#
|
11
12
|
# page.users_with_role(:editor) # => [user1, user2, ...]
|
13
|
+
# page.users_with_role([:editor, :author]) # => [user1, user2, ...]
|
12
14
|
#
|
13
15
|
def users_with_role(role_name)
|
14
16
|
User.joins(:user_roles).merge(::UserRole.with_role_name(role_name).with_resource(self))
|
data/lib/roleable/subject.rb
CHANGED
@@ -54,10 +54,12 @@ module Roleable::Subject
|
|
54
54
|
end
|
55
55
|
|
56
56
|
# Return a list of resources of the given class, for which the user has the given role.
|
57
|
+
# If passed an array or roles, returns resources for which the user has any of the roles.
|
57
58
|
#
|
58
59
|
# ==== Examples
|
59
60
|
#
|
60
61
|
# user.resources_with_role(:editor, Page) # => [page1, page2, ...]
|
62
|
+
# user.resources_with_role([:editor, :author], Page) # => [page1, page2, ...]
|
61
63
|
#
|
62
64
|
def resources_with_role(role_name, resource_class)
|
63
65
|
user_roles = ::UserRole.with_user(self).with_role_name(role_name).with_resource_class(resource_class)
|
data/lib/roleable/user_role.rb
CHANGED
@@ -17,12 +17,13 @@ module Roleable::UserRole
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def with_role_name(role_name)
|
20
|
-
|
21
|
-
|
20
|
+
roles = ::Role.find_all_by_name(role_name)
|
21
|
+
with_roles(roles)
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
|
24
|
+
def with_roles(roles)
|
25
|
+
role_ids = roles.map { |r| r.id }
|
26
|
+
where(:role_id => role_ids)
|
26
27
|
end
|
27
28
|
|
28
29
|
def with_resource_class(resource_class)
|
data/lib/roleable/version.rb
CHANGED
data/roleable.gemspec
CHANGED
@@ -9,38 +9,55 @@ describe Roleable::Resource do
|
|
9
9
|
before do
|
10
10
|
@page = Page.create
|
11
11
|
@editor_role = Role.create(:name => 'editor')
|
12
|
+
@author_role = Role.create(:name => 'author')
|
12
13
|
end
|
13
14
|
|
14
|
-
context 'with a role
|
15
|
-
|
16
|
-
|
15
|
+
context 'with a single role' do
|
16
|
+
|
17
|
+
context 'with a role that doesnt exist' do
|
18
|
+
it 'returns an empty list' do
|
19
|
+
@page.users_with_role(:notarole).should be_empty
|
20
|
+
end
|
17
21
|
end
|
18
|
-
end
|
19
22
|
|
20
|
-
|
23
|
+
context 'when multiple users have the given role' do
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
before do
|
26
|
+
3.times { User.create.add_role(:editor, @page) }
|
27
|
+
end
|
25
28
|
|
26
|
-
|
27
|
-
|
29
|
+
it 'returns a list of the users' do
|
30
|
+
users = @page.users_with_role(:editor)
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
users.length.should == 3
|
33
|
+
users.first.should be_a(User)
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
it 'doesnt return users that dont have the role' do
|
37
|
+
other_page = Page.create
|
38
|
+
other_user = User.create.add_role(:editor, other_page)
|
36
39
|
|
37
|
-
|
40
|
+
users = @page.users_with_role(:editor)
|
38
41
|
|
39
|
-
|
42
|
+
users.length.should == 3
|
43
|
+
end
|
44
|
+
|
40
45
|
end
|
41
46
|
|
42
47
|
end
|
43
48
|
|
49
|
+
context 'with a list of roles' do
|
50
|
+
it 'returns a list of users that have any of the given roles for this resource' do
|
51
|
+
3.times { User.create.add_role(:editor, @page) }
|
52
|
+
2.times { User.create.add_role(:author, @page) }
|
53
|
+
User.create
|
54
|
+
|
55
|
+
users = @page.users_with_role([:editor, :author])
|
56
|
+
|
57
|
+
users.length.should == 5
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
44
61
|
end
|
45
62
|
|
46
63
|
end
|
@@ -8,6 +8,7 @@ describe Roleable::Subject do
|
|
8
8
|
@user = User.create
|
9
9
|
@admin_role = Role.create(:name => 'admin')
|
10
10
|
@editor_role = Role.create(:name => 'editor')
|
11
|
+
@author_role = Role.create(:name => 'author')
|
11
12
|
end
|
12
13
|
|
13
14
|
describe '#add_role' do
|
@@ -160,23 +161,36 @@ describe Roleable::Subject do
|
|
160
161
|
|
161
162
|
describe '#resources_with_role' do
|
162
163
|
|
163
|
-
context '
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
@user.add_role(:editor,
|
168
|
-
end
|
164
|
+
context 'with a single role' do
|
165
|
+
|
166
|
+
context 'when the user has the given role for several resources of the given class' do
|
167
|
+
it 'returns a list containing those resources' do
|
168
|
+
3.times { @user.add_role(:editor, Page.create) }
|
169
169
|
|
170
|
-
|
170
|
+
pages = @user.resources_with_role(:editor, Page)
|
171
171
|
|
172
|
-
|
173
|
-
|
172
|
+
pages.length.should == 3
|
173
|
+
pages.first.should be_a(Page)
|
174
|
+
end
|
174
175
|
end
|
175
|
-
end
|
176
176
|
|
177
|
-
|
178
|
-
|
179
|
-
|
177
|
+
context 'when the user doesnt have the given role for any resources of the given class' do
|
178
|
+
it 'returns an empty list' do
|
179
|
+
@user.resources_with_role(:editor, Page).should be_empty
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'with a list of roles' do
|
186
|
+
it 'returns a list of resources the user has those roles for' do
|
187
|
+
3.times { @user.add_role(:editor, Page.create) }
|
188
|
+
2.times { @user.add_role(:author, Page.create) }
|
189
|
+
Page.create
|
190
|
+
|
191
|
+
pages = @user.resources_with_role([:editor, :author], Page)
|
192
|
+
|
193
|
+
pages.length.should == 5
|
180
194
|
end
|
181
195
|
end
|
182
196
|
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mitch Crowe
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-05-09 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|