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 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
@@ -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))
@@ -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)
@@ -17,12 +17,13 @@ module Roleable::UserRole
17
17
  end
18
18
 
19
19
  def with_role_name(role_name)
20
- role = ::Role.find_by_name(role_name)
21
- with_role(role)
20
+ roles = ::Role.find_all_by_name(role_name)
21
+ with_roles(roles)
22
22
  end
23
23
 
24
- def with_role(role)
25
- where(:role_id => role && role.id)
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)
@@ -1,3 +1,3 @@
1
1
  module Roleable
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -20,5 +20,4 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency 'sqlite3', '~> 1.3'
21
21
  gem.add_development_dependency 'activerecord', '~> 3.0'
22
22
  gem.add_development_dependency 'with_model', '~> 0.2'
23
-
24
23
  end
@@ -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 that doesnt exist' do
15
- it 'returns an empty list' do
16
- @page.users_with_role(:notarole).should be_empty
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
- context 'when multiple users have the given role' do
23
+ context 'when multiple users have the given role' do
21
24
 
22
- before do
23
- 3.times { User.create!.add_role(:editor, @page) }
24
- end
25
+ before do
26
+ 3.times { User.create.add_role(:editor, @page) }
27
+ end
25
28
 
26
- it 'returns a list of the users' do
27
- users = @page.users_with_role(:editor)
29
+ it 'returns a list of the users' do
30
+ users = @page.users_with_role(:editor)
28
31
 
29
- users.length.should == 3
30
- users.first.should be_a(User)
31
- end
32
+ users.length.should == 3
33
+ users.first.should be_a(User)
34
+ end
32
35
 
33
- it 'doesnt return users that dont have the role' do
34
- other_page = Page.create
35
- other_user = User.create!.add_role(:editor, other_page)
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
- users = @page.users_with_role(:editor)
40
+ users = @page.users_with_role(:editor)
38
41
 
39
- users.length.should == 3
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 'when the user has the given role for several resources of the given class' do
164
- it 'returns a list containing those resources' do
165
- 3.times do
166
- page = Page.create
167
- @user.add_role(:editor, page)
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
- pages = @user.resources_with_role(:editor, Page)
170
+ pages = @user.resources_with_role(:editor, Page)
171
171
 
172
- pages.length.should == 3
173
- pages.first.should be_a(Page)
172
+ pages.length.should == 3
173
+ pages.first.should be_a(Page)
174
+ end
174
175
  end
175
- end
176
176
 
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
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
- - 2
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-04-20 00:00:00 -07:00
18
+ date: 2012-05-09 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency