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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +13 -10
  4. data/CHANGELOG.rdoc +18 -0
  5. data/Gemfile +20 -3
  6. data/README.md +91 -73
  7. data/Rakefile +27 -2
  8. data/UPGRADE.rdoc +22 -0
  9. data/gemfiles/Gemfile.rails-3.2 +21 -0
  10. data/gemfiles/Gemfile.rails-4.0 +27 -0
  11. data/lib/generators/active_record/rolify_generator.rb +50 -0
  12. data/lib/generators/active_record/templates/README +8 -0
  13. data/lib/generators/active_record/templates/migration.rb +19 -0
  14. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  15. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  16. data/lib/generators/rolify/rolify_generator.rb +35 -0
  17. data/lib/generators/rolify/{role/templates/README-mongoid → templates/README} +1 -5
  18. data/lib/generators/rolify/{role/templates → templates}/initializer.rb +2 -2
  19. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  20. data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +0 -2
  21. data/lib/generators/rolify/user_generator.rb +39 -0
  22. data/lib/rolify/adapters/active_record/resource_adapter.rb +8 -7
  23. data/lib/rolify/adapters/active_record/role_adapter.rb +17 -12
  24. data/lib/rolify/adapters/base.rb +1 -1
  25. data/lib/rolify/adapters/mongoid/resource_adapter.rb +2 -2
  26. data/lib/rolify/adapters/mongoid/role_adapter.rb +24 -14
  27. data/lib/rolify/configure.rb +16 -1
  28. data/lib/rolify/finders.rb +17 -16
  29. data/lib/rolify/matchers.rb +13 -0
  30. data/lib/rolify/railtie.rb +1 -1
  31. data/lib/rolify/resource.rb +7 -2
  32. data/lib/rolify/role.rb +5 -7
  33. data/lib/rolify/version.rb +1 -1
  34. data/lib/rolify.rb +21 -15
  35. data/rolify.gemspec +5 -11
  36. data/spec/README.rdoc +24 -0
  37. data/spec/generators/rolify/{role/role_generator_spec.rb → rolify_activerecord_generator_spec.rb} +49 -39
  38. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  39. data/spec/generators_helper.rb +21 -0
  40. data/spec/rolify/custom_spec.rb +10 -17
  41. data/spec/rolify/matchers_spec.rb +24 -0
  42. data/spec/rolify/namespace_spec.rb +24 -0
  43. data/spec/rolify/resource_spec.rb +56 -31
  44. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  45. data/spec/rolify/role_spec.rb +9 -16
  46. data/spec/rolify/shared_contexts.rb +9 -3
  47. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +2 -2
  48. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +12 -4
  49. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +45 -4
  50. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +3 -3
  51. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +3 -3
  52. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +59 -59
  53. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +7 -5
  54. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
  55. data/spec/spec_helper.rb +6 -2
  56. data/spec/support/adapters/active_record.rb +47 -9
  57. data/spec/support/adapters/mongoid.rb +85 -24
  58. data/spec/support/data.rb +15 -15
  59. data/spec/support/schema.rb +28 -17
  60. metadata +102 -83
  61. data/lib/generators/rolify/role/role_generator.rb +0 -49
  62. data/lib/generators/rolify/role/templates/README +0 -11
  63. data/lib/generators/rolify/role/templates/README-active_record +0 -21
  64. data/lib/generators/rolify/role/templates/migration.rb +0 -19
  65. data/lib/generators/rolify/role/templates/role-active_record.rb +0 -6
@@ -1,11 +1,12 @@
1
- require 'spec_helper'
1
+ require 'generators_helper'
2
2
 
3
+ require 'rails/all'
3
4
  # Generators are not automatically loaded by Rails
4
- require 'generators/rolify/role/role_generator'
5
+ require 'generators/rolify/rolify_generator'
5
6
 
6
- describe Rolify::Generators::RoleGenerator do
7
+ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'active_record' do
7
8
  # Tell the generator where to put its output (what it thinks of as Rails.root)
