rolify 3.2.0 → 3.3.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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +13 -10
- data/CHANGELOG.rdoc +18 -0
- data/Gemfile +20 -3
- data/README.md +91 -73
- data/Rakefile +27 -2
- data/UPGRADE.rdoc +22 -0
- data/gemfiles/Gemfile.rails-3.2 +21 -0
- data/gemfiles/Gemfile.rails-4.0 +27 -0
- data/lib/generators/active_record/rolify_generator.rb +50 -0
- data/lib/generators/active_record/templates/README +8 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/mongoid/rolify_generator.rb +51 -0
- data/lib/generators/mongoid/templates/README-mongoid +4 -0
- data/lib/generators/rolify/rolify_generator.rb +35 -0
- data/lib/generators/rolify/{role/templates/README-mongoid → templates/README} +1 -5
- data/lib/generators/rolify/{role/templates → templates}/initializer.rb +2 -2
- data/lib/generators/rolify/templates/role-active_record.rb +11 -0
- data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +0 -2
- data/lib/generators/rolify/user_generator.rb +39 -0
- data/lib/rolify/adapters/active_record/resource_adapter.rb +8 -7
- data/lib/rolify/adapters/active_record/role_adapter.rb +17 -12
- data/lib/rolify/adapters/base.rb +1 -1
- data/lib/rolify/adapters/mongoid/resource_adapter.rb +2 -2
- data/lib/rolify/adapters/mongoid/role_adapter.rb +24 -14
- data/lib/rolify/configure.rb +16 -1
- data/lib/rolify/finders.rb +17 -16
- data/lib/rolify/matchers.rb +13 -0
- data/lib/rolify/railtie.rb +1 -1
- data/lib/rolify/resource.rb +7 -2
- data/lib/rolify/role.rb +5 -7
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +21 -15
- data/rolify.gemspec +5 -11
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/{role/role_generator_spec.rb → rolify_activerecord_generator_spec.rb} +49 -39
- data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
- data/spec/generators_helper.rb +21 -0
- data/spec/rolify/custom_spec.rb +10 -17
- data/spec/rolify/matchers_spec.rb +24 -0
- data/spec/rolify/namespace_spec.rb +24 -0
- data/spec/rolify/resource_spec.rb +56 -31
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
- data/spec/rolify/role_spec.rb +9 -16
- data/spec/rolify/shared_contexts.rb +9 -3
- data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +2 -2
- data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +12 -4
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +45 -4
- data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +3 -3
- data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +3 -3
- data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +59 -59
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +7 -5
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
- data/spec/spec_helper.rb +6 -2
- data/spec/support/adapters/active_record.rb +47 -9
- data/spec/support/adapters/mongoid.rb +85 -24
- data/spec/support/data.rb +15 -15
- data/spec/support/schema.rb +28 -17
- metadata +102 -83
- data/lib/generators/rolify/role/role_generator.rb +0 -49
- data/lib/generators/rolify/role/templates/README +0 -11
- data/lib/generators/rolify/role/templates/README-active_record +0 -21
- data/lib/generators/rolify/role/templates/migration.rb +0 -19
- data/lib/generators/rolify/role/templates/role-active_record.rb +0 -6
|
@@ -50,7 +50,7 @@ shared_examples_for "#add_role_examples" do |param_name, param_method|
|
|
|
50
50
|
context "should not create another role" do
|
|
51
51
|
it "if the role was already assigned to the user" do
|
|
52
52
|
subject.add_role "warrior".send(param_method), Forum
|
|
53
|
-
expect { subject.add_role "warrior".send(param_method), Forum }.not_to change { subject.roles.
|
|
53
|
+
expect { subject.add_role "warrior".send(param_method), Forum }.not_to change { subject.roles.count }
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
it "if already existing in the database" do
|
|
@@ -62,7 +62,7 @@ shared_examples_for "#add_role_examples" do |param_name, param_method|
|
|
|
62
62
|
|
|
63
63
|
context "with an instance scoped role", :scope => :instance do
|
|
64
64
|
it "should add the role to the user" do
|
|
65
|
-
expect { subject.add_role "visitor".send(param_method), Forum.last }.to change { subject.roles.
|
|
65
|
+
expect { subject.add_role "visitor".send(param_method), Forum.last }.to change { subject.roles.count }.by(1)
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
it "should create a role in the roles table" do
|
|
@@ -12,7 +12,9 @@ shared_examples_for "Rolify.callbacks" do
|
|
|
12
12
|
describe "rolify association callbacks", :if => (Rolify.orm == "active_record") do
|
|
13
13
|
describe "before_add" do
|
|
14
14
|
it "should receive callback" do
|
|
15
|
-
|
|
15
|
+
rolify_options = { :role_cname => role_class.to_s, :before_add => :role_callback }
|
|
16
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
17
|
+
user_class.rolify rolify_options
|
|
16
18
|
@user = user_class.first
|
|
17
19
|
@user.stub(:role_callback)
|
|
18
20
|
@user.should_receive(:role_callback)
|
|
@@ -22,7 +24,9 @@ shared_examples_for "Rolify.callbacks" do
|
|
|
22
24
|
|
|
23
25
|
describe "after_add" do
|
|
24
26
|
it "should receive callback" do
|
|
25
|
-
|
|
27
|
+
rolify_options = { :role_cname => role_class.to_s, :after_add => :role_callback }
|
|
28
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
29
|
+
user_class.rolify rolify_options
|
|
26
30
|
@user = user_class.first
|
|
27
31
|
@user.stub(:role_callback)
|
|
28
32
|
@user.should_receive(:role_callback)
|
|
@@ -32,7 +36,9 @@ shared_examples_for "Rolify.callbacks" do
|
|
|
32
36
|
|
|
33
37
|
describe "before_remove" do
|
|
34
38
|
it "should receive callback" do
|
|
35
|
-
|
|
39
|
+
rolify_options = { :role_cname => role_class.to_s, :before_remove => :role_callback }
|
|
40
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
41
|
+
user_class.rolify rolify_options
|
|
36
42
|
@user = user_class.first
|
|
37
43
|
@user.add_role :admin
|
|
38
44
|
@user.stub(:role_callback)
|
|
@@ -44,7 +50,9 @@ shared_examples_for "Rolify.callbacks" do
|
|
|
44
50
|
|
|
45
51
|
describe "after_remove" do
|
|
46
52
|
it "should receive callback" do
|
|
47
|
-
|
|
53
|
+
rolify_options = { :role_cname => role_class.to_s, :after_remove => :role_callback }
|
|
54
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
55
|
+
user_class.rolify rolify_options
|
|
48
56
|
@user = user_class.first
|
|
49
57
|
@user.add_role :admin
|
|
50
58
|
@user.stub(:role_callback)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
shared_examples_for Rolify::Dynamic do
|
|
1
|
+
shared_examples_for Rolify::Dynamic do
|
|
2
2
|
before(:all) do
|
|
3
3
|
Rolify.dynamic_shortcuts = true
|
|
4
4
|
role_class.destroy_all
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
rolify_options = { :role_cname => role_class.to_s }
|
|
6
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
7
|
+
user_class.rolify rolify_options
|
|
8
|
+
Forum.resourcify :roles, :role_cname => role_class.to_s
|
|
9
|
+
Group.resourcify :roles, :role_cname => role_class.to_s
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
context "using a global role" do
|
|
@@ -12,6 +14,7 @@ shared_examples_for Rolify::Dynamic do
|
|
|
12
14
|
admin = user_class.first
|
|
13
15
|
admin.add_role :admin
|
|
14
16
|
admin.add_role :moderator, Forum.first
|
|
17
|
+
admin.add_role :solo
|
|
15
18
|
admin
|
|
16
19
|
end
|
|
17
20
|
|
|
@@ -22,12 +25,22 @@ shared_examples_for Rolify::Dynamic do
|
|
|
22
25
|
it { subject.is_admin?.should be(true) }
|
|
23
26
|
it { subject.is_admin?.should be(true) }
|
|
24
27
|
it { subject.is_admin?.should be(true) }
|
|
28
|
+
|
|
29
|
+
context "removing the role on the last user having it" do
|
|
30
|
+
before do
|
|
31
|
+
subject.remove_role :solo
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it { should_not respond_to(:is_solo?) }
|
|
35
|
+
it { subject.is_solo?.should be(false) }
|
|
36
|
+
end
|
|
25
37
|
end
|
|
26
38
|
|
|
27
39
|
context "using a resource scoped role" do
|
|
28
40
|
subject do
|
|
29
41
|
moderator = user_class.where(:login => "moderator").first
|
|
30
42
|
moderator.add_role :moderator, Forum.first
|
|
43
|
+
moderator.add_role :sole_mio, Forum.last
|
|
31
44
|
moderator
|
|
32
45
|
end
|
|
33
46
|
|
|
@@ -44,12 +57,22 @@ shared_examples_for Rolify::Dynamic do
|
|
|
44
57
|
it { subject.is_moderator_of?(Group).should be(false) }
|
|
45
58
|
it { subject.is_moderator_of?(Group.first).should be(false) }
|
|
46
59
|
it { subject.is_moderator_of?(Group.last).should be(false) }
|
|
60
|
+
|
|
61
|
+
context "removing the role on the last user having it" do
|
|
62
|
+
before do
|
|
63
|
+
subject.remove_role :sole_mio, Forum.last
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it { should_not respond_to(:is_sole_mio?) }
|
|
67
|
+
it { subject.is_sole_mio?.should be(false) }
|
|
68
|
+
end
|
|
47
69
|
end
|
|
48
70
|
|
|
49
71
|
context "using a class scoped role" do
|
|
50
72
|
subject do
|
|
51
73
|
manager = user_class.where(:login => "god").first
|
|
52
74
|
manager.add_role :manager, Forum
|
|
75
|
+
manager.add_role :only_me, Forum
|
|
53
76
|
manager
|
|
54
77
|
end
|
|
55
78
|
|
|
@@ -68,6 +91,15 @@ shared_examples_for Rolify::Dynamic do
|
|
|
68
91
|
it { subject.is_manager_of?(Group).should be(false) }
|
|
69
92
|
it { subject.is_manager_of?(Group.first).should be(false) }
|
|
70
93
|
it { subject.is_manager_of?(Group.last).should be(false) }
|
|
94
|
+
|
|
95
|
+
context "removing the role on the last user having it" do
|
|
96
|
+
before do
|
|
97
|
+
subject.remove_role :only_me, Forum
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it { should_not respond_to(:is_only_me?) }
|
|
101
|
+
it { subject.is_only_me?.should be(false) }
|
|
102
|
+
end
|
|
71
103
|
end
|
|
72
104
|
|
|
73
105
|
context "if the role doesn't exist in the database" do
|
|
@@ -85,6 +117,7 @@ shared_examples_for Rolify::Dynamic do
|
|
|
85
117
|
it { should_not respond_to(:is_god?) }
|
|
86
118
|
|
|
87
119
|
it { subject.is_superman?.should be(false) }
|
|
120
|
+
it { subject.is_god?.should be(false) }
|
|
88
121
|
end
|
|
89
122
|
|
|
90
123
|
context "using a resource scope role" do
|
|
@@ -105,6 +138,14 @@ shared_examples_for Rolify::Dynamic do
|
|
|
105
138
|
it { subject.is_batman_of?(Group).should be(false) }
|
|
106
139
|
it { subject.is_batman_of?(Group.first).should be(false) }
|
|
107
140
|
it { subject.is_batman_of?(Group.last).should be(false) }
|
|
141
|
+
|
|
142
|
+
it { subject.is_god?.should be(false) }
|
|
143
|
+
it { subject.is_god_of?(Forum).should be(false) }
|
|
144
|
+
it { subject.is_god_of?(Forum.first).should be(false) }
|
|
145
|
+
it { subject.is_god_of?(Forum.last).should be(false) }
|
|
146
|
+
it { subject.is_god_of?(Group).should be(false) }
|
|
147
|
+
it { subject.is_god_of?(Group.first).should be(false) }
|
|
148
|
+
it { subject.is_god_of?(Group.last).should be(false) }
|
|
108
149
|
end
|
|
109
150
|
end
|
|
110
151
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
shared_examples_for "#has_any_role?_examples" do |param_name, param_method|
|
|
2
2
|
context "using #{param_name} as parameter" do
|
|
3
3
|
context "with a global role", :scope => :global do
|
|
4
|
-
before
|
|
4
|
+
before do
|
|
5
5
|
subject.add_role "staff".send(param_method)
|
|
6
6
|
end
|
|
7
7
|
|
|
@@ -20,7 +20,7 @@ shared_examples_for "#has_any_role?_examples" do |param_name, param_method|
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
context "with a class scoped role", :scope => :class do
|
|
23
|
-
before
|
|
23
|
+
before do
|
|
24
24
|
subject.add_role "player".send(param_method), Forum
|
|
25
25
|
subject.add_role "superhero".send(param_method)
|
|
26
26
|
end
|
|
@@ -40,7 +40,7 @@ shared_examples_for "#has_any_role?_examples" do |param_name, param_method|
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
context "with a instance scoped role", :scope => :instance do
|
|
43
|
-
before
|
|
43
|
+
before do
|
|
44
44
|
subject.add_role "visitor".send(param_method), Forum.last
|
|
45
45
|
subject.add_role "leader", Group
|
|
46
46
|
subject.add_role "warrior"
|
|
@@ -36,7 +36,7 @@ shared_examples_for "#only_has_role?_examples" do |param_name, param_method|
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
context "with multiple roles" do
|
|
39
|
-
before
|
|
39
|
+
before { subject.add_role "multiple_global_roles".send(param_method) }
|
|
40
40
|
|
|
41
41
|
it { subject.only_has_role?("global_role".send(param_method)).should be_false }
|
|
42
42
|
end
|
|
@@ -93,7 +93,7 @@ shared_examples_for "#only_has_role?_examples" do |param_name, param_method|
|
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
context "with multiple roles" do
|
|
96
|
-
before
|
|
96
|
+
before { subject.add_role "multiple_class_roles".send(param_method) }
|
|
97
97
|
|
|
98
98
|
it { subject.only_has_role?("class_role".send(param_method), Forum).should be_false }
|
|
99
99
|
it { subject.only_has_role?("class_role".send(param_method), Forum.first).should be_false }
|
|
@@ -164,7 +164,7 @@ shared_examples_for "#only_has_role?_examples" do |param_name, param_method|
|
|
|
164
164
|
end
|
|
165
165
|
|
|
166
166
|
context "with multiple roles" do
|
|
167
|
-
before
|
|
167
|
+
before { subject.add_role "multiple_instance_roles".send(param_method), Forum.first }
|
|
168
168
|
|
|
169
169
|
it { subject.only_has_role?("instance_role".send(param_method), Forum.first).should be_false }
|
|
170
170
|
it { subject.only_has_role?("instance_role".send(param_method), :any).should be_false }
|
|
@@ -3,55 +3,55 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
|
3
3
|
context "removing a global role", :scope => :global do
|
|
4
4
|
context "being a global role of the user" do
|
|
5
5
|
it { expect { subject.remove_role("admin".send(param_method)) }.to change { subject.roles.size }.by(-1) }
|
|
6
|
-
|
|
7
|
-
it {
|
|
8
|
-
it {
|
|
9
|
-
it {
|
|
10
|
-
it {
|
|
11
|
-
it {
|
|
12
|
-
it {
|
|
6
|
+
|
|
7
|
+
it { should_not have_role("admin".send(param_method)) }
|
|
8
|
+
it { should have_role("staff".send(param_method)) }
|
|
9
|
+
it { should have_role("manager".send(param_method), Group) }
|
|
10
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
|
11
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
|
12
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
context "being a class scoped role to the user" do
|
|
16
16
|
it { expect { subject.remove_role("manager".send(param_method)) }.to change { subject.roles.size }.by(-1) }
|
|
17
|
-
|
|
18
|
-
it {
|
|
19
|
-
it {
|
|
20
|
-
it {
|
|
21
|
-
it {
|
|
22
|
-
it {
|
|
23
|
-
it {
|
|
17
|
+
|
|
18
|
+
it { should_not have_role("admin".send(param_method)) }
|
|
19
|
+
it { should have_role("staff".send(param_method)) }
|
|
20
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
|
21
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
|
22
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
|
23
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
context "being instance scoped roles to the user" do
|
|
27
27
|
it { expect { subject.remove_role("moderator".send(param_method)) }.to change { subject.roles.size }.by(-2) }
|
|
28
|
-
|
|
29
|
-
it {
|
|
30
|
-
it {
|
|
31
|
-
it {
|
|
32
|
-
it {
|
|
33
|
-
it {
|
|
34
|
-
it {
|
|
28
|
+
|
|
29
|
+
it { should_not have_role("admin".send(param_method)) }
|
|
30
|
+
it { should have_role("staff".send(param_method)) }
|
|
31
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
|
32
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
|
33
|
+
it { should_not have_role("moderator".send(param_method), Group.last) }
|
|
34
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
35
35
|
end
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
context "not being a role of the user" do
|
|
38
38
|
it { expect { subject.remove_role("superhero".send(param_method)) }.not_to change { subject.roles.size } }
|
|
39
39
|
end
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
context "used by another user" do
|
|
42
42
|
before do
|
|
43
43
|
user = user_class.last
|
|
44
44
|
user.add_role "staff".send(param_method)
|
|
45
45
|
end
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
it { expect { subject.remove_role("staff".send(param_method)) }.not_to change { role_class.count } }
|
|
48
|
-
|
|
49
|
-
it {
|
|
50
|
-
it {
|
|
51
|
-
it {
|
|
52
|
-
it {
|
|
53
|
-
it {
|
|
54
|
-
it {
|
|
48
|
+
|
|
49
|
+
it { should_not have_role("admin".send(param_method)) }
|
|
50
|
+
it { should_not have_role("staff".send(param_method)) }
|
|
51
|
+
it { should_not have_role("manager".send(param_method), Group) }
|
|
52
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
|
53
|
+
it { should_not have_role("moderator".send(param_method), Group.last) }
|
|
54
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
context "not used by anyone else" do
|
|
@@ -67,29 +67,29 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
|
67
67
|
context "being a global role of the user" do
|
|
68
68
|
it { expect { subject.remove_role("warrior".send(param_method), Forum) }.not_to change{ subject.roles.size } }
|
|
69
69
|
end
|
|
70
|
-
|
|
70
|
+
|
|
71
71
|
context "being a class scoped role to the user" do
|
|
72
72
|
it { expect { subject.remove_role("manager".send(param_method), Forum) }.to change{ subject.roles.size }.by(-1) }
|
|
73
|
-
|
|
74
|
-
it {
|
|
75
|
-
it {
|
|
76
|
-
it {
|
|
77
|
-
it {
|
|
78
|
-
it {
|
|
79
|
-
it {
|
|
73
|
+
|
|
74
|
+
it { should have_role("warrior") }
|
|
75
|
+
it { should_not have_role("manager", Forum) }
|
|
76
|
+
it { should have_role("player", Forum) }
|
|
77
|
+
it { should have_role("moderator".send(param_method), Forum.last) }
|
|
78
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
|
79
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
80
80
|
end
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
context "being instance scoped role to the user" do
|
|
83
83
|
it { expect { subject.remove_role("moderator".send(param_method), Forum) }.to change { subject.roles.size }.by(-1) }
|
|
84
|
-
|
|
85
|
-
it {
|
|
86
|
-
it {
|
|
87
|
-
it {
|
|
88
|
-
it {
|
|
89
|
-
it {
|
|
90
|
-
it {
|
|
84
|
+
|
|
85
|
+
it { should have_role("warrior") }
|
|
86
|
+
it { should_not have_role("manager", Forum) }
|
|
87
|
+
it { should have_role("player", Forum) }
|
|
88
|
+
it { should_not have_role("moderator".send(param_method), Forum.last) }
|
|
89
|
+
it { should have_role("moderator".send(param_method), Group.last) }
|
|
90
|
+
it { should have_role("anonymous".send(param_method), Forum.first) }
|
|
91
91
|
end
|
|
92
|
-
|
|
92
|
+
|
|
93
93
|
context "not being a role of the user" do
|
|
94
94
|
it { expect { subject.remove_role("manager".send(param_method), Group) }.not_to change { subject.roles.size } }
|
|
95
95
|
end
|
|
@@ -99,23 +99,23 @@ shared_examples_for "#remove_role_examples" do |param_name, param_method|
|
|
|
99
99
|
context "being a global role of the user" do
|
|
100
100
|
it { expect { subject.remove_role("soldier".send(param_method), Group.first) }.not_to change { subject.roles.size } }
|
|
101
101
|
end
|
|
102
|
-
|
|
102
|
+
|
|
103
103
|
context "being a class scoped role to the user" do
|
|
104
104
|
it { expect { subject.remove_role("visitor".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
|
|
105
105
|
end
|
|
106
|
-
|
|
106
|
+
|
|
107
107
|
context "being instance scoped role to the user" do
|
|
108
108
|
it { expect { subject.remove_role("moderator".send(param_method), Forum.first) }.to change { subject.roles.size }.by(-1) }
|
|
109
|
-
|
|
110
|
-
it {
|
|
111
|
-
it {
|
|
112
|
-
it {
|
|
113
|
-
it {
|
|
109
|
+
|
|
110
|
+
it { should have_role("soldier") }
|
|
111
|
+
it { should have_role("visitor", Forum) }
|
|
112
|
+
it { should_not have_role("moderator", Forum.first) }
|
|
113
|
+
it { should have_role("anonymous", Forum.last) }
|
|
114
114
|
end
|
|
115
|
-
|
|
115
|
+
|
|
116
116
|
context "not being a role of the user" do
|
|
117
117
|
it { expect { subject.remove_role("anonymous".send(param_method), Forum.first) }.not_to change { subject.roles.size } }
|
|
118
118
|
end
|
|
119
119
|
end
|
|
120
120
|
end
|
|
121
|
-
end
|
|
121
|
+
end
|
|
@@ -11,13 +11,15 @@ shared_examples_for Rolify::Role do
|
|
|
11
11
|
before(:all) do
|
|
12
12
|
reset_defaults
|
|
13
13
|
Rolify.dynamic_shortcuts = false
|
|
14
|
-
|
|
14
|
+
rolify_options = { :role_cname => role_class.to_s }
|
|
15
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
16
|
+
user_class.rolify rolify_options
|
|
15
17
|
role_class.destroy_all
|
|
16
|
-
Forum.resourcify :role_cname => role_class.to_s
|
|
17
|
-
Group.resourcify :role_cname => role_class.to_s
|
|
18
|
+
Forum.resourcify :roles, :role_cname => role_class.to_s
|
|
19
|
+
Group.resourcify :roles, :role_cname => role_class.to_s
|
|
18
20
|
end
|
|
19
21
|
|
|
20
|
-
context "in the Instance level
|
|
22
|
+
context "in the Instance level" do
|
|
21
23
|
before(:all) do
|
|
22
24
|
admin = user_class.first
|
|
23
25
|
admin.add_role :admin
|
|
@@ -82,7 +84,7 @@ shared_examples_for Rolify::Role do
|
|
|
82
84
|
context "with a new instance" do
|
|
83
85
|
let(:user) { user_class.new }
|
|
84
86
|
|
|
85
|
-
before
|
|
87
|
+
before do
|
|
86
88
|
user.add_role :admin
|
|
87
89
|
user.add_role :moderator, Forum.first
|
|
88
90
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require "rolify/shared_contexts"
|
|
2
2
|
|
|
3
|
-
shared_examples_for "Role.scopes" do
|
|
4
|
-
before
|
|
3
|
+
shared_examples_for "Role.scopes" do
|
|
4
|
+
before do
|
|
5
5
|
role_class.destroy_all
|
|
6
6
|
end
|
|
7
7
|
|
|
@@ -18,9 +18,9 @@ shared_examples_for "Role.scopes" do |param_name, param_method|
|
|
|
18
18
|
let!(:manager_role) { subject.add_role :manager, Group }
|
|
19
19
|
let!(:moderator_role) { subject.add_role :moderator, Forum }
|
|
20
20
|
|
|
21
|
-
it { subject.roles.class_scoped.should
|
|
22
|
-
it { subject.roles.class_scoped(Group).should
|
|
23
|
-
it { subject.roles.class_scoped(Forum).should
|
|
21
|
+
it { subject.roles.class_scoped.should =~ [ manager_role, moderator_role ] }
|
|
22
|
+
it { subject.roles.class_scoped(Group).should =~ [ manager_role ] }
|
|
23
|
+
it { subject.roles.class_scoped(Forum).should =~ [ moderator_role ] }
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
describe ".instance_scoped" do
|
|
@@ -28,11 +28,11 @@ shared_examples_for "Role.scopes" do |param_name, param_method|
|
|
|
28
28
|
let!(:zombie_role) { subject.add_role :visitor, Forum.last }
|
|
29
29
|
let!(:anonymous_role) { subject.add_role :anonymous, Group.last }
|
|
30
30
|
|
|
31
|
-
it { subject.roles.instance_scoped.should
|
|
32
|
-
it { subject.roles.instance_scoped(Forum).should
|
|
33
|
-
it { subject.roles.instance_scoped(Forum.first).should
|
|
34
|
-
it { subject.roles.instance_scoped(Forum.last).should
|
|
35
|
-
it { subject.roles.instance_scoped(Group.last).should
|
|
31
|
+
it { subject.roles.instance_scoped.to_a.entries.should =~ [ visitor_role, zombie_role, anonymous_role ] }
|
|
32
|
+
it { subject.roles.instance_scoped(Forum).should =~ [ visitor_role, zombie_role ] }
|
|
33
|
+
it { subject.roles.instance_scoped(Forum.first).should =~ [ visitor_role ] }
|
|
34
|
+
it { subject.roles.instance_scoped(Forum.last).should =~ [ zombie_role ] }
|
|
35
|
+
it { subject.roles.instance_scoped(Group.last).should =~ [ anonymous_role ] }
|
|
36
36
|
it { subject.roles.instance_scoped(Group.first).should be_empty }
|
|
37
37
|
end
|
|
38
38
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -2,7 +2,11 @@ require 'rubygems'
|
|
|
2
2
|
require "bundler/setup"
|
|
3
3
|
|
|
4
4
|
require 'rolify'
|
|
5
|
-
require '
|
|
5
|
+
require 'rolify/matchers'
|
|
6
|
+
require 'rails/all'
|
|
7
|
+
|
|
8
|
+
require 'coveralls'
|
|
9
|
+
Coveralls.wear_merged!
|
|
6
10
|
|
|
7
11
|
ENV['ADAPTER'] ||= 'active_record'
|
|
8
12
|
|
|
@@ -23,4 +27,4 @@ def provision_user(user, roles)
|
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
user
|
|
26
|
-
end
|
|
30
|
+
end
|
|
@@ -6,7 +6,7 @@ ActiveRecord::Base.extend Rolify
|
|
|
6
6
|
|
|
7
7
|
load File.dirname(__FILE__) + '/../schema.rb'
|
|
8
8
|
|
|
9
|
-
#
|
|
9
|
+
# Standard user and role classes
|
|
10
10
|
class User < ActiveRecord::Base
|
|
11
11
|
rolify
|
|
12
12
|
end
|
|
@@ -14,18 +14,17 @@ end
|
|
|
14
14
|
class Role < ActiveRecord::Base
|
|
15
15
|
has_and_belongs_to_many :users, :join_table => :users_roles
|
|
16
16
|
belongs_to :resource, :polymorphic => true
|
|
17
|
-
|
|
18
|
-
extend Rolify::Adapter::Scopes
|
|
19
|
-
end
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
#resourcify done during specs setup to be able to use custom user classes
|
|
18
|
+
extend Rolify::Adapter::Scopes
|
|
23
19
|
end
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
# Resourcifed and rolifed at the same time
|
|
22
|
+
class HumanResource < ActiveRecord::Base
|
|
23
|
+
resourcify :resources
|
|
24
|
+
rolify
|
|
27
25
|
end
|
|
28
26
|
|
|
27
|
+
# Custom role and class names
|
|
29
28
|
class Customer < ActiveRecord::Base
|
|
30
29
|
rolify :role_cname => "Privilege"
|
|
31
30
|
end
|
|
@@ -33,6 +32,45 @@ end
|
|
|
33
32
|
class Privilege < ActiveRecord::Base
|
|
34
33
|
has_and_belongs_to_many :customers, :join_table => :customers_privileges
|
|
35
34
|
belongs_to :resource, :polymorphic => true
|
|
36
|
-
|
|
35
|
+
|
|
37
36
|
extend Rolify::Adapter::Scopes
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Namespaced models
|
|
40
|
+
module Admin
|
|
41
|
+
def self.table_name_prefix
|
|
42
|
+
'admin_'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class Moderator < ActiveRecord::Base
|
|
46
|
+
rolify :role_cname => "Admin::Right", :role_join_table_name => "moderators_rights"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class Right < ActiveRecord::Base
|
|
50
|
+
has_and_belongs_to_many :moderators, :class_name => "Admin::Moderator", :join_table => "moderators_rights"
|
|
51
|
+
belongs_to :resource, :polymorphic => true
|
|
52
|
+
|
|
53
|
+
extend Rolify::Adapter::Scopes
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# Resources classes
|
|
59
|
+
class Forum < ActiveRecord::Base
|
|
60
|
+
#resourcify done during specs setup to be able to use custom user classes
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
class Group < ActiveRecord::Base
|
|
64
|
+
#resourcify done during specs setup to be able to use custom user classes
|
|
65
|
+
|
|
66
|
+
def subgroups
|
|
67
|
+
Group.where(:parent_id => id)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class Team < ActiveRecord::Base
|
|
72
|
+
#resourcify done during specs setup to be able to use custom user classes
|
|
73
|
+
self.primary_key = "team_code"
|
|
74
|
+
|
|
75
|
+
default_scope { order(:team_code) }
|
|
38
76
|
end
|