roles_for_mongoid 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.rspec +1 -0
  2. data/README.markdown +14 -109
  3. data/Rakefile +5 -1
  4. data/VERSION +1 -1
  5. data/lib/generators/mongoid/roles/roles_generator.rb +48 -25
  6. data/lib/roles_mongoid.rb +9 -0
  7. data/lib/roles_mongoid/base.rb +35 -0
  8. data/lib/roles_mongoid/namespaces.rb +12 -0
  9. data/lib/roles_mongoid/role.rb +28 -0
  10. data/lib/roles_mongoid/strategy.rb +15 -0
  11. data/lib/roles_mongoid/strategy/multi/many_roles.rb +65 -0
  12. data/lib/roles_mongoid/strategy/multi/role_strings.rb +63 -0
  13. data/lib/roles_mongoid/strategy/multi/roles_mask.rb +81 -0
  14. data/lib/roles_mongoid/strategy/single/admin_flag.rb +53 -0
  15. data/lib/roles_mongoid/strategy/single/one_role.rb +56 -0
  16. data/lib/roles_mongoid/strategy/single/role_string.rb +51 -0
  17. data/roles_for_mongoid.gemspec +43 -24
  18. data/sandbox/roles_assign.rb +54 -0
  19. data/sandbox/single_role.rb +68 -0
  20. data/spec/roles_mongoid/admin_flag_spec.rb +74 -0
  21. data/spec/roles_mongoid/many_roles_spec.rb +74 -0
  22. data/spec/roles_mongoid/one_role_spec.rb +74 -0
  23. data/spec/roles_mongoid/role_string_spec.rb +73 -0
  24. data/spec/roles_mongoid/role_strings_spec.rb +73 -0
  25. data/spec/roles_mongoid/roles_mask_spec.rb +73 -0
  26. data/spec/spec_helper.rb +17 -7
  27. metadata +94 -25
  28. data/lib/roles_for_mongoid.rb +0 -1
  29. data/lib/roles_for_mongoid/role.rb +0 -4
  30. data/lib/roles_for_mongoid/strategies/admin_flag.rb +0 -17
  31. data/lib/roles_for_mongoid/strategies/inline_role.rb +0 -6
  32. data/lib/roles_for_mongoid/strategies/inline_roles.rb +0 -13
  33. data/lib/roles_for_mongoid/strategies/role_relations.rb +0 -34
  34. data/lib/roles_for_mongoid/strategies/roles_mask.rb +0 -52
  35. data/lib/roles_for_mongoid/user.rb +0 -12
  36. data/spec/roles_for_mongoid/user_admin_field_spec.rb +0 -24
  37. data/spec/roles_for_mongoid/user_inline_role_spec.rb +0 -23
  38. data/spec/roles_for_mongoid/user_inline_roles_spec.rb +0 -27
  39. data/spec/roles_for_mongoid/user_role_relations_spec.rb +0 -37
  40. data/spec/roles_for_mongoid/user_roles_mask_spec.rb +0 -29
  41. data/spec/spec.opts +0 -1