8
- destination File.expand_path("../../../../../tmp", __FILE__)
9
+ destination File.expand_path("../../../../tmp", __FILE__)
9
10
  teardown :cleanup_destination_root
10
11
 
11
12
  before {
@@ -16,15 +17,19 @@ describe Rolify::Generators::RoleGenerator do
16
17
  FileUtils.rm_rf destination_root
17
18
  end
18
19
 
19
- describe 'no arguments' do
20
- before(:all) { arguments [] }
20
+ describe 'specifying only Role class name' do
21
+ before(:all) { arguments %w(Role) }
21
22
 
22
23
  before {
23
24
  capture(:stdout) {
24
25
  generator.create_file "app/models/user.rb" do
25
- "class User < ActiveRecord::Base\nend"
26
+ <<-RUBY
27
+ class User < ActiveRecord::Base
28
+ end
29
+ RUBY
26
30
  end
27
31
  }
32
+ require File.join(destination_root, "app/models/user.rb")
28
33
  run_generator
29
34
  }
30
35
 
@@ -58,7 +63,7 @@ describe Rolify::Generators::RoleGenerator do
58
63
  end
59
64
  end
60
65
 
61
- describe 'specifying user and role names' do
66
+ describe 'specifying User and Role class names' do
62
67
  before(:all) { arguments %w(AdminRole AdminUser) }
63
68
 
64
69
  before {
@@ -67,6 +72,7 @@ describe Rolify::Generators::RoleGenerator do
67
72
  "class AdminUser < ActiveRecord::Base\nend"
68
73
  end
69
74
  }
75
+ require File.join(destination_root, "app/models/admin_user.rb")
70
76
  run_generator
71
77
  }
72
78
 
@@ -74,12 +80,12 @@ describe Rolify::Generators::RoleGenerator do
74
80
  subject { file('config/initializers/rolify.rb') }
75
81
 
76
82
  it { should exist }
77
- it { should contain "Rolify.configure do |config|"}
83
+ it { should contain "Rolify.configure(\"AdminRole\") do |config|"}
78
84
  it { should contain "# config.use_dynamic_shortcuts" }
79
85
  it { should contain "# config.use_mongoid" }
80
86
  end
81
87
 
82
- describe 'app/models/rank.rb' do
88
+ describe 'app/models/admin_role.rb' do
83
89
  subject { file('app/models/admin_role.rb') }
84
90
 
85
91
  it { should exist }
@@ -103,51 +109,55 @@ describe Rolify::Generators::RoleGenerator do
103
109
  end
104
110
  end
105
111
 
106
- describe 'specifying orm adapter' do
107
- before(:all) { arguments [ "Role", "User", "mongoid" ] }
112
+ describe 'specifying namespaced User and Role class names' do
113
+ before(:all) { arguments %w(Admin::Role Admin::User) }
108
114
 
109
115
  before {
110
116
  capture(:stdout) {
111
- generator.create_file "app/models/user.rb" do
112
- <<-CLASS
113
- class User
114
- include Mongoid::Document
115
-
116
- field :login, :type => String
117
- end
118
- CLASS
117
+ generator.create_file "app/models/admin/user.rb" do
118
+ <<-RUBY
119
+ module Admin
120
+ class User < ActiveRecord::Base
121
+ self.table_name_prefix = 'admin_'
122
+ end
123
+ end
124
+ RUBY
119
125
  end
120
126
  }
127
+ require File.join(destination_root, "app/models/admin/user.rb")
121
128
  run_generator
122
129
  }
123
-
130
+
124
131
  describe 'config/initializers/rolify.rb' do
125
132
  subject { file('config/initializers/rolify.rb') }
133
+
126
134
  it { should exist }
127
- it { should contain "Rolify.configure do |config|"}
128
- it { should_not contain "# config.use_mongoid" }
135
+ it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
129
136
  it { should contain "# config.use_dynamic_shortcuts" }
137
+ it { should contain "# config.use_mongoid" }
130
138
  end
131
139
 
