roles_mongoid 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/Rakefile +11 -7
  2. data/VERSION +1 -1
  3. data/lib/roles_mongoid/base.rb +6 -4
  4. data/lib/roles_mongoid/role.rb +3 -1
  5. data/lib/roles_mongoid/strategy/multi/many_roles.rb +51 -35
  6. data/lib/roles_mongoid/strategy/multi/role_strings.rb +43 -22
  7. data/lib/roles_mongoid/strategy/multi/roles_mask.rb +67 -18
  8. data/lib/roles_mongoid/strategy/multi.rb +59 -0
  9. data/lib/roles_mongoid/strategy/shared.rb +29 -0
  10. data/lib/roles_mongoid/strategy/single/admin_flag.rb +20 -18
  11. data/lib/roles_mongoid/strategy/single/one_role.rb +55 -24
  12. data/lib/roles_mongoid/strategy/single/role_string.rb +54 -22
  13. data/lib/roles_mongoid/strategy/single.rb +34 -0
  14. data/roles_mongoid.gemspec +44 -34
  15. data/sandbox/single_role.rb +25 -11
  16. data/spec/{generators → roles_mongoid/generators}/roles_generator_spec.rb +0 -0
  17. data/spec/roles_mongoid/strategy/api_examples.rb +200 -0
  18. data/spec/roles_mongoid/strategy/multi/many_roles_spec.rb +40 -0
  19. data/spec/roles_mongoid/strategy/multi/role_strings_spec.rb +19 -0
  20. data/spec/roles_mongoid/strategy/multi/roles_mask_spec.rb +39 -0
  21. data/spec/roles_mongoid/strategy/single/admin_flag_spec.rb +18 -0
  22. data/spec/roles_mongoid/strategy/single/one_role_spec.rb +19 -0
  23. data/spec/roles_mongoid/strategy/single/role_string_spec.rb +19 -0
  24. data/spec/roles_mongoid/strategy/user_setup.rb +13 -0
  25. data/spec/spec_helper.rb +0 -13
  26. metadata +62 -43
  27. data/spec/roles_mongoid/admin_flag_spec.rb +0 -74
  28. data/spec/roles_mongoid/many_roles_spec.rb +0 -74
  29. data/spec/roles_mongoid/one_role_spec.rb +0 -74
  30. data/spec/roles_mongoid/role_string_spec.rb +0 -73
  31. data/spec/roles_mongoid/role_strings_spec.rb +0 -73
  32. data/spec/roles_mongoid/roles_mask_spec.rb +0 -73
