roleable 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -1
- data/README.md +17 -17
- data/lib/generators/roleable/install/install_generator.rb +3 -3
- data/lib/generators/roleable/install/templates/applied_role.rb +3 -3
- data/lib/generators/roleable/install/templates/migration.rb +5 -5
- data/lib/generators/roleable/install/templates/role.rb +3 -3
- data/lib/roleable.rb +4 -4
- data/lib/roleable/applied_role.rb +8 -8
- data/lib/roleable/configuration.rb +3 -3
- data/lib/roleable/resource.rb +5 -5
- data/lib/roleable/role.rb +3 -3
- data/lib/roleable/subject.rb +13 -13
- data/lib/roleable/version.rb +1 -1
- data/roleable.gemspec +1 -1
- data/spec/roleable/resource_spec.rb +33 -23
- data/spec/roleable/subject_spec.rb +62 -52
- data/spec/spec_helper.rb +2 -2
- data/spec/support/shared_contexts.rb +3 -3
- metadata +96 -62
data/CHANGELOG
CHANGED
@@ -4,4 +4,16 @@
|
|
4
4
|
|
5
5
|
## v0.0.2
|
6
6
|
|
7
|
-
* Fix a bug that allowed users not associated with a resource to be listed in Resource#users_with_role.
|
7
|
+
* Fix a bug that allowed users not associated with a resource to be listed in Resource#users_with_role.
|
8
|
+
|
9
|
+
## v0.1.0
|
10
|
+
|
11
|
+
* Allow resource_with_role and users_with_role to accept an array of role names.
|
12
|
+
|
13
|
+
## v0.2.0
|
14
|
+
|
15
|
+
* Allow the subject class name to be customized so that it does not always have to be "User".
|
16
|
+
|
17
|
+
## v0.2.1
|
18
|
+
|
19
|
+
* Ensure applied_roles are destroyed when an associated subject or resource is destroyed.
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/mcrowe/roleable.png?branch=master)](http://travis-ci.org/mcrowe/roleable)
|
4
4
|
|
5
|
-
A flexible roles solution for active-record-backed Rails 3 applications. Allows for multiple roles scoped to instances of any model, as well as global roles (admin, for example).
|
5
|
+
A flexible roles solution for active-record-backed Rails 3 applications. Allows for multiple roles scoped to instances of any model, as well as global roles (admin, for example).
|
6
6
|
|
7
7
|
Roleable is designed to be ultra simple and obvious, letting you build upon it to satisfy your needs. It is also designed to be efficient: using database indices, and well-crafted queries so that it can handle a huge number of roles.
|
8
8
|
|
@@ -21,15 +21,15 @@ And then execute:
|
|
21
21
|
Run the generator to create the `Role` and `AppliedRole` models, migrations, and a configuration initializer:
|
22
22
|
|
23
23
|
$ rails g roleable:install
|
24
|
-
|
24
|
+
|
25
25
|
And then run the migrations:
|
26
26
|
|
27
27
|
$ rake db:migrate
|
28
|
-
|
28
|
+
|
29
29
|
(This will create the `roles` and `applied_roles` tables, together with the appropriate database indices.)
|
30
30
|
|
31
31
|
## Setup
|
32
|
-
|
32
|
+
|
33
33
|
Include `Roleable::Subject` into your subject model, e.g.:
|
34
34
|
|
35
35
|
```ruby
|
@@ -37,7 +37,7 @@ class User < ActiveRecord::Base
|
|
37
37
|
include Roleable::Subject
|
38
38
|
...
|
39
39
|
end
|
40
|
-
```
|
40
|
+
```
|
41
41
|
|
42
42
|
Include `Roleable::Resource` into any models you want to relate a subject role to (resource), e.g.:
|
43
43
|
|
@@ -67,11 +67,11 @@ Remove a role:
|
|
67
67
|
```ruby
|
68
68
|
# global
|
69
69
|
user.remove_role(:admin)
|
70
|
-
|
70
|
+
|
71
71
|
# resource-scoped
|
72
72
|
user.remove_role(:editor, page)
|
73
73
|
```
|
74
|
-
|
74
|
+
|
75
75
|
Query a role:
|
76
76
|
|
77
77
|
```ruby
|
@@ -81,7 +81,7 @@ user.has_role?(:admin)
|
|
81
81
|
# resource-scoped
|
82
82
|
user.has_role?(:editor, page)
|
83
83
|
```
|
84
|
-
|
84
|
+
|
85
85
|
Find the resources of a given class for which a user has a given role:
|
86
86
|
|
87
87
|
```ruby
|
@@ -105,7 +105,7 @@ Or, all the global roles for a user:
|
|
105
105
|
```ruby
|
106
106
|
user.roles_for_resource(nil)
|
107
107
|
```
|
108
|
-
|
108
|
+
|
109
109
|
### Resource
|
110
110
|
|
111
111
|
Find subjects (users) with a given role:
|
@@ -114,6 +114,12 @@ Find subjects (users) with a given role:
|
|
114
114
|
page.subjects_with_role(:editor)
|
115
115
|
```
|
116
116
|
|
117
|
+
Find subjects matching _any_ of a list of roles:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
page.subjects_with_role([:editor, :author])
|
121
|
+
```
|
122
|
+
|
117
123
|
## Customization
|
118
124
|
|
119
125
|
By default, roleable assumes that your subject model is called `User`. You can customize this by modifying the generated configuration intializer located at `config/initializers/roleable.rb`, e.g.:
|
@@ -124,14 +130,8 @@ Roleable.configure do |config|
|
|
124
130
|
end
|
125
131
|
```
|
126
132
|
|
127
|
-
Find users matching _any_ of a list of roles:
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
page.users_with_role([:editor, :author])
|
131
|
-
```
|
132
|
-
|
133
133
|
For more details check out the [API documentation](http://rubydoc.info/github/mcrowe/roleable/master/frames) on rubydoc.info.
|
134
|
-
|
134
|
+
|
135
135
|
## Requirements
|
136
136
|
|
137
137
|
Rails 3, ActiveRecord, Ruby >= 1.8.7
|
@@ -142,4 +142,4 @@ Rails 3, ActiveRecord, Ruby >= 1.8.7
|
|
142
142
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
143
143
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
144
144
|
4. Push to the branch (`git push origin my-new-feature`)
|
145
|
-
5. Create new Pull Request
|
145
|
+
5. Create new Pull Request
|
@@ -13,11 +13,11 @@ module Roleable
|
|
13
13
|
copy_file 'initializer.rb', 'config/initializers/roleable.rb'
|
14
14
|
migration_template 'migration.rb', 'db/migrate/roleable_create_roles_and_applied_roles.rb'
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def self.next_migration_number(path)
|
18
18
|
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
end
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class RoleableCreateRolesAndAppliedRoles < ActiveRecord::Migration
|
2
|
-
|
2
|
+
|
3
3
|
def change
|
4
4
|
create_table :roles do |t|
|
5
|
-
t.string :name
|
5
|
+
t.string :name
|
6
6
|
t.timestamps
|
7
7
|
end
|
8
8
|
|
@@ -12,9 +12,9 @@ class RoleableCreateRolesAndAppliedRoles < ActiveRecord::Migration
|
|
12
12
|
t.references :resource, :polymorphic => true
|
13
13
|
t.timestamps
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
add_index :applied_roles, :subject_id
|
17
17
|
add_index :applied_roles, [:resource_type, :resource_id]
|
18
18
|
end
|
19
|
-
|
20
|
-
end
|
19
|
+
|
20
|
+
end
|
data/lib/roleable.rb
CHANGED
@@ -6,13 +6,13 @@ require 'roleable/applied_role'
|
|
6
6
|
require 'roleable/configuration'
|
7
7
|
|
8
8
|
module Roleable
|
9
|
-
|
9
|
+
|
10
10
|
def self.configuration
|
11
11
|
@configuration ||= Roleable::Configuration.new
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def self.configure
|
15
15
|
yield configuration if block_given?
|
16
16
|
end
|
17
|
-
|
18
|
-
end
|
17
|
+
|
18
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Roleable::AppliedRole
|
2
|
-
|
2
|
+
|
3
3
|
def self.extended(base)
|
4
4
|
base.belongs_to :subject, :class_name => Roleable.configuration.subject_class_name
|
5
5
|
base.belongs_to :role
|
6
6
|
base.belongs_to :resource, :polymorphic => true
|
7
|
-
|
7
|
+
|
8
8
|
base.attr_accessible :role, :subject_id, :resource
|
9
9
|
end
|
10
10
|
|
@@ -29,18 +29,18 @@ module Roleable::AppliedRole
|
|
29
29
|
def with_resource_class(resource_class)
|
30
30
|
where(:resource_type => resource_type_from_class(resource_class))
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
# Create a record with the given attributes if there are no records
|
34
34
|
# that already have those attributes.
|
35
35
|
#
|
36
36
|
# Returns the record if it was saved, otherwise nil.
|
37
|
-
def create_if_unique!(attributes)
|
37
|
+
def create_if_unique!(attributes)
|
38
38
|
applied_role = new(attributes)
|
39
39
|
|
40
|
-
record_attributes = applied_role.attributes.reject do |k, v|
|
40
|
+
record_attributes = applied_role.attributes.reject do |k, v|
|
41
41
|
%w(id updated_at created_at).include?(k)
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
if !exists?(record_attributes) && applied_role.save
|
45
45
|
applied_role
|
46
46
|
else
|
@@ -57,5 +57,5 @@ module Roleable::AppliedRole
|
|
57
57
|
def resource_type_from_class(resource_class)
|
58
58
|
resource_class.name
|
59
59
|
end
|
60
|
-
|
61
|
-
end
|
60
|
+
|
61
|
+
end
|
data/lib/roleable/resource.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Roleable::Resource
|
2
2
|
|
3
3
|
def self.included(base)
|
4
|
-
base.has_many :applied_roles, :as => :resource
|
4
|
+
base.has_many :applied_roles, :as => :resource, :dependent => :destroy
|
5
5
|
end
|
6
6
|
|
7
7
|
# Return a list of users that have the given role for this resource.
|
@@ -16,11 +16,11 @@ module Roleable::Resource
|
|
16
16
|
subject_class.joins(:applied_roles).
|
17
17
|
merge( ::AppliedRole.with_role_name(role_name).with_resource(self) )
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
private
|
21
|
-
|
21
|
+
|
22
22
|
def subject_class
|
23
23
|
Roleable.configuration.subject_class_name.classify.constantize
|
24
24
|
end
|
25
|
-
|
26
|
-
end
|
25
|
+
|
26
|
+
end
|
data/lib/roleable/role.rb
CHANGED
data/lib/roleable/subject.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Roleable::Subject
|
2
|
-
|
2
|
+
|
3
3
|
def self.included(base)
|
4
|
-
base.has_many :applied_roles, :foreign_key => 'subject_id'
|
4
|
+
base.has_many :applied_roles, :foreign_key => 'subject_id', :dependent => :destroy
|
5
5
|
end
|
6
6
|
|
7
7
|
# Add a role to the subject scoped to the given resource or global if no resource given.
|
@@ -16,8 +16,8 @@ module Roleable::Subject
|
|
16
16
|
#
|
17
17
|
def add_role(role_name, resource = nil)
|
18
18
|
role = ::Role.find_by_name(role_name) or return
|
19
|
-
|
20
|
-
::AppliedRole.create_if_unique!(:subject_id => self.id, :role => role, :resource => resource)
|
19
|
+
|
20
|
+
::AppliedRole.create_if_unique!(:subject_id => self.id, :role => role, :resource => resource)
|
21
21
|
end
|
22
22
|
|
23
23
|
# Check if the subject has the given role for the given resource, or if they have the role globally
|
@@ -37,7 +37,7 @@ module Roleable::Subject
|
|
37
37
|
with_role_name(role_name).
|
38
38
|
exists?
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
# Remove the given role from the subject for the given resource, or globally if no resource given.
|
42
42
|
#
|
43
43
|
# Returns <tt>true</tt> if the role was found and deleted, <tt>false</tt> otherwise.
|
@@ -49,12 +49,12 @@ module Roleable::Subject
|
|
49
49
|
#
|
50
50
|
def remove_role(role_name, resource = nil)
|
51
51
|
applied_roles = ::AppliedRole.with_subject(self).with_resource(resource).with_role_name(role_name)
|
52
|
-
|
52
|
+
|
53
53
|
deleted_count = applied_roles.delete_all
|
54
|
-
|
54
|
+
|
55
55
|
deleted_count > 0
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
# Return a list of resources of the given class, for which the subject has the given role.
|
59
59
|
# If passed an array or roles, returns resources for which the subject has any of the roles.
|
60
60
|
#
|
@@ -67,16 +67,16 @@ module Roleable::Subject
|
|
67
67
|
applied_roles = ::AppliedRole.with_subject(self).with_role_name(role_name).with_resource_class(resource_class)
|
68
68
|
resource_class.includes(:applied_roles).merge(applied_roles)
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
# Return a list of roles that the subject has for the given resource.
|
72
72
|
#
|
73
73
|
# ==== Examples
|
74
74
|
#
|
75
75
|
# user.roles_for_resource(page) # => [role1, role2, ...]
|
76
|
-
#
|
76
|
+
#
|
77
77
|
def roles_for_resource(resource)
|
78
|
-
applied_roles = ::AppliedRole.with_subject(self).with_resource(resource)
|
78
|
+
applied_roles = ::AppliedRole.with_subject(self).with_resource(resource)
|
79
79
|
::Role.includes(:applied_roles).merge(applied_roles)
|
80
80
|
end
|
81
|
-
|
82
|
-
end
|
81
|
+
|
82
|
+
end
|
data/lib/roleable/version.rb
CHANGED
data/roleable.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "roleable"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Roleable::VERSION
|
17
|
-
|
17
|
+
|
18
18
|
gem.add_development_dependency 'rake', '~> 0.9'
|
19
19
|
gem.add_development_dependency 'rspec', '~> 2.8'
|
20
20
|
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
@@ -3,62 +3,72 @@ require 'spec_helper'
|
|
3
3
|
describe Roleable::Resource do
|
4
4
|
|
5
5
|
include_context 'with models'
|
6
|
-
|
6
|
+
|
7
|
+
before do
|
8
|
+
@page = Page.create
|
9
|
+
@editor_role = Role.create(:name => 'editor')
|
10
|
+
@author_role = Role.create(:name => 'author')
|
11
|
+
end
|
12
|
+
|
7
13
|
describe '#subjects_with_role' do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@page = Page.create
|
11
|
-
@editor_role = Role.create(:name => 'editor')
|
12
|
-
@author_role = Role.create(:name => 'author')
|
13
|
-
end
|
14
14
|
|
15
15
|
context 'with a single role' do
|
16
|
-
|
16
|
+
|
17
17
|
context 'with a role that doesnt exist' do
|
18
18
|
it 'returns an empty list' do
|
19
19
|
@page.subjects_with_role(:notarole).should be_empty
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
context 'when multiple users have the given role' do
|
24
|
-
|
24
|
+
|
25
25
|
before do
|
26
26
|
3.times { User.create.add_role(:editor, @page) }
|
27
|
-
end
|
28
|
-
|
27
|
+
end
|
28
|
+
|
29
29
|
|
30
|
-
it 'returns a list of the users' do
|
30
|
+
it 'returns a list of the users' do
|
31
31
|
users = @page.subjects_with_role(:editor)
|
32
|
-
|
32
|
+
|
33
33
|
users.length.should == 3
|
34
34
|
users.first.should be_a(User)
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
it 'doesnt return users that dont have the role' do
|
38
38
|
other_page = Page.create
|
39
39
|
other_user = User.create.add_role(:editor, other_page)
|
40
40
|
|
41
41
|
users = @page.subjects_with_role(:editor)
|
42
|
-
|
42
|
+
|
43
43
|
users.length.should == 3
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
context 'with a list of roles' do
|
51
51
|
it 'returns a list of users that have any of the given roles for this resource' do
|
52
52
|
3.times { User.create.add_role(:editor, @page) }
|
53
53
|
2.times { User.create.add_role(:author, @page) }
|
54
54
|
User.create
|
55
|
-
|
55
|
+
|
56
56
|
users = @page.subjects_with_role([:editor, :author])
|
57
|
-
|
57
|
+
|
58
58
|
users.length.should == 5
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
end
|
63
63
|
|
64
|
-
|
64
|
+
describe '#destroy' do
|
65
|
+
|
66
|
+
it 'destroys any associated applied_roles' do
|
67
|
+
User.create.add_role(:editor, @page)
|
68
|
+
|
69
|
+
expect { @page.destroy }.to change { AppliedRole.count}.by(-1)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Roleable::Subject do
|
4
4
|
|
5
5
|
include_context 'with models'
|
6
|
-
|
6
|
+
|
7
7
|
before do
|
8
8
|
@user = User.create
|
9
9
|
@admin_role = Role.create(:name => 'admin')
|
@@ -12,66 +12,66 @@ describe Roleable::Subject do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
describe '#add_role' do
|
15
|
-
|
15
|
+
|
16
16
|
context 'with a role that doesnt exist' do
|
17
|
-
|
17
|
+
|
18
18
|
before do
|
19
19
|
@result = @user.add_role(:notarole)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it 'returns false' do
|
23
23
|
@result.should be_false
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
it 'doesnt create a new applied_role' do
|
27
27
|
AppliedRole.count.should == 0
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
context 'without a resource' do
|
33
|
-
|
33
|
+
|
34
34
|
before do
|
35
35
|
@user_role = @user.add_role(:admin)
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
it 'creates a new user role' do
|
39
39
|
AppliedRole.count.should == 1
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it 'associates the user role with the given user' do
|
43
43
|
@user_role.subject.should == @user
|
44
|
-
end
|
44
|
+
end
|
45
45
|
|
46
46
|
it 'associates the user role with the given role' do
|
47
47
|
@user_role.role.should == @admin_role
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it 'sets the resource to nil' do
|
51
51
|
@user_role.resource.should == nil
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
context 'with a resource' do
|
57
|
-
|
57
|
+
|
58
58
|
before do
|
59
59
|
@page = Page.create
|
60
60
|
@user_role = @user.add_role(:admin, @page)
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
it 'associates the user role with the given resource' do
|
64
64
|
@user_role.resource.should == @page
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
context 'when the user already has the given role for the resource' do
|
68
68
|
it 'doesnt create another user role' do
|
69
69
|
expect { @user.add_role(:admin, @page) }.to_not change(AppliedRole, :count)
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
end
|
76
76
|
|
77
77
|
describe '#has_role?' do
|
@@ -83,47 +83,47 @@ describe Roleable::Subject do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
context 'without a resource' do
|
86
|
-
|
87
|
-
context 'when the user DOESNT have the given role' do
|
86
|
+
|
87
|
+
context 'when the user DOESNT have the given role' do
|
88
88
|
it 'is false' do
|
89
89
|
@user.has_role?(:admin).should be_false
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
context 'when the user DOES have the given role' do
|
94
94
|
it 'is true' do
|
95
95
|
@user.add_role(:admin)
|
96
96
|
@user.has_role?(:admin).should be_true
|
97
97
|
end
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
context 'with a resource' do
|
103
|
-
|
103
|
+
|
104
104
|
before do
|
105
105
|
@page = Page.create
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
context 'when the user DOESNT have the role for that resource' do
|
109
109
|
it 'is false' do
|
110
110
|
@user.has_role?(:editor, @page).should be_false
|
111
111
|
end
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
context 'when the user DOES have the role for that resource' do
|
115
115
|
it 'is true' do
|
116
116
|
@user.add_role(:editor, @page)
|
117
117
|
@user.has_role?(:editor, @page).should be_true
|
118
118
|
end
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
end
|
124
124
|
|
125
125
|
describe '#remove_role' do
|
126
|
-
|
126
|
+
|
127
127
|
context 'global' do
|
128
128
|
|
129
129
|
context 'when the user doesnt have the role' do
|
@@ -131,69 +131,69 @@ describe Roleable::Subject do
|
|
131
131
|
@user.remove_role(:admin).should be_false
|
132
132
|
end
|
133
133
|
end
|
134
|
-
|
134
|
+
|
135
135
|
context 'when the role doesnt exist' do
|
136
136
|
it 'is false' do
|
137
137
|
@user.remove_role(:notarole).should be_false
|
138
138
|
end
|
139
139
|
end
|
140
|
-
|
140
|
+
|
141
141
|
context 'when the user has the given role' do
|
142
|
-
|
142
|
+
|
143
143
|
before do
|
144
144
|
@user.add_role(:admin)
|
145
145
|
@result = @user.remove_role(:admin)
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
it 'is true' do
|
149
149
|
@result.should be_true
|
150
150
|
end
|
151
|
-
|
151
|
+
|
152
152
|
it 'removes the given role' do
|
153
153
|
@user.has_role?(:admin).should be_false
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
end
|
157
|
-
|
157
|
+
|
158
158
|
end
|
159
|
-
|
159
|
+
|
160
160
|
end
|
161
161
|
|
162
162
|
describe '#resources_with_role' do
|
163
|
-
|
163
|
+
|
164
164
|
context 'with a single role' do
|
165
|
-
|
165
|
+
|
166
166
|
context 'when the user has the given role for several resources of the given class' do
|
167
167
|
it 'returns a list containing those resources' do
|
168
168
|
3.times { @user.add_role(:editor, Page.create) }
|
169
|
-
|
169
|
+
|
170
170
|
pages = @user.resources_with_role(:editor, Page)
|
171
|
-
|
171
|
+
|
172
172
|
pages.length.should == 3
|
173
173
|
pages.first.should be_a(Page)
|
174
174
|
end
|
175
175
|
end
|
176
|
-
|
176
|
+
|
177
177
|
context 'when the user doesnt have the given role for any resources of the given class' do
|
178
178
|
it 'returns an empty list' do
|
179
|
-
@user.resources_with_role(:editor, Page).should be_empty
|
179
|
+
@user.resources_with_role(:editor, Page).should be_empty
|
180
180
|
end
|
181
181
|
end
|
182
|
-
|
182
|
+
|
183
183
|
end
|
184
|
-
|
184
|
+
|
185
185
|
context 'with a list of roles' do
|
186
186
|
it 'returns a list of resources the user has those roles for' do
|
187
187
|
3.times { @user.add_role(:editor, Page.create) }
|
188
188
|
2.times { @user.add_role(:author, Page.create) }
|
189
189
|
Page.create
|
190
|
-
|
190
|
+
|
191
191
|
pages = @user.resources_with_role([:editor, :author], Page)
|
192
|
-
|
193
|
-
pages.length.should == 5
|
192
|
+
|
193
|
+
pages.length.should == 5
|
194
194
|
end
|
195
195
|
end
|
196
|
-
|
196
|
+
|
197
197
|
end
|
198
198
|
|
199
199
|
describe '#roles_for_resource' do
|
@@ -201,11 +201,21 @@ describe Roleable::Subject do
|
|
201
201
|
page = Page.create
|
202
202
|
@user.add_role(:editor, page)
|
203
203
|
@user.add_role(:admin, page)
|
204
|
-
|
204
|
+
|
205
205
|
roles = @user.roles_for_resource(page)
|
206
206
|
roles.length.should == 2
|
207
207
|
roles.first.should be_a(Role)
|
208
208
|
end
|
209
209
|
end
|
210
|
-
|
210
|
+
|
211
|
+
describe '#destroy' do
|
212
|
+
|
213
|
+
it 'destroys any associated applied_roles' do
|
214
|
+
@user.add_role(:admin)
|
215
|
+
|
216
|
+
expect { @user.destroy }.to change { AppliedRole.count}.by(-1)
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
211
221
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,78 +1,108 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: roleable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Mitch Crowe
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2012-05-23 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
23
|
none: false
|
18
|
-
requirements:
|
24
|
+
requirements:
|
19
25
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 25
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 9
|
31
|
+
version: "0.9"
|
32
|
+
prerelease: false
|
33
|
+
name: rake
|
34
|
+
version_requirements: *id001
|
22
35
|
type: :development
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 19
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 8
|
46
|
+
version: "2.8"
|
23
47
|
prerelease: false
|
24
|
-
version_requirements: *2162720700
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
48
|
name: rspec
|
27
|
-
|
49
|
+
version_requirements: *id002
|
50
|
+
type: :development
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
28
53
|
none: false
|
29
|
-
requirements:
|
54
|
+
requirements:
|
30
55
|
- - ~>
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
33
|
-
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 9
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 3
|
61
|
+
version: "1.3"
|
34
62
|
prerelease: false
|
35
|
-
version_requirements: *2162719820
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
63
|
name: sqlite3
|
38
|
-
|
64
|
+
version_requirements: *id003
|
65
|
+
type: :development
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
39
68
|
none: false
|
40
|
-
requirements:
|
69
|
+
requirements:
|
41
70
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 7
|
73
|
+
segments:
|
74
|
+
- 3
|
75
|
+
- 0
|
76
|
+
version: "3.0"
|
45
77
|
prerelease: false
|
46
|
-
version_requirements: *2162719040
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
78
|
name: activerecord
|
49
|
-
|
79
|
+
version_requirements: *id004
|
80
|
+
type: :development
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
50
83
|
none: false
|
51
|
-
requirements:
|
84
|
+
requirements:
|
52
85
|
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 15
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
- 2
|
91
|
+
version: "0.2"
|
56
92
|
prerelease: false
|
57
|
-
version_requirements: *2162718020
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
93
|
name: with_model
|
60
|
-
|
61
|
-
none: false
|
62
|
-
requirements:
|
63
|
-
- - ~>
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
version: '0.2'
|
94
|
+
version_requirements: *id005
|
66
95
|
type: :development
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: *2162717140
|
69
96
|
description: Roles solution for active-record-backed Rails 3 applications
|
70
|
-
email:
|
97
|
+
email:
|
71
98
|
- crowe.mitch@gmail.com
|
72
99
|
executables: []
|
100
|
+
|
73
101
|
extensions: []
|
102
|
+
|
74
103
|
extra_rdoc_files: []
|
75
|
-
|
104
|
+
|
105
|
+
files:
|
76
106
|
- .gitignore
|
77
107
|
- .rspec
|
78
108
|
- .travis.yml
|
@@ -99,37 +129,41 @@ files:
|
|
99
129
|
- spec/roleable/subject_spec.rb
|
100
130
|
- spec/spec_helper.rb
|
101
131
|
- spec/support/shared_contexts.rb
|
132
|
+
has_rdoc: true
|
102
133
|
homepage: https://github.com/mcrowe/roleable
|
103
134
|
licenses: []
|
135
|
+
|
104
136
|
post_install_message:
|
105
137
|
rdoc_options: []
|
106
|
-
|
138
|
+
|
139
|
+
require_paths:
|
107
140
|
- lib
|
108
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
142
|
none: false
|
110
|
-
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
|
114
|
-
segments:
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: 3
|
147
|
+
segments:
|
115
148
|
- 0
|
116
|
-
|
117
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
version: "0"
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
151
|
none: false
|
119
|
-
requirements:
|
120
|
-
- -
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
|
123
|
-
segments:
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
hash: 3
|
156
|
+
segments:
|
124
157
|
- 0
|
125
|
-
|
158
|
+
version: "0"
|
126
159
|
requirements: []
|
160
|
+
|
127
161
|
rubyforge_project:
|
128
|
-
rubygems_version: 1.
|
162
|
+
rubygems_version: 1.3.9.4
|
129
163
|
signing_key:
|
130
164
|
specification_version: 3
|
131
165
|
summary: Roles solution for active-record-backed Rails 3 applications
|
132
|
-
test_files:
|
166
|
+
test_files:
|
133
167
|
- spec/roleable/resource_spec.rb
|
134
168
|
- spec/roleable/subject_spec.rb
|
135
169
|
- spec/spec_helper.rb
|