132
- describe 'app/models/role.rb' do
133
- subject { file('app/models/role.rb') }
140
+ describe 'app/models/admin/role.rb' do
141
+ subject { file('app/models/admin/role.rb') }
142
+
134
143
  it { should exist }
135
- it { should contain "class Role\n" }
136
- it { should contain "has_and_belongs_to_many :users\n" }
144
+ it { should contain "class Admin::Role < ActiveRecord::Base" }
145
+ it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
137
146
  it { should contain "belongs_to :resource, :polymorphic => true" }
138
- it { should contain "field :name, :type => String" }
139
- it { should contain "index({ :name => 1 }, { :unique => true })" }
140
- it { should contain " index({\n"
141
- " { :name => 1 },\n"
142
- " { :resource_type => 1 },\n"
143
- " { :resource_id => 1 }\n"
144
- " },\n"
145
- " { unique => true })"}
146
147
  end
147
148
 
148
- describe 'app/models/user.rb' do
149
- subject { file('app/models/user.rb') }
150
- it { should contain /class User\n include Mongoid::Document\n rolify\n/ }
149
+ describe 'app/models/admin/user.rb' do
150
+ subject { file('app/models/admin/user.rb') }
151
+
152
+ it { should contain /class User < ActiveRecord::Base\n rolify :role_cname => 'Admin::Role'\n/ }
153
+ end
154
+
155
+ describe 'migration file' do
156
+ subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
157
+
158
+ it { should be_a_migration }
159
+ it { should contain "create_table(:admin_roles)" }
160
+ it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
151
161
  end
152
162
  end
153
- end
163
+ end
@@ -0,0 +1,112 @@
1
+ require 'generators_helper'
2
+
3
+ require 'mongoid'
4
+
5
+ # Generators are not automatically loaded by Rails
6
+ require 'generators/rolify/rolify_generator'
7
+
8
+ describe Rolify::Generators::RolifyGenerator, :if => ENV['ADAPTER'] == 'mongoid' do
9
+ # Tell the generator where to put its output (what it thinks of as Rails.root)
10
+ destination File.expand_path("../../../../tmp", __FILE__)
11
+ teardown :cleanup_destination_root
12
+
13
+ before {
14
+ prepare_destination
15
+ }
16
+
17
+ def cleanup_destination_root
18
+ FileUtils.rm_rf destination_root
19
+ end
20
+
21
+ describe 'specifying ORM adapter' do
22
+ before(:all) { arguments [ "Role", "User", "--orm=mongoid" ] }
23
+
24
+ before {
25
+ capture(:stdout) {
26
+ generator.create_file "app/models/user.rb" do
27
+ <<-RUBY
28
+ class User
29
+ include Mongoid::Document
30
+
31
+ field :login, :type => String
32
+ end
33
+ RUBY
34
+ end
35
+ }
36
+ require File.join(destination_root, "app/models/user.rb")
37
+ run_generator
38
+ }
39
+
40
+ describe 'config/initializers/rolify.rb' do
41
+ subject { file('config/initializers/rolify.rb') }
42
+ it { should exist }
43
+ it { should contain "Rolify.configure do |config|"}
44
+ it { should_not contain "# config.use_mongoid" }
45
+ it { should contain "# config.use_dynamic_shortcuts" }
46
+ end
47
+
48
+ describe 'app/models/role.rb' do
49
+ subject { file('app/models/role.rb') }
50
+ it { should exist }
51
+ it { should contain "class Role\n" }
52
+ it { should contain "has_and_belongs_to_many :users\n" }
53
+ it { should contain "belongs_to :resource, :polymorphic => true" }
54
+ it { should contain "field :name, :type => String" }
55
+ it { should contain " index({\n"
56
+ " { :name => 1 },\n"
57
+ " { :resource_type => 1 },\n"
58
+ " { :resource_id => 1 }\n"
59
+ " },\n"
60
+ " { unique => true })"}
61
+ end
62
+
63
+ describe 'app/models/user.rb' do
64
+ subject { file('app/models/user.rb') }
65
+ it { should contain /class User\n include Mongoid::Document\n rolify\n/ }
66
+ end
67
+ end
68
+
69
+ describe 'specifying namespaced User and Role class names and ORM adapter' do
70
+ before(:all) { arguments %w(Admin::Role Admin::User --orm=mongoid) }
71
+
72
+ before {
73
+ capture(:stdout) {
74
+ generator.create_file "app/models/admin/user.rb" do
75
+ <<-RUBY
76
+ module Admin
77
+ class User
78
+ include Mongoid::Document
79
+ end
80
+ end
81
+ RUBY
82
+ end
83
+ }
84
+ require File.join(destination_root, "app/models/admin/user.rb")
85
+ run_generator
86
+ }
87
+
88
+ describe 'config/initializers/rolify.rb' do
89
+ subject { file('config/initializers/rolify.rb') }
90
+
91
+ it { should exist }
92
+ it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
93
+ it { should contain "# config.use_dynamic_shortcuts" }
94
+ it { should_not contain "# config.use_mongoid" }
95
+ end
96
+
97
+ describe 'app/models/admin/role.rb' do
98
+ subject { file('app/models/admin/role.rb') }
99
+
100
+ it { should exist }
101
+ it { should contain "class Admin::Role" }
102
+ it { should contain "has_and_belongs_to_many :admin_users" }
103
+ it { should contain "belongs_to :resource, :polymorphic => true" }
104
+ end
105
+
106
+ describe 'app/models/admin/user.rb' do
107
+ subject { file('app/models/admin/user.rb') }
108
+
109
+ it { should contain /class User\n include Mongoid::Document\n rolify :role_cname => 'Admin::Role'\n/ }
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ require 'rolify'
5
+ require 'rolify/matchers'
6
+ require 'rails/all'
7
+
8
+ require 'coveralls'
9
+ Coveralls.wear_merged!
10
+
11
+ module TestApp
12
+ class Application < ::Rails::Application
13
+ config.root = File.dirname(__FILE__)
14
+ end
15
+ end
16
+
17
+ require 'ammeter/init'
18
+
19
+ ENV['ADAPTER'] ||= 'active_record'
20
+
21
+
@@ -2,26 +2,19 @@ require "spec_helper"
2
2
  require "rolify/shared_examples/shared_examples_for_roles"
