rolify 3.3.0.rc3 → 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 +12 -11
- data/CHANGELOG.rdoc +18 -0
- data/Gemfile +20 -3
- data/README.md +84 -70
- data/Rakefile +21 -8
- data/UPGRADE.rdoc +22 -0
- data/gemfiles/Gemfile.rails-3.2 +21 -0
- data/gemfiles/Gemfile.rails-4.0 +27 -0
- data/lib/generators/mongoid/rolify_generator.rb +1 -1
- data/lib/generators/rolify/templates/README +1 -1
- data/lib/rolify/adapters/active_record/resource_adapter.rb +8 -7
- data/lib/rolify/adapters/active_record/role_adapter.rb +13 -9
- data/lib/rolify/configure.rb +1 -1
- data/lib/rolify/railtie.rb +1 -1
- data/lib/rolify/role.rb +3 -5
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +13 -10
- data/rolify.gemspec +5 -12
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/{rolify_generator_spec.rb → rolify_activerecord_generator_spec.rb} +13 -104
- data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
- data/spec/generators_helper.rb +8 -1
- data/spec/rolify/custom_spec.rb +9 -16
- data/spec/rolify/namespace_spec.rb +12 -15
- data/spec/rolify/resource_spec.rb +20 -2
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +5 -1
- 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 +43 -2
- 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_roles.rb +5 -3
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +10 -10
- data/spec/spec_helper.rb +3 -0
- data/spec/support/adapters/active_record.rb +9 -2
- data/spec/support/adapters/mongoid.rb +8 -4
- data/spec/support/data.rb +3 -0
- data/spec/support/schema.rb +6 -1
- metadata +51 -110
data/lib/rolify.rb
CHANGED
|
@@ -9,20 +9,23 @@ require 'rolify/adapters/base'
|
|
|
9
9
|
module Rolify
|
|
10
10
|
extend Configure
|
|
11
11
|
|
|
12
|
-
attr_accessor :role_cname, :adapter, :role_table_name
|
|
12
|
+
attr_accessor :role_cname, :adapter, :role_join_table_name, :role_table_name
|
|
13
13
|
|
|
14
14
|
def rolify(options = {})
|
|
15
15
|
include Role
|
|
16
16
|
extend Dynamic if Rolify.dynamic_shortcuts
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
options.reverse_merge!({:role_cname => 'Role'})
|
|
19
19
|
self.role_cname = options[:role_cname]
|
|
20
20
|
self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
|
|
23
|
+
options.reverse_merge!({:role_join_table_name => default_join_table})
|
|
24
|
+
self.role_join_table_name = options[:role_join_table_name]
|
|
25
|
+
|
|
23
26
|
rolify_options = { :class_name => options[:role_cname].camelize }
|
|
24
|
-
rolify_options.merge!({ :join_table =>
|
|
25
|
-
rolify_options.merge!(options.reject{ |k,v| ![:before_add, :after_add, :before_remove, :after_remove].include? k.to_sym })
|
|
27
|
+
rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
|
|
28
|
+
rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
|
|
26
29
|
|
|
27
30
|
has_and_belongs_to_many :roles, rolify_options
|
|
28
31
|
|
|
@@ -32,22 +35,22 @@ module Rolify
|
|
|
32
35
|
|
|
33
36
|
def resourcify(association_name = :roles, options = {})
|
|
34
37
|
include Resource
|
|
35
|
-
|
|
38
|
+
|
|
36
39
|
options.reverse_merge!({ :role_cname => 'Role', :dependent => :destroy })
|
|
37
40
|
resourcify_options = { :class_name => options[:role_cname].camelize, :as => :resource, :dependent => options[:dependent] }
|
|
38
41
|
self.role_cname = options[:role_cname]
|
|
39
42
|
self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
|
|
40
43
|
|
|
41
44
|
has_many association_name, resourcify_options
|
|
42
|
-
|
|
45
|
+
|
|
43
46
|
self.adapter = Rolify::Adapter::Base.create("resource_adapter", self.role_cname, self.name)
|
|
44
47
|
end
|
|
45
|
-
|
|
48
|
+
|
|
46
49
|
def scopify
|
|
47
50
|
require "rolify/adapters/#{Rolify.orm}/scopes.rb"
|
|
48
|
-
extend Rolify::Adapter::Scopes
|
|
51
|
+
extend Rolify::Adapter::Scopes
|
|
49
52
|
end
|
|
50
|
-
|
|
53
|
+
|
|
51
54
|
def role_class
|
|
52
55
|
self.role_cname.constantize
|
|
53
56
|
end
|
data/rolify.gemspec
CHANGED
|
@@ -15,23 +15,16 @@ Gem::Specification.new do |s|
|
|
|
15
15
|
s.rubyforge_project = s.name
|
|
16
16
|
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
|
18
|
-
s.test_files = `git ls-files --
|
|
18
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
20
|
s.require_paths = ["lib"]
|
|
21
21
|
|
|
22
|
-
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
|
23
|
-
s.add_development_dependency "activerecord-jdbcsqlite3-adapter"
|
|
24
|
-
else
|
|
25
|
-
s.add_development_dependency "sqlite3"
|
|
26
|
-
s.add_development_dependency "bson_ext" if RUBY_VERSION < "2.0.0"
|
|
27
|
-
end
|
|
28
|
-
s.add_development_dependency "activerecord", ">= 3.1.0"
|
|
29
|
-
s.add_development_dependency "mongoid", ">= 3.0"
|
|
30
22
|
s.add_development_dependency "ammeter"
|
|
31
23
|
s.add_development_dependency "rake"
|
|
32
24
|
s.add_development_dependency "rspec", ">= 2.0"
|
|
33
25
|
s.add_development_dependency "rspec-rails", ">= 2.0"
|
|
34
|
-
s.add_development_dependency "
|
|
35
|
-
s.add_development_dependency "
|
|
36
|
-
s.add_development_dependency "
|
|
26
|
+
s.add_development_dependency "bundler"
|
|
27
|
+
s.add_development_dependency "fuubar"
|
|
28
|
+
s.add_development_dependency "activerecord", ">= 3.2.0"
|
|
29
|
+
s.add_development_dependency "mongoid", ">= 3.1"
|
|
37
30
|
end
|
data/spec/README.rdoc
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
= Rolify Specs
|
|
2
|
+
|
|
3
|
+
== Running the specs
|
|
4
|
+
|
|
5
|
+
To run the specs first run the +bundle+ command to install the necessary gems and the +rake+ command to run the specs.
|
|
6
|
+
|
|
7
|
+
bundle
|
|
8
|
+
rake
|
|
9
|
+
|
|
10
|
+
== Model Adapters
|
|
11
|
+
|
|
12
|
+
Rolify currently supports 2 different ORMs: ActiveRecord and Mongoid. By default it will use Active Record but you can change this by setting the +ADAPTER+ environment variable before running the specs. You can run the +bundle+ command with this as well to ensure you have all the required gems.
|
|
13
|
+
|
|
14
|
+
ADAPTER=mongoid bundle
|
|
15
|
+
ADAPTER=mongoid rake
|
|
16
|
+
|
|
17
|
+
The different model adapters you can specify are:
|
|
18
|
+
|
|
19
|
+
* active_record (default)
|
|
20
|
+
* mongoid
|
|
21
|
+
|
|
22
|
+
You can also run the +spec_all+ rake task to run specs for each adapter.
|
|
23
|
+
|
|
24
|
+
rake spec_all
|
data/spec/generators/rolify/{rolify_generator_spec.rb → rolify_activerecord_generator_spec.rb}
RENAMED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
require 'generators_helper'
|
|
2
2
|
|
|
3
|
+
require 'rails/all'
|
|
3
4
|
# Generators are not automatically loaded by Rails
|
|
4
5
|
require 'generators/rolify/rolify_generator'
|
|
5
6
|
|
|
6
|
-
describe Rolify::Generators::RolifyGenerator 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
9
|
destination File.expand_path("../../../../tmp", __FILE__)
|
|
9
10
|
teardown :cleanup_destination_root
|
|
@@ -22,10 +23,10 @@ describe Rolify::Generators::RolifyGenerator do
|
|
|
22
23
|
before {
|
|
23
24
|
capture(:stdout) {
|
|
24
25
|
generator.create_file "app/models/user.rb" do
|
|
25
|
-
<<-RUBY
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
RUBY
|
|
26
|
+
<<-RUBY
|
|
27
|
+
class User < ActiveRecord::Base
|
|
28
|
+
end
|
|
29
|
+
RUBY
|
|
29
30
|
end
|
|
30
31
|
}
|
|
31
32
|
require File.join(destination_root, "app/models/user.rb")
|
|
@@ -114,13 +115,13 @@ RUBY
|
|
|
114
115
|
before {
|
|
115
116
|
capture(:stdout) {
|
|
116
117
|
generator.create_file "app/models/admin/user.rb" do
|
|
117
|
-
<<-RUBY
|
|
118
|
-
module Admin
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
end
|
|
123
|
-
RUBY
|
|
118
|
+
<<-RUBY
|
|
119
|
+
module Admin
|
|
120
|
+
class User < ActiveRecord::Base
|
|
121
|
+
self.table_name_prefix = 'admin_'
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
RUBY
|
|
124
125
|
end
|
|
125
126
|
}
|
|
126
127
|
require File.join(destination_root, "app/models/admin/user.rb")
|
|
@@ -159,96 +160,4 @@ RUBY
|
|
|
159
160
|
it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
|
|
160
161
|
end
|
|
161
162
|
end
|
|
162
|
-
|
|
163
|
-
describe 'specifying ORM adapter' do
|
|
164
|
-
before(:all) { arguments [ "Role", "User", "--orm=mongoid" ] }
|
|
165
|
-
|
|
166
|
-
before {
|
|
167
|
-
capture(:stdout) {
|
|
168
|
-
generator.create_file "app/models/user.rb" do
|
|
169
|
-
<<-RUBY
|
|
170
|
-
class User
|
|
171
|
-
include Mongoid::Document
|
|
172
|
-
|
|
173
|
-
field :login, :type => String
|
|
174
163
|
end
|
|
175
|
-
RUBY
|
|
176
|
-
end
|
|
177
|
-
}
|
|
178
|
-
require File.join(destination_root, "app/models/user.rb")
|
|
179
|
-
run_generator
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
describe 'config/initializers/rolify.rb' do
|
|
183
|
-
subject { file('config/initializers/rolify.rb') }
|
|
184
|
-
it { should exist }
|
|
185
|
-
it { should contain "Rolify.configure do |config|"}
|
|
186
|
-
it { should_not contain "# config.use_mongoid" }
|
|
187
|
-
it { should contain "# config.use_dynamic_shortcuts" }
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
describe 'app/models/role.rb' do
|
|
191
|
-
subject { file('app/models/role.rb') }
|
|
192
|
-
it { should exist }
|
|
193
|
-
it { should contain "class Role\n" }
|
|
194
|
-
it { should contain "has_and_belongs_to_many :users\n" }
|
|
195
|
-
it { should contain "belongs_to :resource, :polymorphic => true" }
|
|
196
|
-
it { should contain "field :name, :type => String" }
|
|
197
|
-
it { should contain " index({\n"
|
|
198
|
-
" { :name => 1 },\n"
|
|
199
|
-
" { :resource_type => 1 },\n"
|
|
200
|
-
" { :resource_id => 1 }\n"
|
|
201
|
-
" },\n"
|
|
202
|
-
" { unique => true })"}
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
describe 'app/models/user.rb' do
|
|
206
|
-
subject { file('app/models/user.rb') }
|
|
207
|
-
it { should contain /class User\n include Mongoid::Document\n rolify\n/ }
|
|
208
|
-
end
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
describe 'specifying namespaced User and Role class names and ORM adapter' do
|
|
212
|
-
before(:all) { arguments %w(Admin::Role Admin::User --orm=mongoid) }
|
|
213
|
-
|
|
214
|
-
before {
|
|
215
|
-
capture(:stdout) {
|
|
216
|
-
generator.create_file "app/models/admin/user.rb" do
|
|
217
|
-
<<-RUBY
|
|
218
|
-
module Admin
|
|
219
|
-
class User
|
|
220
|
-
include Mongoid::Document
|
|
221
|
-
end
|
|
222
|
-
end
|
|
223
|
-
RUBY
|
|
224
|
-
end
|
|
225
|
-
}
|
|
226
|
-
require File.join(destination_root, "app/models/admin/user.rb")
|
|
227
|
-
run_generator
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
describe 'config/initializers/rolify.rb' do
|
|
231
|
-
subject { file('config/initializers/rolify.rb') }
|
|
232
|
-
|
|
233
|
-
it { should exist }
|
|
234
|
-
it { should contain "Rolify.configure(\"Admin::Role\") do |config|"}
|
|
235
|
-
it { should contain "# config.use_dynamic_shortcuts" }
|
|
236
|
-
it { should_not contain "# config.use_mongoid" }
|
|
237
|
-
end
|
|
238
|
-
|
|
239
|
-
describe 'app/models/admin/role.rb' do
|
|
240
|
-
subject { file('app/models/admin/role.rb') }
|
|
241
|
-
|
|
242
|
-
it { should exist }
|
|
243
|
-
it { should contain "class Admin::Role" }
|
|
244
|
-
it { should contain "has_and_belongs_to_many :admin_users" }
|
|
245
|
-
it { should contain "belongs_to :resource, :polymorphic => true" }
|
|
246
|
-
end
|
|
247
|
-
|
|
248
|
-
describe 'app/models/admin/user.rb' do
|
|
249
|
-
subject { file('app/models/admin/user.rb') }
|
|
250
|
-
|
|
251
|
-
it { should contain /class User\n include Mongoid::Document\n rolify :role_cname => 'Admin::Role'\n/ }
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
|
-
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
|
data/spec/generators_helper.rb
CHANGED
|
@@ -5,10 +5,17 @@ require 'rolify'
|
|
|
5
5
|
require 'rolify/matchers'
|
|
6
6
|
require 'rails/all'
|
|
7
7
|
|
|
8
|
+
require 'coveralls'
|
|
9
|
+
Coveralls.wear_merged!
|
|
10
|
+
|
|
8
11
|
module TestApp
|
|
9
12
|
class Application < ::Rails::Application
|
|
10
13
|
config.root = File.dirname(__FILE__)
|
|
11
14
|
end
|
|
12
15
|
end
|
|
13
16
|
|
|
14
|
-
require 'ammeter/init'
|
|
17
|
+
require 'ammeter/init'
|
|
18
|
+
|
|
19
|
+
ENV['ADAPTER'] ||= 'active_record'
|
|
20
|
+
|
|
21
|
+
|
data/spec/rolify/custom_spec.rb
CHANGED
|
@@ -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 "Using Rolify with custom User and Role class names" do
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let(:role_class) { Privilege }
|
|
8
|
+
def user_class
|
|
9
|
+
Customer
|
|
11
10
|
end
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
@@ -5,23 +5,20 @@ require "rolify/shared_examples/shared_examples_for_scopes"
|
|
|
5
5
|
require "rolify/shared_examples/shared_examples_for_callbacks"
|
|
6
6
|
|
|
7
7
|
describe "Rolify.namespace" do
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let(:role_class) { Admin::Right }
|
|
8
|
+
def user_class
|
|
9
|
+
Admin::Moderator
|
|
11
10
|
end
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
let(:role_class) { Admin::Right }
|
|
12
|
+
def role_class
|
|
13
|
+
Admin::Right
|
|
16
14
|
end
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let(:role_class) { Admin::Right }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it_behaves_like "Rolify.callbacks" do
|
|
24
|
-
let(:user_class) { Admin::Moderator }
|
|
25
|
-
let(:role_class) { Admin::Right }
|
|
15
|
+
|
|
16
|
+
def join_table
|
|
17
|
+
"moderators_rights"
|
|
26
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"
|
|
27
24
|
end
|
|
@@ -6,12 +6,14 @@ describe Rolify::Resource do
|
|
|
6
6
|
User.rolify
|
|
7
7
|
Forum.resourcify
|
|
8
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 }
|
|
16
|
+
let(:captain) { User.where(:login => "god").first }
|
|
15
17
|
|
|
16
18
|
# roles
|
|
17
19
|
let!(:forum_role) { admin.add_role(:forum, Forum.first) }
|
|
@@ -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 }
|
|
@@ -34,9 +38,16 @@ describe Rolify::Resource do
|
|
|
34
38
|
it "should include Forum instances with forum role" do
|
|
35
39
|
subject.with_role(:forum).should =~ [ Forum.first, Forum.last ]
|
|
36
40
|
end
|
|
41
|
+
|
|
37
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
53
|
context "on the Group class" do
|
|
@@ -127,7 +138,14 @@ describe Rolify::Resource do
|
|
|
127
138
|
|
|
128
139
|
end
|
|
129
140
|
end
|
|
130
|
-
|
|
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
|
|
131
149
|
end
|
|
132
150
|
|
|
133
151
|
describe ".find_role" do
|
|
@@ -327,7 +345,7 @@ describe Rolify::Resource do
|
|
|
327
345
|
|
|
328
346
|
context "on a Forum instance" do
|
|
329
347
|
its(:roles) { should eq([ forum_role, sneaky_role ]) }
|
|
330
|
-
its(:roles) {
|
|
348
|
+
its(:roles) { should_not include(group_role, godfather_role, tourist_role) }
|
|
331
349
|
end
|
|
332
350
|
|
|
333
351
|
context "on a Group instance" do
|
|
@@ -8,7 +8,11 @@ describe "Resourcify and rolify on the same model" do
|
|
|
8
8
|
HumanResource.delete_all
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
let!(:user)
|
|
11
|
+
let!(:user) do
|
|
12
|
+
user = HumanResource.new login: 'Samer'
|
|
13
|
+
user.save
|
|
14
|
+
user
|
|
15
|
+
end
|
|
12
16
|
|
|
13
17
|
it "should add the role to the user" do
|
|
14
18
|
expect { user.add_role :admin }.to change { user.roles.count }.by(1)
|
data/spec/rolify/role_spec.rb
CHANGED
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
let(:role_class) { Role }
|
|
8
|
+
def user_class
|
|
9
|
+
User
|
|
11
10
|
end
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
@@ -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)
|