roles_data_mapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rspec +1 -0
- data/LICENSE +20 -0
- data/README.markdown +59 -0
- data/Rakefile +27 -0
- data/VERSION +1 -0
- data/lib/generators/data_mapper/roles/roles_generator.rb +74 -0
- data/lib/roles_data_mapper/base.rb +32 -0
- data/lib/roles_data_mapper/namespaces.rb +12 -0
- data/lib/roles_data_mapper/role.rb +30 -0
- data/lib/roles_data_mapper/strategy/multi/many_roles.rb +85 -0
- data/lib/roles_data_mapper/strategy/multi/roles_mask.rb +81 -0
- data/lib/roles_data_mapper/strategy/single/admin_flag.rb +56 -0
- data/lib/roles_data_mapper/strategy/single/one_role.rb +62 -0
- data/lib/roles_data_mapper/strategy/single/role_string.rb +51 -0
- data/lib/roles_data_mapper/strategy.rb +15 -0
- data/lib/roles_data_mapper.rb +12 -0
- data/roles_data_mapper.gemspec +99 -0
- data/spec/generator_spec_helper.rb +13 -0
- data/spec/roles_data_mapper/generators/roles_generator_spec.rb +58 -0
- data/spec/roles_data_mapper/strategy/admin_flag_spec.rb +79 -0
- data/spec/roles_data_mapper/strategy/many_roles_spec.rb +76 -0
- data/spec/roles_data_mapper/strategy/one_role_spec.rb +81 -0
- data/spec/roles_data_mapper/strategy/role_string_spec.rb +73 -0
- data/spec/roles_data_mapper/strategy/roles_mask_spec.rb +73 -0
- data/spec/spec_helper.rb +17 -0
- data/tmp/rails/config/routes.rb +2 -0
- metadata +217 -0
@@ -0,0 +1,99 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{roles_data_mapper}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = %q{2010-09-12}
|
13
|
+
s.description = %q{Makes it easy to set a role strategy on your User model in DataMapper}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"LICENSE",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/generators/data_mapper/roles/roles_generator.rb",
|
28
|
+
"lib/roles_data_mapper.rb",
|
29
|
+
"lib/roles_data_mapper/base.rb",
|
30
|
+
"lib/roles_data_mapper/namespaces.rb",
|
31
|
+
"lib/roles_data_mapper/role.rb",
|
32
|
+
"lib/roles_data_mapper/strategy.rb",
|
33
|
+
"lib/roles_data_mapper/strategy/multi/many_roles.rb",
|
34
|
+
"lib/roles_data_mapper/strategy/multi/roles_mask.rb",
|
35
|
+
"lib/roles_data_mapper/strategy/single/admin_flag.rb",
|
36
|
+
"lib/roles_data_mapper/strategy/single/one_role.rb",
|
37
|
+
"lib/roles_data_mapper/strategy/single/role_string.rb",
|
38
|
+
"roles_data_mapper.gemspec",
|
39
|
+
"spec/generator_spec_helper.rb",
|
40
|
+
"spec/roles_data_mapper/generators/roles_generator_spec.rb",
|
41
|
+
"spec/roles_data_mapper/strategy/admin_flag_spec.rb",
|
42
|
+
"spec/roles_data_mapper/strategy/many_roles_spec.rb",
|
43
|
+
"spec/roles_data_mapper/strategy/one_role_spec.rb",
|
44
|
+
"spec/roles_data_mapper/strategy/role_string_spec.rb",
|
45
|
+
"spec/roles_data_mapper/strategy/roles_mask_spec.rb",
|
46
|
+
"spec/spec_helper.rb",
|
47
|
+
"tmp/rails/config/routes.rb"
|
48
|
+
]
|
49
|
+
s.homepage = %q{http://github.com/kristianmandrup/roles_for_dm}
|
50
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
51
|
+
s.require_paths = ["lib"]
|
52
|
+
s.rubygems_version = %q{1.3.7}
|
53
|
+
s.summary = %q{Implementation of Roles generic API for DataMapper}
|
54
|
+
s.test_files = [
|
55
|
+
"spec/generator_spec_helper.rb",
|
56
|
+
"spec/roles_data_mapper/generators/roles_generator_spec.rb",
|
57
|
+
"spec/roles_data_mapper/strategy/admin_flag_spec.rb",
|
58
|
+
"spec/roles_data_mapper/strategy/many_roles_spec.rb",
|
59
|
+
"spec/roles_data_mapper/strategy/one_role_spec.rb",
|
60
|
+
"spec/roles_data_mapper/strategy/role_string_spec.rb",
|
61
|
+
"spec/roles_data_mapper/strategy/roles_mask_spec.rb",
|
62
|
+
"spec/spec_helper.rb"
|
63
|
+
]
|
64
|
+
|
65
|
+
if s.respond_to? :specification_version then
|
66
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
67
|
+
s.specification_version = 3
|
68
|
+
|
69
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
70
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
71
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.0"])
|
72
|
+
s.add_runtime_dependency(%q<dm-types>, ["~> 1.0"])
|
73
|
+
s.add_runtime_dependency(%q<dm-migrations>, ["~> 1.0"])
|
74
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
|
75
|
+
s.add_runtime_dependency(%q<require_all>, ["~> 1.1.0"])
|
76
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
77
|
+
s.add_runtime_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
78
|
+
else
|
79
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
80
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0"])
|
81
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0"])
|
82
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0"])
|
83
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
84
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
85
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
86
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
87
|
+
end
|
88
|
+
else
|
89
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
90
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0"])
|
91
|
+
s.add_dependency(%q<dm-types>, ["~> 1.0"])
|
92
|
+
s.add_dependency(%q<dm-migrations>, ["~> 1.0"])
|
93
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
94
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
95
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
96
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'rails3_artifactor'
|
4
|
+
require 'generator-spec'
|
5
|
+
require 'roles_generic'
|
6
|
+
|
7
|
+
RSpec::Generator.configure do |config|
|
8
|
+
config.debug = true #false
|
9
|
+
config.remove_temp_dir = false # true
|
10
|
+
config.default_rails_root(__FILE__)
|
11
|
+
config.lib = File.dirname(__FILE__) + '/../lib'
|
12
|
+
config.logger = :stdout
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'generator_spec_helper'
|
2
|
+
require_generator :data_mapper => :roles
|
3
|
+
|
4
|
+
# root_dir = Rails3::Assist::Directory.rails_root
|
5
|
+
root_dir = File.join(Rails.application.config.root_dir, 'rails')
|
6
|
+
|
7
|
+
describe 'role strategy generator: admin_flag' do
|
8
|
+
describe 'ORM: data_mapper' do
|
9
|
+
use_orm :data_mapper
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
setup_generator 'datamapper_roles_generator' do
|
13
|
+
tests DataMapper::Generators::RolesGenerator
|
14
|
+
end
|
15
|
+
remove_model :user
|
16
|
+
end
|
17
|
+
|
18
|
+
after :each do
|
19
|
+
remove_model :user
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should configure 'admin_flag' strategy" do
|
23
|
+
create_model :user do
|
24
|
+
'# content'
|
25
|
+
end
|
26
|
+
with_generator do |g|
|
27
|
+
arguments = "User --strategy admin_flag --roles admin user"
|
28
|
+
puts "arguments: #{arguments}"
|
29
|
+
g.run_generator arguments.args
|
30
|
+
g.should generate_model :user do |clazz|
|
31
|
+
clazz.should include_module 'DataMapper::Resource'
|
32
|
+
clazz.should include_module 'Roles::DataMapper'
|
33
|
+
clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
|
34
|
+
clazz.should have_call :strategy, :args => ":admin_flag"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should configure 'one_role' strategy" do
|
40
|
+
create_model :user do
|
41
|
+
'# content'
|
42
|
+
end
|
43
|
+
with_generator do |g|
|
44
|
+
arguments = "User --strategy one_role --roles admin user"
|
45
|
+
puts "arguments: #{arguments}"
|
46
|
+
g.run_generator arguments.args
|
47
|
+
g.should generate_model :user do |clazz|
|
48
|
+
clazz.should include_module 'DataMapper::Resource'
|
49
|
+
clazz.should include_module 'Roles::DataMapper'
|
50
|
+
clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
|
51
|
+
clazz.should have_call :role_class, :args => ":role"
|
52
|
+
clazz.should have_call :strategy, :args => ":one_role"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :admin_flag
|
3
|
+
|
4
|
+
class User
|
5
|
+
include DataMapper::Resource
|
6
|
+
include Roles::DataMapper
|
7
|
+
|
8
|
+
strategy :admin_flag, :default
|
9
|
+
|
10
|
+
property :id, Serial
|
11
|
+
property :name, String
|
12
|
+
end
|
13
|
+
|
14
|
+
DataMapper.finalize
|
15
|
+
DataMapper.auto_migrate!
|
16
|
+
|
17
|
+
User.valid_roles_are :admin, :guest
|
18
|
+
|
19
|
+
describe "Roles for Mongoid" do
|
20
|
+
before :each do
|
21
|
+
|
22
|
+
@user = User.create(:name => 'Kristian')
|
23
|
+
@user.roles = :guest
|
24
|
+
@user.save
|
25
|
+
|
26
|
+
@admin_user = User.create(:name => 'Admin user')
|
27
|
+
@admin_user.roles = :admin
|
28
|
+
@admin_user.save
|
29
|
+
|
30
|
+
puts "user: #{@user.inspect}"
|
31
|
+
puts "admin: #{@admin_user.inspect}"
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "Admin flag strategy " do
|
35
|
+
|
36
|
+
describe '#in_role' do
|
37
|
+
it "should return first user maching role" do
|
38
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
39
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'roles API' do
|
44
|
+
it "should have admin user role to :admin" do
|
45
|
+
@admin_user.role.should == :admin
|
46
|
+
@admin_user.roles.should == [:admin]
|
47
|
+
@admin_user.admin?.should be_true
|
48
|
+
|
49
|
+
@admin_user.class.valid_roles.should == [:admin, :guest]
|
50
|
+
|
51
|
+
@admin_user.has_role?(:guest).should be_false
|
52
|
+
|
53
|
+
@admin_user.has_role?(:admin).should be_true
|
54
|
+
@admin_user.is?(:admin).should be_true
|
55
|
+
@admin_user.has_roles?(:admin).should be_true
|
56
|
+
@admin_user.has?(:admin).should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have user role to :guest" do
|
60
|
+
@user.roles.should == [:guest]
|
61
|
+
# @user.admin?.should be_false
|
62
|
+
|
63
|
+
# @user.has_role?(:guest).should be_true
|
64
|
+
# @user.has_role?(:admin).should be_false
|
65
|
+
# @user.is?(:admin).should be_false
|
66
|
+
#
|
67
|
+
# @user.has_roles?(:guest).should be_true
|
68
|
+
# @user.has?(:admin).should be_false
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should set user role to :admin using =" do
|
72
|
+
@user.roles = :admin
|
73
|
+
@user.role.should == :admin
|
74
|
+
@user.has_role?(:admin).should be_true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :many_roles
|
3
|
+
|
4
|
+
class User
|
5
|
+
include DataMapper::Resource
|
6
|
+
include Roles::DataMapper
|
7
|
+
|
8
|
+
strategy :many_roles, :default
|
9
|
+
role_class :role
|
10
|
+
|
11
|
+
property :id, Serial
|
12
|
+
property :name, String
|
13
|
+
end
|
14
|
+
|
15
|
+
DataMapper.finalize
|
16
|
+
DataMapper.auto_migrate!
|
17
|
+
|
18
|
+
User.valid_roles_are :admin, :guest
|
19
|
+
|
20
|
+
describe "Roles for Mongoid" do
|
21
|
+
context "default setup" do
|
22
|
+
|
23
|
+
before :each do
|
24
|
+
# DataMapper.finalize
|
25
|
+
# DataMapper.auto_migrate!
|
26
|
+
|
27
|
+
@user = User.create(:name => 'Kristian')
|
28
|
+
@user.add_roles :guest
|
29
|
+
@user.save
|
30
|
+
|
31
|
+
@admin_user = User.create(:name => 'Admin user')
|
32
|
+
@admin_user.roles = :admin
|
33
|
+
@admin_user.save
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#in_role' do
|
37
|
+
it "should return first user matching role" do
|
38
|
+
User.in_roles(:guest).first.name.should == 'Kristian'
|
39
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "Role API" do
|
44
|
+
it "should have admin user role to :admin" do
|
45
|
+
@admin_user.roles_list.first.should == :admin
|
46
|
+
@admin_user.admin?.should be_true
|
47
|
+
|
48
|
+
@admin_user.has_role?(:guest).should be_false
|
49
|
+
|
50
|
+
@admin_user.has_role?(:admin).should be_true
|
51
|
+
@admin_user.is?(:admin).should be_true
|
52
|
+
@admin_user.has_roles?(:admin).should be_true
|
53
|
+
@admin_user.has?(:admin).should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should have user role to :guest" do
|
57
|
+
@user.roles_list.first.should == :guest
|
58
|
+
@user.admin?.should be_false
|
59
|
+
|
60
|
+
@user.has_role?(:guest).should be_true
|
61
|
+
@user.has_role?(:admin).should be_false
|
62
|
+
@user.is?(:admin).should be_false
|
63
|
+
|
64
|
+
@user.has_roles?(:admin).should be_false
|
65
|
+
@user.has?(:admin).should be_false
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should set user role to :admin using roles=" do
|
69
|
+
@user.roles = :admin
|
70
|
+
@user.roles_list.first.should == :admin
|
71
|
+
@user.has_role?(:admin).should be_true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :one_role
|
3
|
+
|
4
|
+
class User
|
5
|
+
include DataMapper::Resource
|
6
|
+
include Roles::DataMapper
|
7
|
+
|
8
|
+
strategy :one_role, :default
|
9
|
+
role_class :role
|
10
|
+
|
11
|
+
property :id, Serial
|
12
|
+
property :name, String
|
13
|
+
end
|
14
|
+
|
15
|
+
DataMapper.finalize
|
16
|
+
DataMapper.auto_migrate!
|
17
|
+
|
18
|
+
User.valid_roles_are :admin, :guest
|
19
|
+
|
20
|
+
describe "Roles for Mongoid" do
|
21
|
+
|
22
|
+
context "default setup" do
|
23
|
+
|
24
|
+
before :each do
|
25
|
+
# DataMapper.finalize
|
26
|
+
# DataMapper.auto_migrate!
|
27
|
+
|
28
|
+
@user = User.create(:name => 'Kristian')
|
29
|
+
@user.role = :guest
|
30
|
+
@user.save
|
31
|
+
|
32
|
+
@admin_user = User.create(:name => 'Admin user')
|
33
|
+
@admin_user.role = :admin
|
34
|
+
@admin_user.save
|
35
|
+
|
36
|
+
puts "user: #{@user.inspect}"
|
37
|
+
puts "admin: #{@admin_user.inspect}"
|
38
|
+
puts "role: #{Role.first.inspect}"
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#in_role' do
|
42
|
+
it "should return first user matching role" do
|
43
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
44
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "Role API" do
|
49
|
+
it "should have admin user role to :admin" do
|
50
|
+
@admin_user.roles_list.first.should == :admin
|
51
|
+
@admin_user.admin?.should be_true
|
52
|
+
|
53
|
+
@admin_user.has_role?(:guest).should be_false
|
54
|
+
|
55
|
+
@admin_user.has_role?(:admin).should be_true
|
56
|
+
@admin_user.is?(:admin).should be_true
|
57
|
+
@admin_user.has_roles?(:admin).should be_true
|
58
|
+
@admin_user.has?(:admin).should be_true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should have user role to :guest" do
|
62
|
+
@user.roles_list.first.should == :guest
|
63
|
+
@user.admin?.should be_false
|
64
|
+
|
65
|
+
@user.has_role?(:guest).should be_true
|
66
|
+
@user.has_role?(:admin).should be_false
|
67
|
+
@user.is?(:admin).should be_false
|
68
|
+
|
69
|
+
@user.has_roles?(:admin).should be_false
|
70
|
+
@user.has?(:admin).should be_false
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should set user role to :admin using roles=" do
|
74
|
+
@user.roles = :admin
|
75
|
+
@user.roles_list.first.should == :admin
|
76
|
+
@user.has_role?(:admin).should be_true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :role_string
|
3
|
+
|
4
|
+
class User
|
5
|
+
include DataMapper::Resource
|
6
|
+
include Roles::DataMapper
|
7
|
+
|
8
|
+
strategy :role_string, :default
|
9
|
+
|
10
|
+
property :id, Serial
|
11
|
+
property :name, String
|
12
|
+
end
|
13
|
+
|
14
|
+
DataMapper.finalize
|
15
|
+
DataMapper.auto_migrate!
|
16
|
+
|
17
|
+
User.valid_roles_are :admin, :guest
|
18
|
+
|
19
|
+
describe "Roles for Mongoid" do
|
20
|
+
|
21
|
+
context "default setup" do
|
22
|
+
before :each do
|
23
|
+
|
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 DataMapper::Resource
|
6
|
+
include Roles::DataMapper
|
7
|
+
|
8
|
+
strategy :roles_mask, :default
|
9
|
+
|
10
|
+
property :id, Serial
|
11
|
+
property :name, String
|
12
|
+
end
|
13
|
+
|
14
|
+
DataMapper.finalize
|
15
|
+
DataMapper.auto_migrate!
|
16
|
+
|
17
|
+
User.valid_roles_are :admin, :guest
|
18
|
+
|
19
|
+
describe "Roles for Mongoid" do
|
20
|
+
|
21
|
+
context "default setup" do
|
22
|
+
|
23
|
+
before :each 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
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'dm-core'
|
4
|
+
require 'dm-types'
|
5
|
+
require 'dm-migrations'
|
6
|
+
require 'roles_data_mapper'
|
7
|
+
|
8
|
+
# gem install dm-core dm-sqlite-adapter
|
9
|
+
# gem install dm-types dm-validations dm-timestamps dm-aggregates dm-adjust dm-is-list dm-is-tree dm-is-versioned dm-is-nested_set
|
10
|
+
# gem install rails_datamapper dm-migrations dm-observer
|
11
|
+
|
12
|
+
DataMapper::Logger.new($stdout, :debug)
|
13
|
+
DataMapper.setup(:default, 'sqlite::memory:')
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.mock_with :mocha
|
17
|
+
end
|