3
3
  require "rolify/shared_examples/shared_examples_for_dynamic"
4
4
  require "rolify/shared_examples/shared_examples_for_scopes"
5
- require "rolify/shared_examples/shared_examples_for_callbacks"
5
+ require "rolify/shared_examples/shared_examples_for_callbacks"
6
6
 
7
7
  describe "Using Rolify with custom User and Role class names" do
8
- it_behaves_like Rolify::Role do
9
- let(:user_class) { Customer }
10
- let(:role_class) { Privilege }
8
+ def user_class
9
+ Customer
11
10
  end
12
11
 
13
- it_behaves_like "Role.scopes" do
14
- let(:user_class) { Customer }
15
- let(:role_class) { Privilege }
16
- end
17
-
18
- it_behaves_like Rolify::Dynamic do
19
- let(:user_class) { Customer }
20
- let(:role_class) { Privilege }
21
- end
22
-
23
- it_behaves_like "Rolify.callbacks" do
24
- let(:user_class) { Customer }
25
- let(:role_class) { Privilege }
12
+ def role_class
13
+ Privilege
26
14
  end
15
+
16
+ it_behaves_like Rolify::Role
17
+ it_behaves_like "Role.scopes"
18
+ it_behaves_like Rolify::Dynamic
19
+ it_behaves_like "Rolify.callbacks"
27
20
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'have_role', focus: true do
4
+ let(:object) { Object.new }
5
+
6
+ it 'delegates to has_role?' do
7
+ object.should_receive(:has_role?).with(:read, 'Resource') { true }
8
+ object.should have_role(:read, 'Resource')
9
+ end
10
+
11
+ it 'reports a nice failure message for should' do
12
+ object.should_receive(:has_role?) { false }
13
+ expect{
14
+ object.should have_role(:read, 'Resource')
15
+ }.to raise_error('expected to have role :read "Resource"')
16
+ end
17
+
18
+ it 'reports a nice failure message for should_not' do
19
+ object.should_receive(:has_role?) { true }
20
+ expect{
21
+ object.should_not have_role(:read, 'Resource')
22
+ }.to raise_error('expected not to have role :read "Resource"')
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ require "rolify/shared_examples/shared_examples_for_roles"
3
+ require "rolify/shared_examples/shared_examples_for_dynamic"
4
+ require "rolify/shared_examples/shared_examples_for_scopes"
5
+ require "rolify/shared_examples/shared_examples_for_callbacks"
6
+
7
+ describe "Rolify.namespace" do
8
+ def user_class
9
+ Admin::Moderator
10
+ end
11
+
12
+ def role_class
13
+ Admin::Right
14
+ end
15
+
16
+ def join_table
17
+ "moderators_rights"
18
+ end
19
+
20
+ it_behaves_like Rolify::Role
21
+ it_behaves_like "Role.scopes"
22
+ it_behaves_like Rolify::Dynamic
23
+ it_behaves_like "Rolify.callbacks"
24
+ end
@@ -3,16 +3,18 @@ require "spec_helper"
3
3
  describe Rolify::Resource do