@@ -0,0 +1,200 @@
1
+ describe "Roles for Mongoid: #{api_name}" do
2
+ require "roles_mongoid/strategy/user_setup.rb"
3
+
4
+ before do
5
+ default_user_setup
6
+ end
7
+
8
+ describe '#in_role' do
9
+ it "should return first user matching role" do
10
+ if User.respond_to? :in_role
11
+ User.in_role(:guest).first.name.should == 'Guest user'
12
+ User.in_role(:admin).first.name.should == 'Admin user'
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#in_any_role' do
18
+ it "should return first user matching role" do
19
+ if User.respond_to? :in_roles
20
+ User.in_any_role(:guest, :user).first.name.should == 'Guest user'
21
+ User.in_any_role(:admin, :guest).should be_empty
22
+ end
23
+ end
24
+ end
25
+
26
+ it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
27
+ # mutation API
28
+ [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
29
+ @admin_user.respond_to?(api_method).should be_true
30
+ end
31
+
32
+ # inspection API
33
+ [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
34
+ @admin_user.respond_to?(api_method).should be_true
35
+ end
36
+
37
+ # class method API
38
+ [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
39
+ @admin_user.class.respond_to?(class_api_method).should be_true
40
+ end
41
+ end
42
+ #
43
+ describe '#valid_role?' do
44
+ it "should be true that the admin user has a valid role of :guest" do
45
+ # @admin_user.valid_role?(:guest).should be_true
46
+ end
47
+
48
+ it "should be true that the User class has a valid role of :guest" do
49
+ # User.valid_role?(:guest).should be_true
50
+ end
51
+ end
52
+
53
+ describe '#valid_roles' do
54
+ it "should be true that the admin user has a valid role of :guest" do
55
+ # @admin_user.valid_roles.should include(:guest, :admin)
56
+ end
57
+
58
+ it "should be true that the User class has a valid role of :guest" do
59
+ User.valid_roles.should include(:guest, :admin)
60
+ end
61
+ end
62
+
63
+ describe '#valid_roles?' do
64
+ it "should be true that the admin user has a valid role of :guest" do
65
+ @admin_user.valid_roles?(:guest, :admin).should be_true
66
+ end
67
+
68
+ it "should be true that the User class has a valid role of :guest" do
69
+ User.valid_roles?(:guest, :admin).should be_true
70
+ end
71
+ end
72
+
73
+ describe '#has_role?' do
74
+ it "should have admin user role to :admin and not to :user" do
75
+ @admin_user.has_role?(:user).should be_false
76
+ @admin_user.has_role?(:admin).should be_true
77
+ end
78
+
79
+ it "should be true that guest user has role :guest and not :admin" do
80
+ @guest_user.has_role?(:guest).should be_true
81
+ @guest_user.has_role?(:admin).should be_false
82
+ end
83
+ end
84
+
85
+ describe '#has?' do
86
+ it "should be true that the admin_user has the :admin role" do
87
+ @admin_user.has?(:admin).should be_true
88
+ end
89
+
90
+ it "should NOT be true that the admin_user has the :admin role" do
91
+ @guest_user.has?(:admin).should be_false
92
+ end
93
+ end
94
+
95
+ describe '#has_roles?' do
96
+ it "should be true that the admin_user has the roles :admin" do
97
+ @admin_user.has_roles?(:admin).should be_true
98
+ end
99
+
100
+ it "should NOT be true that the user has the roles :admin" do
101
+ @guest_user.has_roles?(:admin).should be_false
102
+ end
103
+ end
104
+
105
+ describe '#roles_list' do
106
+ it "should be true that the first role of admin_user is the :admin role" do
107
+ @admin_user.roles_list.should include(:admin)
108
+ end
109
+
110
+ it "should be true that the first role of admin_user is the :user role" do
111
+ case @normal_user.class.role_strategy.multiplicity
112
+ when :single
113
+ @normal_user.roles_list.should include(:guest)
114
+ # @normal_user.roles_list.should include(:user)
115
+ when :multi
116
+ @normal_user.roles_list.should include(:user, :guest)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#roles' do
122
+ it "should be true that the roles of admin_user is an array with the role :admin" do
123
+ roles = @admin_user.roles
124
+ if defined?(Role) && roles.kind_of?(Role)
125
+ roles.name.to_sym.should == :admin
126
+ elsif roles.kind_of? Array
127
+ if @normal_user.class.role_strategy.type == :complex
128
+ roles.first.name.to_sym.should == :admin
129
+ end
130
+ if @normal_user.class.role_strategy.name == :admin_flag
131
+ roles.first.should == true
132
+ end
133
+ else
134
+ roles.to_sym.should == :admin
135
+ end
136
+ end
137
+ end
138
+
139
+ describe '#admin?' do
140
+ it "should be true that admin_user is in the :admin role" do
141
+ @admin_user.admin?.should be_true
142
+ end
143
+
144
+ it "should NOT be true that the user is in the :admin role" do
145
+ @guest_user.admin?.should be_false
146
+ end
147
+ end
148
+
149
+ describe '#is?' do
150
+ it "should be true that admin_user is in the :admin role" do
151
+ @admin_user.is?(:admin).should be_true
152
+ end
153
+
154
+ it "should NOT be true that the user is in the :admin role" do
155
+ @guest_user.is?(:admin).should be_false
156
+ end
157
+ end
158
+
159
+ describe '#roles=' do
160
+ it "should set user role to :admin" do
161
+ @guest_user.roles = :admin
162
+ @guest_user.has_role?(:admin).should be_true
163
+ @guest_user.roles = :guest
164
+ end
165
+ end
166
+
167
+ describe '#exchange_roles' do
168
+ it "should exchange user role :user with role :admin" do
169
+ @guest_user.exchange_role :guest, :with => :admin
170
+ @guest_user.has?(:guest).should be_false
171
+ @guest_user.has?(:admin).should be_true
172
+ end
173
+
174
+ it "should exchange user role :admin with roles :user and :guest" do
175
+ case @admin_user.class.role_strategy.multiplicity
176
+ when :single
177
+ lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
178
+ when :multi
179
+ @admin_user.exchange_role :admin, :with => [:user, :guest]
180
+ @admin_user.has_role?(:user).should be_true
181
+ @admin_user.has_role?(:guest).should be_true
182
+ @admin_user.has?(:admin).should be_false
183
+ end
184
+ end
185
+ end
186
+
187
+ describe '#remove_roles' do
188
+ it "should remove user role :admin using #remove_roles" do
189
+ @admin_user.remove_roles :admin
190
+ @admin_user.has_role?(:admin).should_not be_true
191
+ end
192
+
193
+ it "should remove user role :admin using #remove_role" do
194
+ @guest_user.add_role :admin
195
+ @guest_user.has_role?(:admin).should be_true
196
+ @guest_user.remove_role :admin
197
+ @guest_user.has_role?(:admin).should_not be_true
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :many_roles
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :many_roles, :default
10
+ role_class :role
11
+
12
+ valid_roles_are :admin, :guest, :user
13
+ end
14
+
15
+ def api_name
16
+ :many_roles
17
+ end
18
+
19
+ load 'roles_mongoid/strategy/api_examples.rb'
20
+
21
+ #
22
+ # require 'spec_helper'
23
+ # use_roles_strategy :many_roles
24
+ #
25
+ # class User
26
+ # include Mongoid::Document
27
+ # include Roles::Mongoid
28
+ #
29
+ # strategy :many_roles, :default
30
+ # role_class :role
31
+ # valid_roles_are :admin, :guest
32
+ #
33
+ # field :name, :type => String
34
+ # end
35
+ #
36
+ # describe "Roles for Mongoid: :many_roles strategy" do
37
+ # load "roles_mongoid/user_setup"
38
+ # load "roles_generic/rspec/api"
39
+ # end
40
+ #
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :role_strings
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :role_strings, :default
10
+
11
+ valid_roles_are :admin, :guest, :user
12
+ end
13
+
14
+ def api_name
15
+ :role_strings
16
+ end
17
+
18
+ load 'roles_mongoid/strategy/api_examples.rb'
19
+
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :roles_mask
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :roles_mask, :default
10
+
11
+ valid_roles_are :admin, :guest, :user
12
+ end
13
+
14
+ def api_name
15
+ :roles_mask
16
+ end
17
+
18
+ load 'roles_mongoid/strategy/api_examples.rb'
19
+
20
+
21
+
22
+ # require 'spec_helper'
23
+ # use_roles_strategy :roles_mask
24
+ #
25
+ # class User
26
+ # include Mongoid::Document
27
+ # include Roles::Mongoid
28
+ #
29
+ # strategy :roles_mask, :default
30
+ # valid_roles_are :admin, :guest
31
+ #
32
+ # field :name, :type => String
33
+ # end
34
+ #
35
+ # describe "Roles for Mongoid: :roles_mask strategy" do
36
+ # load "roles_mongoid/user_setup"
37
+ # load "roles_generic/rspec/api"
38
+ # end
39
+ #
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :admin_flag
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :admin_flag, :default
9
+ valid_roles_are :admin, :guest
10
+
11
+ field :name, :type => String
12
+ end
13
+
14
+ def api_name
15
+ :admin_flag
16
+ end
17
+
18
+ load 'roles_mongoid/strategy/api_examples.rb'
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :one_role
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :one_role, :default
10
+ role_class :role
11
+
12
+ valid_roles_are :admin, :guest, :user
13
+ end
14
+
15
+ def api_name
16
+ :one_role
17
+ end
18
+
19
+ load 'roles_mongoid/strategy/api_examples.rb'
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :role_string
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :role_string, :default
10
+
11
+ valid_roles_are :admin, :guest, :user
12
+ end
13
+
14
+ def api_name
15
+ :role_string
16
+ end
17
+
18
+ load 'roles_mongoid/strategy/api_examples.rb'
19
+
@@ -0,0 +1,13 @@
1
+ def default_user_setup
2
+ @guest_user = User.create(:name => 'Guest user')
3
+ @guest_user.add_roles :guest
4
+ @guest_user.save
5
+
6
+ @normal_user = User.create(:name => 'Normal user')
7
+ @normal_user.add_roles :guest, :user
8
+ @normal_user.save
9
+
10
+ @admin_user = User.create(:name => 'Admin user')
11
+ @admin_user.add_roles :admin
12
+ @admin_user.save
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,25 +4,12 @@ require 'roles_mongoid'
4
4
 
5
5
  Mongoid.configure.master = Mongo::Connection.new.db('roles_mongoid')
6
6
 
7
- module Database
8
- def self.teardown
9
- # Mongoid.database.collections.each do |coll|
10
- # coll.remove
11
- # end
12
- end
13
- end
14
-
15
7
  Mongoid.database.collections.each do |coll|
16
8
  coll.remove
17
9
  end
18
10
 
19
11
  RSpec.configure do |config|
20
12
  config.mock_with :mocha
21
- # config.before do
22
- # Mongoid.database.collections.each do |coll|
23
- # coll.remove
24
- # end
25
- # end
26
13
  end
27
14
 
28
15
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 4
9
- version: 0.2.4
7
+ - 3
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-06 00:00:00 +02:00
17
+ date: 2010-11-23 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,15 +23,12 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ~>
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
- - 0
31
- - 0
32
- - beta
33
- - 22
34
- version: 2.0.0.beta.22
30
+ - 1
31
+ version: "2.1"
35
32
  type: :development
36
33
  version_requirements: *id001
37
34
  - !ruby/object:Gem::Dependency
@@ -40,15 +37,15 @@ dependencies:
40
37
  requirement: &id002 !ruby/object:Gem::Requirement
41
38
  none: false
42
39
  requirements:
43
- - - ~>
40
+ - - ">="
44
41
  - !ruby/object:Gem::Version
45
42
  segments:
46
43
  - 2
47
44
  - 0
48
45
  - 0
49
46
  - beta
50
- - 18
51
- version: 2.0.0.beta.18
47
+ - 19
48
+ version: 2.0.0.beta.19
52
49
  type: :runtime
53
50
  version_requirements: *id002
54
51
  - !ruby/object:Gem::Dependency
@@ -57,17 +54,17 @@ dependencies:
57
54
  requirement: &id003 !ruby/object:Gem::Requirement
58
55
  none: false
59
56
  requirements:
60
- - - ~>
57
+ - - ">="
61
58
  - !ruby/object:Gem::Version
62
59
  segments:
63
60
  - 1
64
- - 0
65
- - 7
66
- version: 1.0.7
61
+ - 1
62
+ - 1
63
+ version: 1.1.1
67
64
  type: :runtime
68
65
  version_requirements: *id003
69
66
  - !ruby/object:Gem::Dependency
70
- name: activesupport
67
+ name: sugar-high
71
68
  prerelease: false
72
69
  requirement: &id004 !ruby/object:Gem::Requirement
73
70
  none: false
@@ -75,10 +72,10 @@ dependencies:
75
72
  - - ~>
76
73
  - !ruby/object:Gem::Version
77
74
  segments:
78
- - 3
79
75
  - 0
76
+ - 3
80
77
  - 0
81
- version: 3.0.0
78
+ version: 0.3.0
82
79
  type: :runtime
83
80
  version_requirements: *id004
84
81
  - !ruby/object:Gem::Dependency
@@ -97,22 +94,22 @@ dependencies:
97
94
  type: :runtime
98
95
  version_requirements: *id005
99
96
  - !ruby/object:Gem::Dependency
100
- name: sugar-high
97
+ name: roles_generic
101
98
  prerelease: false
102
99
  requirement: &id006 !ruby/object:Gem::Requirement
103
100
  none: false
104
101
  requirements:
105
- - - ~>
102
+ - - ">="
106
103
  - !ruby/object:Gem::Version
107
104
  segments:
108
105
  - 0
109
- - 2
110
- - 11
111
- version: 0.2.11
106
+ - 3
107
+ - 0
108
+ version: 0.3.0
112
109
  type: :runtime
113
110
  version_requirements: *id006
114
111
  - !ruby/object:Gem::Dependency
115
- name: roles_generic
112
+ name: rails3_artifactor
116
113
  prerelease: false
117
114
  requirement: &id007 !ruby/object:Gem::Requirement
118
115
  none: false
@@ -121,11 +118,26 @@ dependencies:
121
118
  - !ruby/object:Gem::Version
122
119
  segments:
123
120
  - 0
124
- - 2
125
- - 7
126
- version: 0.2.7
121
+ - 3
122
+ - 1
123
+ version: 0.3.1
127
124
  type: :runtime
128
125
  version_requirements: *id007
126
+ - !ruby/object:Gem::Dependency
127
+ name: logging_assist
128
+ prerelease: false
129
+ requirement: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ segments:
135
+ - 0
136
+ - 1
137
+ - 6
138
+ version: 0.1.6
139
+ type: :runtime
140
+ version_requirements: *id008
129
141
  description: Makes it easy to set a role strategy on your User model in Mongoid
130
142
  email: kmandrup@gmail.com
131
143
  executables: []
@@ -151,9 +163,12 @@ files:
151
163
  - lib/roles_mongoid/namespaces.rb
152
164
  - lib/roles_mongoid/role.rb
153
165
  - lib/roles_mongoid/strategy.rb
166
+ - lib/roles_mongoid/strategy/multi.rb
154
167
  - lib/roles_mongoid/strategy/multi/many_roles.rb
155
168
  - lib/roles_mongoid/strategy/multi/role_strings.rb
156
169
  - lib/roles_mongoid/strategy/multi/roles_mask.rb
170
+ - lib/roles_mongoid/strategy/shared.rb
171
+ - lib/roles_mongoid/strategy/single.rb
157
172
  - lib/roles_mongoid/strategy/single/admin_flag.rb
158
173
  - lib/roles_mongoid/strategy/single/one_role.rb
159
174
  - lib/roles_mongoid/strategy/single/role_string.rb
@@ -162,13 +177,15 @@ files:
162
177
  - sandbox/single_role.rb
163
178
  - sandbox/test.rb
164
179
  - spec/generator_spec_helper.rb
165
- - spec/generators/roles_generator_spec.rb
166
- - spec/roles_mongoid/admin_flag_spec.rb
167
- - spec/roles_mongoid/many_roles_spec.rb
168
- - spec/roles_mongoid/one_role_spec.rb
169
- - spec/roles_mongoid/role_string_spec.rb
170
- - spec/roles_mongoid/role_strings_spec.rb
171
- - spec/roles_mongoid/roles_mask_spec.rb
180
+ - spec/roles_mongoid/generators/roles_generator_spec.rb
181
+ - spec/roles_mongoid/strategy/api_examples.rb
182
+ - spec/roles_mongoid/strategy/multi/many_roles_spec.rb
183
+ - spec/roles_mongoid/strategy/multi/role_strings_spec.rb
184
+ - spec/roles_mongoid/strategy/multi/roles_mask_spec.rb
185
+ - spec/roles_mongoid/strategy/single/admin_flag_spec.rb
186
+ - spec/roles_mongoid/strategy/single/one_role_spec.rb
187
+ - spec/roles_mongoid/strategy/single/role_string_spec.rb
188
+ - spec/roles_mongoid/strategy/user_setup.rb
172
189
  - spec/spec_helper.rb
173
190
  - tmp/rails/app/models/user.rb
174
191
  - tmp/rails/config/routes.rb
@@ -206,11 +223,13 @@ specification_version: 3
206
223
  summary: Implementation of Roles generic API for Mongoid
207
224
  test_files:
208
225
  - spec/generator_spec_helper.rb
209
- - spec/generators/roles_generator_spec.rb
210
- - spec/roles_mongoid/admin_flag_spec.rb
211
- - spec/roles_mongoid/many_roles_spec.rb
212
- - spec/roles_mongoid/one_role_spec.rb
213
- - spec/roles_mongoid/role_string_spec.rb
214
- - spec/roles_mongoid/role_strings_spec.rb
215
- - spec/roles_mongoid/roles_mask_spec.rb
226
+ - spec/roles_mongoid/generators/roles_generator_spec.rb
227
+ - spec/roles_mongoid/strategy/api_examples.rb
228
+ - spec/roles_mongoid/strategy/multi/many_roles_spec.rb
229
+ - spec/roles_mongoid/strategy/multi/role_strings_spec.rb
230
+ - spec/roles_mongoid/strategy/multi/roles_mask_spec.rb
231
+ - spec/roles_mongoid/strategy/single/admin_flag_spec.rb
232
+ - spec/roles_mongoid/strategy/single/one_role_spec.rb
233
+ - spec/roles_mongoid/strategy/single/role_string_spec.rb
234
+ - spec/roles_mongoid/strategy/user_setup.rb
216
235
  - spec/spec_helper.rb