@@ -0,0 +1,74 @@
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
+ describe "Roles for Mongoid" do
15
+ after :each do
16
+ Database.teardown
17
+ end
18
+
19
+ before :each do
20
+ @user = User.create(:name => 'Kristian')
21
+ @user.roles = :guest
22
+ @user.save
23
+
24
+ @admin_user = User.create(:name => 'Admin user')
25
+ @admin_user.roles = :admin
26
+ @admin_user.save
27
+ end
28
+
29
+ describe "Admin flag strategy " do
30
+
31
+ describe '#in_role' do
32
+ it "should return first user maching role" do
33
+ User.in_role(:guest).first.name.should == 'Kristian'
34
+ User.in_role(:admin).first.name.should == 'Admin user'
35
+ end
36
+ end
37
+
38
+ describe 'roles API' do
39
+ it "should have admin user role to :admin" do
40
+ @admin_user.role.should == :admin
41
+ @admin_user.roles.should == [:admin]
42
+ @admin_user.admin?.should be_true
43
+
44
+ @admin_user.class.valid_roles.should == [:admin, :guest]
45
+
46
+ @admin_user.has_role?(:guest).should be_false
47
+
48
+ @admin_user.has_role?(:admin).should be_true
49
+ @admin_user.is?(:admin).should be_true
50
+ @admin_user.has_roles?(:admin).should be_true
51
+ @admin_user.has?(:admin).should be_true
52
+ end
53
+
54
+ it "should have user role to :guest" do
55
+ @user.roles.should == [:guest]
56
+ @user.admin?.should be_false
57
+
58
+ @user.has_role?(:guest).should be_true
59
+ @user.has_role?(:admin).should be_false
60
+ @user.is?(:admin).should be_false
61
+
62
+ @user.has_roles?(:guest).should be_true
63
+ @user.has?(:admin).should be_false
64
+ end
65
+
66
+ it "should set user role to :admin using =" do
67
+ @user.roles = :admin
68
+ @user.role.should == :admin
69
+ @user.has_role?(:admin).should be_true
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :many_roles
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :many_roles, :default
9
+ role_class :role
10
+
11
+ valid_roles_are :admin, :guest
12
+
13
+ field :name, :type => String
14
+ end
15
+
16
+ describe "Roles for Mongoid" do
17
+
18
+ after :each do
19
+ Database.teardown
20
+ end
21
+
22
+ context "default setup" do
23
+
24
+ before do
25
+ @user = User.create(:name => 'Kristian')
26
+ @user.add_roles :guest
27
+ @user.save
28
+
29
+ @admin_user = User.create(:name => 'Admin user')
30
+ @admin_user.roles = :admin
31
+ @admin_user.save
32
+ end
33
+
34
+ describe '#in_role' do
35
+ it "should return first user matching role" do
36
+ User.in_role(:guest).first.name.should == 'Kristian'
37
+ User.in_role(:admin).first.name.should == 'Admin user'
38
+ end
39
+ end
40
+
41
+ describe "Role API" do
42
+ it "should have admin user role to :admin" do
43
+ @admin_user.roles_list.first.should == :admin
44
+ @admin_user.admin?.should be_true
45
+
46
+ @admin_user.has_role?(:guest).should be_false
47
+
48
+ @admin_user.has_role?(:admin).should be_true
49
+ @admin_user.is?(:admin).should be_true
50
+ @admin_user.has_roles?(:admin).should be_true
51
+ @admin_user.has?(:admin).should be_true
52
+ end
53
+
54
+ it "should have user role to :guest" do
55
+ @user.roles_list.first.should == :guest
56
+ @user.admin?.should be_false
57
+
58
+ @user.has_role?(:guest).should be_true
59
+ @user.has_role?(:admin).should be_false
60
+ @user.is?(:admin).should be_false
61
+
62
+ @user.has_roles?(:admin).should be_false
63
+ @user.has?(:admin).should be_false
64
+ end
65
+
66
+ it "should set user role to :admin using roles=" do
67
+ @user.roles = :admin
68
+ @user.roles_list.first.should == :admin
69
+ @user.has_role?(:admin).should be_true
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :one_role
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :one_role, :default
9
+ role_class :role
10
+
11
+ valid_roles_are :admin, :guest
12
+
13
+ field :name, :type => String
14
+ end
15
+
16
+ describe "Roles for Mongoid" do
17
+
18
+ after :each do
19
+ Database.teardown
20
+ end
21
+
22
+ context "default setup" do
23
+
24
+ before do
25
+ @user = User.create(:name => 'Kristian')
26
+ @user.role = :guest
27
+ @user.save
28
+
29
+ @admin_user = User.create(:name => 'Admin user')
30
+ @admin_user.role = :admin
31
+ @admin_user.save
32
+ end
33
+
34
+ describe '#in_role' do
35
+ it "should return first user matching role" do
36
+ User.in_role(:guest).first.name.should == 'Kristian'
37
+ User.in_role(:admin).first.name.should == 'Admin user'
38
+ end
39
+ end
40
+
41
+ describe "Role API" do
42
+ it "should have admin user role to :admin" do
43
+ @admin_user.roles_list.first.should == :admin
44
+ @admin_user.admin?.should be_true
45
+
46
+ @admin_user.has_role?(:guest).should be_false
47
+
48
+ @admin_user.has_role?(:admin).should be_true
49
+ @admin_user.is?(:admin).should be_true
50
+ @admin_user.has_roles?(:admin).should be_true
51
+ @admin_user.has?(:admin).should be_true
52
+ end
53
+
54
+ it "should have user role to :guest" do
55
+ @user.roles_list.first.should == :guest
56
+ @user.admin?.should be_false
57
+
58
+ @user.has_role?(:guest).should be_true
59
+ @user.has_role?(:admin).should be_false
60
+ @user.is?(:admin).should be_false
61
+
62
+ @user.has_roles?(:admin).should be_false
63
+ @user.has?(:admin).should be_false
64
+ end
65
+
66
+ it "should set user role to :admin using roles=" do
67
+ @user.roles = :admin
68
+ @user.roles_list.first.should == :admin
69
+ @user.has_role?(:admin).should be_true
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :role_string
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :role_string, :default
9
+
10
+ valid_roles_are :admin, :guest
11
+
12
+ field :name, :type => String
13
+ end
14
+
15
+ describe "Roles for Mongoid" do
16
+
17
+ after :each do
18
+ Database.teardown
19
+ end
20
+
21
+ context "default setup" do
22
+
23
+ before do
24
+ @user = User.create(:name => 'Kristian')
25
+ @user.role = :guest
26
+ @user.save
27
+
28
+ @admin_user = User.create(:name => 'Admin user')
29
+ @admin_user.roles = :admin
30
+ @admin_user.save
31
+ end
32
+
33
+ describe '#in_role' do
34
+ it "should return first user matching role" do
35
+ User.in_role(:guest).first.name.should == 'Kristian'
36
+ User.in_role(:admin).first.name.should == 'Admin user'
37
+ end
38
+ end
39
+
40
+ describe "Role API" do
41
+ it "should have admin user role to :admin" do
42
+ @admin_user.roles_list.first.should == :admin
43
+ @admin_user.admin?.should be_true
44
+
45
+ @admin_user.has_role?(:guest).should be_false
46
+
47
+ @admin_user.has_role?(:admin).should be_true
48
+ @admin_user.is?(:admin).should be_true
49
+ @admin_user.has_roles?(:admin).should be_true
50
+ @admin_user.has?(:admin).should be_true
51
+ end
52
+
53
+ it "should have user role to :guest" do
54
+ @user.roles_list.first.should == :guest
55
+ @user.admin?.should be_false
56
+
57
+ @user.has_role?(:guest).should be_true
58
+ @user.has_role?(:admin).should be_false
59
+ @user.is?(:admin).should be_false
60
+
61
+ @user.has_roles?(:admin).should be_false
62
+ @user.has?(:admin).should be_false
63
+ end
64
+
65
+ it "should set user role to :admin using roles=" do
66
+ @user.roles = :admin
67
+ @user.roles_list.first.should == :admin
68
+ @user.has_role?(:admin).should be_true
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :role_strings
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :role_strings, :default
9
+
10
+ valid_roles_are :admin, :guest
11
+
12
+ field :name, :type => String
13
+ end
14
+
15
+ describe "Roles for Mongoid" do
16
+
17
+ after :each do
18
+ Database.teardown
19
+ end
20
+
21
+ context "default setup" do
22
+
23
+ before do
24
+ @user = User.create(:name => 'Kristian')
25
+ @user.role = :guest
26
+ @user.save
27
+
28
+ @admin_user = User.create(:name => 'Admin user')
29
+ @admin_user.roles = :admin
30
+ @admin_user.save
31
+ end
32
+
33
+ describe '#in_role' do
34
+ it "should return first user matching role" do
35
+ User.in_role(:guest).first.name.should == 'Kristian'
36
+ User.in_role(:admin).first.name.should == 'Admin user'
37
+ end
38
+ end
39
+
40
+ describe "Role API" do
41
+ it "should have admin user role to :admin" do
42
+ @admin_user.roles_list.first.should == :admin
43
+ @admin_user.admin?.should be_true
44
+
45
+ @admin_user.has_role?(:guest).should be_false
46
+
47
+ @admin_user.has_role?(:admin).should be_true
48
+ @admin_user.is?(:admin).should be_true
49
+ @admin_user.has_roles?(:admin).should be_true
50
+ @admin_user.has?(:admin).should be_true
51
+ end
52
+
53
+ it "should have user role to :guest" do
54
+ @user.roles_list.first.should == :guest
55
+ @user.admin?.should be_false
56
+
57
+ @user.has_role?(:guest).should be_true
58
+ @user.has_role?(:admin).should be_false
59
+ @user.is?(:admin).should be_false
60
+
61
+ @user.has_roles?(:admin).should be_false
62
+ @user.has?(:admin).should be_false
63
+ end
64
+
65
+ it "should set user role to :admin using roles=" do
66
+ @user.roles = :admin
67
+ @user.roles_list.first.should == :admin
68
+ @user.has_role?(:admin).should be_true
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :roles_mask
3
+
4
+ class User
5
+ include Mongoid::Document
6
+ include Roles::Mongoid
7
+
8
+ strategy :roles_mask, :default
9
+
10
+ valid_roles_are :admin, :guest
11
+
12
+ field :name, :type => String
13
+ end
14
+
15
+ describe "Roles for Mongoid" do
16
+
17
+ after :each do
18
+ Database.teardown
19
+ end
20
+
21
+ context "default setup" do
22
+
23
+ before do
24
+ @user = User.create(:name => 'Kristian')
25
+ @user.role = :guest
26
+ @user.save
27
+
28
+ @admin_user = User.create(:name => 'Admin user')
29
+ @admin_user.roles = :admin
30
+ @admin_user.save
31
+ end
32
+
33
+ describe '#in_role' do
34
+ it "should return first user matching role" do
35
+ User.in_role(:guest).first.name.should == 'Kristian'
36
+ User.in_role(:admin).first.name.should == 'Admin user'
37
+ end
38
+ end
39
+
40
+ describe "Role API" do
41
+ it "should have admin user role to :admin" do
42
+ @admin_user.roles_list.first.should == :admin
43
+ @admin_user.admin?.should be_true
44
+
45
+ @admin_user.has_role?(:guest).should be_false
46
+
47
+ @admin_user.has_role?(:admin).should be_true
48
+ @admin_user.is?(:admin).should be_true
49
+ @admin_user.has_roles?(:admin).should be_true
50
+ @admin_user.has?(:admin).should be_true
51
+ end
52
+
53
+ it "should have user role to :guest" do
54
+ @user.roles_list.first.should == :guest
55
+ @user.admin?.should be_false
56
+
57
+ @user.has_role?(:guest).should be_true
58
+ @user.has_role?(:admin).should be_false
59
+ @user.is?(:admin).should be_false
60
+
61
+ @user.has_roles?(:admin).should be_false
62
+ @user.has?(:admin).should be_false
63
+ end
64
+
65
+ it "should set user role to :admin using roles=" do
66
+ @user.roles = :admin
67
+ @user.roles_list.first.should == :admin
68
+ @user.has_role?(:admin).should be_true
69
+ end
70
+ end
71
+ end
72
+ end
73
+