4
4
  before(:all) do
5
5
  reset_defaults
6
- User.rolify :role_cname => "Role"
7
- Forum.resourcify :role_cname => "Role"
8
- Group.resourcify :role_cname => "Role"
6
+ User.rolify
7
+ Forum.resourcify
8
+ Group.resourcify
9
+ Team.resourcify
9
10
  Role.destroy_all
10
11
  end
11
12
 
12
13
  # Users
13
14
  let(:admin) { User.first }
14
15
  let(:tourist) { User.last }
15
-
16
+ let(:captain) { User.where(:login => "god").first }
17
+
16
18
  # roles
17
19
  let!(:forum_role) { admin.add_role(:forum, Forum.first) }
18
20
  let!(:godfather_role) { admin.add_role(:godfather, Forum) }
@@ -20,6 +22,8 @@ describe Rolify::Resource do
20
22
  let!(:grouper_role) { admin.add_role(:grouper, Group.first) }
21
23
  let!(:tourist_role) { tourist.add_role(:forum, Forum.last) }
22
24
  let!(:sneaky_role) { tourist.add_role(:group, Forum.first) }
25
+ let!(:captain_role) { captain.add_role(:captain, Team.first) }
26
+ let!(:player_role) { captain.add_role(:player, Team.last) }
23
27
 
24
28
  describe ".with_roles" do
25
29
  subject { Group }
@@ -27,19 +31,26 @@ describe Rolify::Resource do
27
31
  it { should respond_to(:find_roles).with(1).arguments }
28
32
  it { should respond_to(:find_roles).with(2).arguments }
29
33
 
30
- context "with a role name as argument" do
31
- context "on the Forum class" do
34
+ context "with a role name as argument" do
35
+ context "on the Forum class" do
32
36
  subject { Forum }
33
37
 
34
- it "should include Forum instances with forum role" do
38
+ it "should include Forum instances with forum role" do
35
39
  subject.with_role(:forum).should =~ [ Forum.first, Forum.last ]
36
40
  end
37
- it "should include Forum instances with godfather role" do
41
+
42
+ it "should include Forum instances with godfather role" do
38
43
  subject.with_role(:godfather).should =~ Forum.all.to_a
39
44
  end
45
+
46
+ it "should be able to modify the resource", :if => ENV['ADAPTER'] == 'active_record' do
47
+ forum_resource = subject.with_role(:forum).first
48
+ forum_resource.name = "modified name"
49
+ expect(forum_resource.save).not_to raise_error
50
+ end
40
51
  end
41
52
 
42
- context "on the Group class" do
53
+ context "on the Group class" do
43
54
  subject { Group }
44
55
 
45
56
  it "should include Group instances with group role" do
@@ -47,20 +58,27 @@ describe Rolify::Resource do
47
58
  end
48
59
  end
49
60
 
