roles_for_dm 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 +168 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/generators/data_mapper/roles/roles_generator.rb +46 -0
- data/lib/roles_for_dm.rb +1 -0
- data/lib/roles_for_dm/role.rb +5 -0
- data/lib/roles_for_dm/strategies/admin_flag.rb +21 -0
- data/lib/roles_for_dm/strategies/inline_role.rb +14 -0
- data/lib/roles_for_dm/strategies/role_relations.rb +22 -0
- data/lib/roles_for_dm/strategies/roles_mask.rb +66 -0
- data/lib/roles_for_dm/user.rb +15 -0
- data/spec/roles_for_dm/user_admin_field_spec.rb +25 -0
- data/spec/roles_for_dm/user_inline_role_spec.rb +25 -0
- data/spec/roles_for_dm/user_role_relations_spec.rb +40 -0
- data/spec/roles_for_dm/user_roles_mask_spec.rb +32 -0
- data/spec/spec_helper.rb +19 -0
- metadata +146 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested --color
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# Roles for Data Mapper
|
2
|
+
|
3
|
+
# Install
|
4
|
+
|
5
|
+
<code>gem install roles_for_dm</code>
|
6
|
+
|
7
|
+
# Usage
|
8
|
+
|
9
|
+
The library comes with the following role models built-in:
|
10
|
+
|
11
|
+
* Single inline role (string)
|
12
|
+
* Multiple inline role (strings)
|
13
|
+
* Multiple role relations (many relationship using array of object ids)
|
14
|
+
|
15
|
+
Note: The following examples use RSpec to demonstrate usage scenarios.
|
16
|
+
|
17
|
+
## Example : Admin flag
|
18
|
+
|
19
|
+
Creates and uses a binary field 'admin', which when true signals that this user is an administrator and otherwise a normal user.
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
require 'roles_for_dm'
|
23
|
+
|
24
|
+
User.role_strategy :admin_flag
|
25
|
+
|
26
|
+
before :each do
|
27
|
+
User.new(:name => 'Guest user', :role => 'guest').save
|
28
|
+
User.new(:name => 'Admin user', :role => 'admin').save
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return first user maching role" do
|
32
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
33
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
34
|
+
end
|
35
|
+
</pre>
|
36
|
+
|
37
|
+
before :each do
|
38
|
+
User.new(:name => 'Guest user', :role => 'guest').save
|
39
|
+
User.new(:name => 'Admin user', :role => 'admin').save
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return first user maching role" do
|
43
|
+
User.in_role('guest').first.name.should == 'Guest user'
|
44
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
45
|
+
end
|
46
|
+
</pre>
|
47
|
+
|
48
|
+
## Example : Single inline role
|
49
|
+
|
50
|
+
Creates and uses a single role name, a string
|
51
|
+
|
52
|
+
<pre>
|
53
|
+
User.role_strategy :inline_role
|
54
|
+
|
55
|
+
# same as the previous admin_flag example
|
56
|
+
...
|
57
|
+
</pre>
|
58
|
+
|
59
|
+
## Example : Multiple inline roles
|
60
|
+
|
61
|
+
Creates and uses an array of role names as strings
|
62
|
+
|
63
|
+
<pre>
|
64
|
+
User.role_strategy :inline_roles
|
65
|
+
|
66
|
+
before :each do
|
67
|
+
guest_user = User.new(:name => 'Guest user', :roles => ['guest']).save
|
68
|
+
admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
|
69
|
+
author_user = User.new(:name => 'Author', :roles => ['author']).save
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return first user matching role" do
|
73
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return all users matching any of the roles" do
|
77
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
78
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
79
|
+
end
|
80
|
+
</pre>
|
81
|
+
|
82
|
+
## Example : Multiple role relations
|
83
|
+
|
84
|
+
Creates and uses a 1-M relation to a Role model, which the User model stores as an array of BSON ObjectIDs
|
85
|
+
|
86
|
+
<pre>
|
87
|
+
require 'roles_for_mm'
|
88
|
+
|
89
|
+
User.role_strategy :roles_relations
|
90
|
+
|
91
|
+
before :each do
|
92
|
+
guest_user = User.new(:name => 'Guest user')
|
93
|
+
admin_user = User.new(:name => 'Admin user')
|
94
|
+
author_user = User.new(:name => 'Author')
|
95
|
+
|
96
|
+
guest_user.roles << Role.new(:name => 'guest')
|
97
|
+
guest_user.save
|
98
|
+
|
99
|
+
admin_user.roles << Role.new(:name => 'admin')
|
100
|
+
admin_user.save
|
101
|
+
|
102
|
+
author_user.roles << Role.new(:name => 'author')
|
103
|
+
author_user.save
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return first user matching role" do
|
107
|
+
User.in_role('guest').first.name.should == 'Guest user'
|
108
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should return all users matching any of the roles" do
|
112
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
113
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
114
|
+
end
|
115
|
+
</pre>
|
116
|
+
|
117
|
+
## Example : Roles mask
|
118
|
+
|
119
|
+
Creates and uses an inline Integer field in the User model called 'role_mask'
|
120
|
+
|
121
|
+
<pre>
|
122
|
+
before :each do
|
123
|
+
User.available_roles = ['guest', 'admin', 'author']
|
124
|
+
|
125
|
+
User.new(:name => 'Kristian', :roles => ['guest']).save
|
126
|
+
User.new(:name => 'Admin user', :roles => ['admin']).save
|
127
|
+
User.new(:name => 'Author', :roles => ['author']).save
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return first user matching role" do
|
131
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should return all users matching any of the roles" do
|
135
|
+
User.in_roles(['guest', 'admin']).size.should == 2
|
136
|
+
User.in_roles('guest', 'admin').size.should == 2
|
137
|
+
end
|
138
|
+
</pre>
|
139
|
+
|
140
|
+
## Rails generator
|
141
|
+
|
142
|
+
The library comes with a Rails 3 generator that lets you populate a user model with a given role strategy
|
143
|
+
The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
|
144
|
+
|
145
|
+
* admin_flag
|
146
|
+
* inline_role
|
147
|
+
* inline_roles
|
148
|
+
* role_relations
|
149
|
+
* role_mask
|
150
|
+
|
151
|
+
Example:
|
152
|
+
|
153
|
+
<code>$ rails g mongo_mapper:roles user admin_flag</code>
|
154
|
+
|
155
|
+
|
156
|
+
## Note on Patches/Pull Requests
|
157
|
+
|
158
|
+
* Fork the project.
|
159
|
+
* Make your feature addition or bug fix.
|
160
|
+
* Add tests for it. This is important so I don't break it in a
|
161
|
+
future version unintentionally.
|
162
|
+
* Commit, do not mess with rakefile, version, or history.
|
163
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
164
|
+
* Send me a pull request. Bonus points for topic branches.
|
165
|
+
|
166
|
+
## Copyright
|
167
|
+
|
168
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gem|
|
4
|
+
gem.name = "roles_for_dm"
|
5
|
+
gem.summary = %Q{Faciliatates adding a role strategy to your Data Mapper user model}
|
6
|
+
gem.description = %Q{Faciliatates adding a role strategy to your Data Mapper user model}
|
7
|
+
gem.email = "kmandrup@gmail.com"
|
8
|
+
gem.homepage = "http://github.com/kristianmandrup/roles_for_dm"
|
9
|
+
gem.authors = ["Kristian Mandrup"]
|
10
|
+
gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
|
11
|
+
gem.add_dependency "dm-core", ">= 1.0"
|
12
|
+
gem.add_dependency "dm-types", ">= 1.0"
|
13
|
+
gem.add_dependency "dm-migrations", ">= 1.0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
# require 'spec/rake/spectask'
|
22
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
# spec.libs << 'lib' << 'spec'
|
24
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
# spec.libs << 'lib' << 'spec'
|
29
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
# spec.rcov = true
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# task :spec => :check_dependencies
|
34
|
+
#
|
35
|
+
# task :default => :spec
|
36
|
+
#
|
37
|
+
# require 'rake/rdoctask'
|
38
|
+
# Rake::RDocTask.new do |rdoc|
|
39
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
#
|
41
|
+
# rdoc.rdoc_dir = 'rdoc'
|
42
|
+
# rdoc.title = "roles_for_mm #{version}"
|
43
|
+
# rdoc.rdoc_files.include('README*')
|
44
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
# end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Generators
|
3
|
+
class RolesGenerator < Rails::Generators::NamedBase
|
4
|
+
desc "Generate roles model for User"
|
5
|
+
|
6
|
+
argument :role_strategy, :type => :string, :aliases => "-r", :default => 'inline_role', :desc => "Create roles model for user"
|
7
|
+
|
8
|
+
hook_for :orm
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
@source_root ||= File.expand_path("../../templates", __FILE__)
|
12
|
+
end
|
13
|
+
|
14
|
+
def apply_role_strategy
|
15
|
+
insert_into_model('user', role_strategy_statement)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def match_expr
|
21
|
+
/include DataMapper::Resource/
|
22
|
+
end
|
23
|
+
|
24
|
+
def role_strategy_statement
|
25
|
+
"role_strategy #{role_strategy}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def role_strategy
|
29
|
+
options[:role_strategy]
|
30
|
+
end
|
31
|
+
|
32
|
+
def model_file(name)
|
33
|
+
File.join(Rails.root, "app/models/#{name}.rb")
|
34
|
+
end
|
35
|
+
|
36
|
+
def insert_into_model(model_name, insert_text)
|
37
|
+
model_name = model_name.to_s
|
38
|
+
file = File.new(model_file(model_name))
|
39
|
+
return if (file.read =~ /#{insert_text}/)
|
40
|
+
gsub_file model_file(model_name), match_expr do |match|
|
41
|
+
match << insert_text
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/roles_for_dm.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'roles_for_dm/user'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module AdminFlag
|
3
|
+
|
4
|
+
def self.included base
|
5
|
+
base.extend ClassMethods
|
6
|
+
base.property :role, DataMapper::Property::Flag[:admin, :default]
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def in_role(role_name)
|
12
|
+
case role_name.downcase.to_sym
|
13
|
+
when :admin
|
14
|
+
all(:role => :admin)
|
15
|
+
else
|
16
|
+
all(:role => :default)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'roles_for_dm/role'
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
module RoleRelations
|
5
|
+
def self.included base
|
6
|
+
base.extend ClassMethods
|
7
|
+
base.has base.n, :roles
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def in_role(role_name)
|
12
|
+
role = Role.all(:name => role_name).first
|
13
|
+
all('roles.id' => role.id)
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_roles(*roles)
|
17
|
+
role_ids = Role.all(:name.in => roles.flatten).map{|role| role.id}
|
18
|
+
all(:'roles.id'.in => role_ids)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class Array
|
2
|
+
def extract(sym)
|
3
|
+
map { |e| e.send(sym) }
|
4
|
+
end
|
5
|
+
|
6
|
+
def sum
|
7
|
+
inject( 0 ) { |sum,x| sum+x }
|
8
|
+
end
|
9
|
+
|
10
|
+
def mean
|
11
|
+
(size > 0) ? sum.to_f / size : 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module DataMapper
|
16
|
+
module RolesMask
|
17
|
+
def self.included base
|
18
|
+
base.extend ClassMethods
|
19
|
+
base.property :roles_mask, Integer
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
attr_accessor :available_roles
|
24
|
+
|
25
|
+
def in_role(role)
|
26
|
+
mask = calc_index(role.to_s)
|
27
|
+
all.select do |user|
|
28
|
+
(user.roles_mask & mask) > 0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def in_roles(*roles)
|
33
|
+
all.select do |user|
|
34
|
+
roles.flatten.any? do |role|
|
35
|
+
mask = calc_index(role.to_s)
|
36
|
+
(user.roles_mask & mask) > 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def calc_index(r)
|
42
|
+
2**available_roles.index(r)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def available_roles
|
47
|
+
self.class.available_roles
|
48
|
+
end
|
49
|
+
|
50
|
+
def calc_index(r)
|
51
|
+
self.class.calc_index(r)
|
52
|
+
end
|
53
|
+
|
54
|
+
def roles=(*roles)
|
55
|
+
self.roles_mask = (roles.flatten & available_roles).map { |r| calc_index(r) }.sum
|
56
|
+
end
|
57
|
+
|
58
|
+
def roles
|
59
|
+
self.available_roles.reject { |r| ((roles_mask || 0) & self.calc_index(r)).zero? }
|
60
|
+
end
|
61
|
+
|
62
|
+
def role?(role)
|
63
|
+
roles.include?(role.to_s)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'require_all'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
class User
|
5
|
+
include DataMapper::Resource
|
6
|
+
|
7
|
+
property :id, Serial
|
8
|
+
property :name, String
|
9
|
+
|
10
|
+
def self.role_strategy type
|
11
|
+
include "DataMapper::#{type.to_s.camelize}".constantize
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require_all File.dirname(__FILE__) + '/strategies'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :admin_flag
|
4
|
+
|
5
|
+
describe "Roles for DataMapper" do
|
6
|
+
describe "Inline admin field" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
# Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
DataMapper.finalize
|
14
|
+
DataMapper.auto_migrate!
|
15
|
+
|
16
|
+
User.new(:name => 'Kristian', :role => :default).save
|
17
|
+
User.new(:name => 'Admin user', :role => :admin).save
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return first user maching role" do
|
21
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
22
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :inline_role
|
4
|
+
|
5
|
+
describe "Roles for MongoMapper" do
|
6
|
+
describe "Inline role" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
# Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
DataMapper.finalize
|
14
|
+
DataMapper.auto_migrate!
|
15
|
+
|
16
|
+
User.new(:name => 'Guest user', :role => 'guest').save
|
17
|
+
User.new(:name => 'Admin user', :role => 'admin').save
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return first user maching role" do
|
21
|
+
User.in_role('guest').first.name.should == 'Guest user'
|
22
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :role_relations
|
4
|
+
|
5
|
+
describe "Roles for DataMapper" do
|
6
|
+
|
7
|
+
after :each do
|
8
|
+
# Database.teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
DataMapper.finalize
|
13
|
+
DataMapper.auto_migrate!
|
14
|
+
|
15
|
+
guest_user = User.new(:name => 'Kristian')
|
16
|
+
admin_user = User.new(:name => 'Admin user')
|
17
|
+
author_user = User.new(:name => 'Author')
|
18
|
+
|
19
|
+
guest_user.roles << Role.new(:name => 'guest')
|
20
|
+
guest_user.save
|
21
|
+
|
22
|
+
admin_user.roles << Role.new(:name => 'admin')
|
23
|
+
admin_user.save
|
24
|
+
|
25
|
+
author_user.roles << Role.new(:name => 'author')
|
26
|
+
author_user.save
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Role relations" do
|
30
|
+
it "should return first user matching role" do
|
31
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
32
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return all users matching any of the roles" do
|
36
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
37
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :roles_mask
|
4
|
+
|
5
|
+
describe "Roles for DataMapper" do
|
6
|
+
describe "Inline roles" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
# Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
DataMapper.finalize
|
14
|
+
DataMapper.auto_migrate!
|
15
|
+
|
16
|
+
User.available_roles = ['guest', 'admin', 'author']
|
17
|
+
|
18
|
+
User.new(:name => 'Kristian', :roles => ['guest']).save
|
19
|
+
User.new(:name => 'Admin user', :roles => ['admin']).save
|
20
|
+
User.new(:name => 'Author', :roles => ['author']).save
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return first user matching role" do
|
24
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return all users matching any of the roles" do
|
28
|
+
User.in_roles(['guest', 'admin']).size.should == 2
|
29
|
+
User.in_roles('guest', 'admin').size.should == 2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'dm-core'
|
4
|
+
require 'dm-types'
|
5
|
+
require 'dm-migrations'
|
6
|
+
require 'roles_for_dm'
|
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 dm-querizer
|
11
|
+
|
12
|
+
DataMapper::Logger.new($stdout, :debug)
|
13
|
+
DataMapper.setup(:default, 'sqlite::memory:')
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roles_for_dm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-03 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta
|
33
|
+
- 19
|
34
|
+
version: 2.0.0.beta.19
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: dm-core
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
version: "1.0"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: dm-types
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 0
|
62
|
+
version: "1.0"
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: dm-migrations
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 0
|
76
|
+
version: "1.0"
|
77
|
+
type: :runtime
|
78
|
+
version_requirements: *id004
|
79
|
+
description: Faciliatates adding a role strategy to your Data Mapper user model
|
80
|
+
email: kmandrup@gmail.com
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files:
|
86
|
+
- LICENSE
|
87
|
+
- README.markdown
|
88
|
+
files:
|
89
|
+
- .document
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- LICENSE
|
93
|
+
- README.markdown
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- lib/generators/data_mapper/roles/roles_generator.rb
|
97
|
+
- lib/roles_for_dm.rb
|
98
|
+
- lib/roles_for_dm/role.rb
|
99
|
+
- lib/roles_for_dm/strategies/admin_flag.rb
|
100
|
+
- lib/roles_for_dm/strategies/inline_role.rb
|
101
|
+
- lib/roles_for_dm/strategies/role_relations.rb
|
102
|
+
- lib/roles_for_dm/strategies/roles_mask.rb
|
103
|
+
- lib/roles_for_dm/user.rb
|
104
|
+
- spec/roles_for_dm/user_admin_field_spec.rb
|
105
|
+
- spec/roles_for_dm/user_inline_role_spec.rb
|
106
|
+
- spec/roles_for_dm/user_role_relations_spec.rb
|
107
|
+
- spec/roles_for_dm/user_roles_mask_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
has_rdoc: true
|
110
|
+
homepage: http://github.com/kristianmandrup/roles_for_dm
|
111
|
+
licenses: []
|
112
|
+
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options:
|
115
|
+
- --charset=UTF-8
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
requirements: []
|
135
|
+
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.3.7
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Faciliatates adding a role strategy to your Data Mapper user model
|
141
|
+
test_files:
|
142
|
+
- spec/roles_for_dm/user_admin_field_spec.rb
|
143
|
+
- spec/roles_for_dm/user_inline_role_spec.rb
|
144
|
+
- spec/roles_for_dm/user_role_relations_spec.rb
|
145
|
+
- spec/roles_for_dm/user_roles_mask_spec.rb
|
146
|
+
- spec/spec_helper.rb
|