61
+ context "on a Group instance" do
62
+ subject { Group.last }
63
+
64
+ it "should ignore nil entries" do
65
+ subject.subgroups.with_role(:group).should =~ [ ]
66
+ end
67
+ end
50
68
  end
51
69
 
52
- context "with an array of role names as argument" do
53
- context "on the Group class" do
70
+ context "with an array of role names as argument" do
71
+ context "on the Group class" do
54
72
  subject { Group }
55
-
56
- it "should include Group instances with both group and grouper roles" do
73
+
74
+ it "should include Group instances with both group and grouper roles" do
57
75
  subject.with_roles([:group, :grouper]).should =~ [ Group.first, Group.last ]
58
76
  end
59
77
  end
60
78
  end
61
79
 
62
80
  context "with a role name and a user as arguments" do
63
- context "on the Forum class" do
81
+ context "on the Forum class" do
64
82
  subject { Forum }
65
83
 
66
84
  it "should get all Forum instances binded to the forum role and the admin user" do
@@ -88,7 +106,7 @@ describe Rolify::Resource do
88
106
  end
89
107
  end
90
108
 
91
- context "on the Group class" do
109
+ context "on the Group class" do
92
110
  subject { Group }
93
111
 
94
112
  it "should get all resources binded to the group role and the admin user" do
@@ -102,7 +120,7 @@ describe Rolify::Resource do
102
120
  end
103
121
 
104
122
  context "with an array of role names and a user as arguments" do
105
- context "on the Forum class" do
123
+ context "on the Forum class" do
106
124
  subject { Forum }
107
125
 
108
126
  it "should get Forum instances binded to the forum and group roles and the tourist user" do
@@ -111,7 +129,7 @@ describe Rolify::Resource do
111
129
 
112
130
  end
113
131
 
114
- context "on the Group class" do
132
+ context "on the Group class" do
115
133
  subject { Group }
116
134
 
117
135
  it "should get Group instances binded to the group and grouper roles and the admin user" do
@@ -120,13 +138,20 @@ describe Rolify::Resource do
120
138
 
121
139
  end
122
140
  end
123
-
141
+
142
+ context "with a model not having ID column" do
143
+ subject { Team }
144
+
145
+ it "should find Team instance using team_code column" do
146
+ subject.with_roles([:captain, :player], captain).should =~ [ Team.first, Team.last ]
147
+ end
148
+ end
124
149
  end
125
150
 
126
151
  describe ".find_role" do
127
152
 
128
- context "without using a role name parameter" do
129
-
153
+ context "without using a role name parameter" do
154
+
130
155
  context "on the Forum class" do
131
156
  subject { Forum }
132
157
 
@@ -151,7 +176,7 @@ describe Rolify::Resource do
151
176
 
152
177
  context "on the Group class" do
153
178
  subject { Group }
154
-
179
+
155
180
  it "should get all roles binded to a Group class or instance" do
156
181
  subject.find_roles.to_a.should =~ [ group_role, grouper_role ]
157
182
  end
@@ -172,7 +197,7 @@ describe Rolify::Resource do
172
197
  end
173
198
  end
174
199
 
175
- context "using a role name parameter" do
200
+ context "using a role name parameter" do
176
201
  context "on the Forum class" do
177
202
  subject { Forum }
178
203
 
@@ -209,7 +234,7 @@ describe Rolify::Resource do
209
234
 
210
235
  context "on the Group class" do
211
236
  subject { Group }
212
-
237
+
213
238
  context "without using a user parameter" do
214
239
  it "should get all roles binded to a Group class or instance and group role name" do
215
240
  subject.find_roles(:group).should include(group_role)
@@ -242,7 +267,7 @@ describe Rolify::Resource do
242
267
  end
243
268
  end
244
269
 
245
- context "using :any as role name parameter" do
270
+ context "using :any as role name parameter" do
246
271
  context "on the Forum class" do
247
272
  subject { Forum }
248
273
 
@@ -318,27 +343,27 @@ describe Rolify::Resource do
318
343
 
319
344
  it { should respond_to :roles }
320
345
 
321
- context "on a Forum instance" do
346
+ context "on a Forum instance" do
322
347
  its(:roles) { should eq([ forum_role, sneaky_role ]) }
323
- its(:roles) { subject.should_not include(group_role, godfather_role, tourist_role) }
348
+ its(:roles) { should_not include(group_role, godfather_role, tourist_role) }
324
349
  end
325
350
 
326
351
  context "on a Group instance" do
327
352
  subject { Group.last }
328
353
 
329
- its(:roles) { should eq([ group_role ]) }
354
+ its(:roles) { should eq([ group_role ]) }
330
355
  its(:roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
331
-
356
+
332
357
  context "when deleting a Group instance" do
333
- subject do
358
+ subject do
334
359
  Group.create(:name => "to delete")
335
360
  end
336
-
361
+
337
362
  before do
338
363
  subject.roles.create :name => "group_role1"
339
364
  subject.roles.create :name => "group_role2"
340
365
  end
341
-
366
+
342
367
  it "should remove the roles binded to this instance" do
343
368
  expect { subject.destroy }.to change { Role.count }.by(-2)
344
369
  end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe "Resourcify and rolify on the same model" do
4
+
5
+ before(:all) do
6
+ reset_defaults
7
+ Role.delete_all
8
+ HumanResource.delete_all
9
+ end
10
+
11
+ let!(:user) do
12
+ user = HumanResource.new login: 'Samer'
13
+ user.save
14
+ user
15
+ end
16
+
17
+ it "should add the role to the user" do
18
+ expect { user.add_role :admin }.to change { user.roles.count }.by(1)
19
+ end
20
+
21
+ it "should create a role to the roles collection" do
22
+ expect { user.add_role :moderator }.to change { Role.count }.by(1)
23
+ end
24
+ end
@@ -5,23 +5,16 @@ require "rolify/shared_examples/shared_examples_for_scopes"
5
5
  require "rolify/shared_examples/shared_examples_for_callbacks"
6
6
 
7
7
  describe Rolify do
8
- it_behaves_like Rolify::Role do
9
- let(:user_class) { User }
10
- let(:role_class) { Role }
8
+ def user_class
9
+ User
11
10
  end
12
11
 
13
- it_behaves_like "Role.scopes" do
14
- let(:user_class) { User }
15
- let(:role_class) { Role }
16
- end
17
-
18
- it_behaves_like Rolify::Dynamic do
19
- let(:user_class) { User }
20
- let(:role_class) { Role }
21
- end
22
-
23
- it_behaves_like "Rolify.callbacks" do
24
- let(:user_class) { User }
25
- let(:role_class) { Role }
12
+ def role_class
13
+ Role
26
14
  end
15
+
16
+ it_behaves_like Rolify::Role
17
+ it_behaves_like "Role.scopes"
18
+ it_behaves_like Rolify::Dynamic
19
+ it_behaves_like "Rolify.callbacks"
27
20
  end
@@ -1,7 +1,9 @@
1
1
  shared_context "global role", :scope => :global do
2
2
  subject { admin }
3
3
 
4
- let(:admin) { user_class.first }
4
+ def admin
5
+ user_class.first
6
+ end
5
7
 
6
8
  before(:all) do
7
9
  load_roles
@@ -29,7 +31,9 @@ shared_context "class scoped role", :scope => :class do
29
31
  create_other_roles
30
32
  end
31
33
 
32
- let(:manager) { user_class.where(:login => "moderator").first }
34
+ def manager
35
+ user_class.where(:login => "moderator").first
36
+ end
33
37
 
34
38
  def load_roles
35
39
  role_class.destroy_all
@@ -51,7 +55,9 @@ shared_context "instance scoped role", :scope => :instance do
51
55
  create_other_roles
52
56
  end
53
57
 
54
- let(:moderator) { user_class.where(:login => "god").first }
58
+ def moderator
59
+ user_class.where(:login => "god").first
60
+ end
55
61
 
56
62
  def load_roles
57
63
  role_class.destroy_all