role_play 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +18 -0
- data/README +35 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/generators/role_play/USAGE +7 -0
- data/generators/role_play/role_play_generator.rb +10 -0
- data/generators/role_play/templates/migration.rb +17 -0
- data/install.rb +1 -0
- data/lib/role.rb +6 -0
- data/lib/role_assignment.rb +6 -0
- data/lib/role_play.rb +60 -0
- data/rails/init.rb +4 -0
- data/role_play.gemspec +63 -0
- data/tasks/has_roles_tasks.rake +4 -0
- data/test/assertions.rb +82 -0
- data/test/role_play_test.rb +151 -0
- data/test/test_helper.rb +29 -0
- data/uninstall.rb +1 -0
- metadata +86 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Krzysztof Zylawy
|
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/Manifest
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
generators/has_roles/has_roles_generator.rb
|
2
|
+
generators/has_roles/templates/migration.rb
|
3
|
+
generators/has_roles/USAGE
|
4
|
+
has_roles.gemspec
|
5
|
+
install.rb
|
6
|
+
lib/has_roles.rb
|
7
|
+
lib/role.rb
|
8
|
+
lib/role_assignment.rb
|
9
|
+
Manifest
|
10
|
+
MIT-LICENSE
|
11
|
+
rails/init.rb
|
12
|
+
Rakefile
|
13
|
+
README
|
14
|
+
tasks/has_roles_tasks.rake
|
15
|
+
test/assertions.rb
|
16
|
+
test/has_roles_test.rb
|
17
|
+
test/test_helper.rb
|
18
|
+
uninstall.rb
|
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Install
|
2
|
+
=======
|
3
|
+
|
4
|
+
$ gem source -a http://gems.github.com/
|
5
|
+
$ sudo gem install mintdigital-has_roles
|
6
|
+
|
7
|
+
# environment.rb
|
8
|
+
config.gem 'mintdigital-has_roles', :lib => 'has_roles'
|
9
|
+
|
10
|
+
Example
|
11
|
+
=======
|
12
|
+
|
13
|
+
In an ActiveRecord class:
|
14
|
+
|
15
|
+
has_roles :admin, :moderator
|
16
|
+
|
17
|
+
Then run:
|
18
|
+
|
19
|
+
./script/generate has_roles && rake db:migrate
|
20
|
+
|
21
|
+
Now your model has these instance methods:
|
22
|
+
|
23
|
+
add_role(:moderator) #=> true
|
24
|
+
remove_role(:admin) #=> true (doesn't have role)
|
25
|
+
has_role?(:admin) #=> false
|
26
|
+
can_have_role?(:writer) #=> false
|
27
|
+
|
28
|
+
# These are auto-generated based on your defined roles.
|
29
|
+
# They just call has_role?(name)
|
30
|
+
admin? #=> false
|
31
|
+
moderator? #=> true
|
32
|
+
|
33
|
+
---
|
34
|
+
|
35
|
+
Copyright (c) 2008 Krzysztof Zylawy, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require File.join(File.dirname(__FILE__), %w[lib/role_play])
|
5
|
+
|
6
|
+
# begin
|
7
|
+
# require 'echoe'
|
8
|
+
# Echoe.new('has_roles') do |a|
|
9
|
+
# a.version = HasRoles::Version::STRING
|
10
|
+
# a.summary = "Simple roles for ActiveRecord"
|
11
|
+
# a.author = "Krzysztof Zylawy"
|
12
|
+
# a.email = "krzysztof@mintdigital.com"
|
13
|
+
# a.runtime_dependencies = ['activerecord']
|
14
|
+
# a.development_dependencies = ['echoe']
|
15
|
+
# a.has_rdoc = false
|
16
|
+
# a.retain_gemspec = true
|
17
|
+
# end
|
18
|
+
# rescue LoadError
|
19
|
+
# puts "Install 'echoe' if you want gem building-ness."
|
20
|
+
# end
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'jeweler'
|
24
|
+
Jeweler::Tasks.new do |s|
|
25
|
+
s.name = "role_play"
|
26
|
+
s.summary = "Simple roles for ActiveRecord"
|
27
|
+
s.email = "philnash@gmail.com"
|
28
|
+
s.homepage = "http://github.com/mintdigital/has_roles"
|
29
|
+
s.authors = ["Krzysztof Zylawy", "Mint Digital"]
|
30
|
+
s.add_dependency 'activerecord'
|
31
|
+
end
|
32
|
+
Jeweler::GemcutterTasks.new
|
33
|
+
rescue LoadError
|
34
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
desc 'Default: Run Tests'
|
39
|
+
Rake::TestTask.new do |t|
|
40
|
+
t.warning = true
|
41
|
+
t.pattern = 'test/**/*_test.rb'
|
42
|
+
end
|
43
|
+
|
44
|
+
desc 'Generate documentation for the role_play plugin.'
|
45
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = 'RolePlay'
|
48
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
49
|
+
rdoc.rdoc_files.include('README')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateRoles < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :roles do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
|
7
|
+
create_table :role_assignments do |t|
|
8
|
+
t.integer :roleable_id, :role_id
|
9
|
+
t.string :roleable_type
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :role_assignments
|
15
|
+
drop_table :roles
|
16
|
+
end
|
17
|
+
end
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/role.rb
ADDED
data/lib/role_play.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module RolePlay
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend HasMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module HasMethods
|
8
|
+
def has_roles(*roles)
|
9
|
+
roles.flatten!
|
10
|
+
write_inheritable_attribute(:available_roles, roles)
|
11
|
+
class_inheritable_reader :available_roles
|
12
|
+
|
13
|
+
unless included_modules.include? InstanceMethods
|
14
|
+
has_many :role_assignments, :as => :roleable, :dependent => :destroy
|
15
|
+
has_many :roles, :through => :role_assignments
|
16
|
+
|
17
|
+
extend ClassMethods
|
18
|
+
include InstanceMethods
|
19
|
+
available_roles.each do |role|
|
20
|
+
self.class_eval do
|
21
|
+
define_method "#{role}?".to_sym do
|
22
|
+
has_role?(role)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
def can_have_role?(role_name)
|
35
|
+
available_roles.include?(role_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def has_role?(rolename)
|
39
|
+
!self.roles.find_by_name(rolename.to_s).nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_role rolename
|
43
|
+
return false unless can_have_role?(rolename)
|
44
|
+
return true if has_role?(rolename)
|
45
|
+
self.roles << Role.find_or_create_by_name(rolename.to_s)
|
46
|
+
self.save
|
47
|
+
end
|
48
|
+
|
49
|
+
def remove_role name
|
50
|
+
return true if !has_role?(name)
|
51
|
+
role_assignment = self.role_assignments.detect { |x| x.role.name == name.to_s }
|
52
|
+
role_assignment.destroy if role_assignment
|
53
|
+
end
|
54
|
+
|
55
|
+
def clear_roles
|
56
|
+
role_assignments.clear
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
data/rails/init.rb
ADDED
data/role_play.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
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{role_play}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Krzysztof Zylawy", "Mint Digital"]
|
12
|
+
s.date = %q{2009-12-04}
|
13
|
+
s.email = %q{philnash@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"Manifest",
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"generators/role_play/USAGE",
|
25
|
+
"generators/role_play/role_play_generator.rb",
|
26
|
+
"generators/role_play/templates/migration.rb",
|
27
|
+
"install.rb",
|
28
|
+
"lib/role.rb",
|
29
|
+
"lib/role_assignment.rb",
|
30
|
+
"lib/role_play.rb",
|
31
|
+
"rails/init.rb",
|
32
|
+
"role_play.gemspec",
|
33
|
+
"tasks/has_roles_tasks.rake",
|
34
|
+
"test/assertions.rb",
|
35
|
+
"test/role_play_test.rb",
|
36
|
+
"test/test_helper.rb",
|
37
|
+
"uninstall.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/mintdigital/has_roles}
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.5}
|
43
|
+
s.summary = %q{Simple roles for ActiveRecord}
|
44
|
+
s.test_files = [
|
45
|
+
"test/assertions.rb",
|
46
|
+
"test/role_play_test.rb",
|
47
|
+
"test/test_helper.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/test/assertions.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Copyright (c) 2005-2009 David Heinemeier Hansson
|
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.
|
21
|
+
|
22
|
+
module ActiveSupport
|
23
|
+
module Testing
|
24
|
+
module Assertions
|
25
|
+
# Test numeric difference between the return value of an expression as a result of what is evaluated
|
26
|
+
# in the yielded block.
|
27
|
+
#
|
28
|
+
# assert_difference 'Article.count' do
|
29
|
+
# post :create, :article => {...}
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# An arbitrary expression is passed in and evaluated.
|
33
|
+
#
|
34
|
+
# assert_difference 'assigns(:article).comments(:reload).size' do
|
35
|
+
# post :create, :comment => {...}
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# An arbitrary positive or negative difference can be specified. The default is +1.
|
39
|
+
#
|
40
|
+
# assert_difference 'Article.count', -1 do
|
41
|
+
# post :delete, :id => ...
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# An array of expressions can also be passed in and evaluated.
|
45
|
+
#
|
46
|
+
# assert_difference [ 'Article.count', 'Post.count' ], +2 do
|
47
|
+
# post :create, :article => {...}
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# A error message can be specified.
|
51
|
+
#
|
52
|
+
# assert_difference 'Article.count', -1, "An Article should be destroyed" do
|
53
|
+
# post :delete, :id => ...
|
54
|
+
# end
|
55
|
+
def assert_difference(expressions, difference = 1, message = nil, &block)
|
56
|
+
expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } }
|
57
|
+
|
58
|
+
original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
|
59
|
+
yield
|
60
|
+
expression_evaluations.each_with_index do |expression, i|
|
61
|
+
assert_equal original_values[i] + difference, expression.call, message
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Assertion that the numeric result of evaluating an expression is not changed before and after
|
66
|
+
# invoking the passed in block.
|
67
|
+
#
|
68
|
+
# assert_no_difference 'Article.count' do
|
69
|
+
# post :create, :article => invalid_attributes
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# A error message can be specified.
|
73
|
+
#
|
74
|
+
# assert_no_difference 'Article.count', "An Article should not be destroyed" do
|
75
|
+
# post :create, :article => invalid_attributes
|
76
|
+
# end
|
77
|
+
def assert_no_difference(expressions, message = nil, &block)
|
78
|
+
assert_difference expressions, 0, message, &block
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class RoleableSample < ActiveRecord::Base
|
4
|
+
has_roles [:sample_role_1, :sample_role_2]
|
5
|
+
end
|
6
|
+
|
7
|
+
class AnotherRoleable < ActiveRecord::Base
|
8
|
+
has_roles :another1, :another2
|
9
|
+
end
|
10
|
+
|
11
|
+
class RolePlayTest < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@roleable_sample = RoleableSample.create
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
[Role, RoleAssignment, RoleableSample].each {|klass| klass.delete_all }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_active_record_should_respond_to_has_roles
|
22
|
+
assert ActiveRecord::Base.respond_to?(:has_roles)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_roleable_class_should_return_available_roles
|
26
|
+
assert_equal [:sample_role_1, :sample_role_2], RoleableSample.available_roles
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_roleable_object_can_have_its_class_roles
|
30
|
+
[:sample_role_1, :sample_role_2].each do |role_name|
|
31
|
+
assert_equal(true, @roleable_sample.can_have_role?(role_name))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_be_able_to_add_valid_role_to_roleable
|
36
|
+
assert_difference "RoleAssignment.count", 1 do
|
37
|
+
assert_difference "Role.count", 1 do
|
38
|
+
@roleable_sample.add_role(:sample_role_1)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_return_true_when_role_added
|
44
|
+
assert @roleable_sample.add_role(:sample_role_1)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_not_be_able_to_add_invalid_role_to_roleable
|
48
|
+
assert_no_difference "RoleAssignment.count", 1 do
|
49
|
+
assert_no_difference "Role.count", 1 do
|
50
|
+
@roleable_sample.add_role(:invalid_role)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
assert_equal(false, @roleable_sample.has_role?(:invalid_role))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_should_return_false_when_invalid_role_not_added
|
57
|
+
assert !@roleable_sample.add_role(:invalid_role)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_not_add_role_nor_assignment_if_already_has_role
|
61
|
+
@roleable_sample.add_role(:sample_role_1)
|
62
|
+
assert_no_difference "RoleAssignment.count" do
|
63
|
+
assert_no_difference "Role.count" do
|
64
|
+
@roleable_sample.add_role(:sample_role_1)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
assert_equal(true, @roleable_sample.has_role?(:sample_role_1))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_return_true_if_already_has_role
|
71
|
+
@roleable_sample.add_role(:sample_role_1)
|
72
|
+
assert @roleable_sample.has_role?(:sample_role_1), 'Precondition: has role'
|
73
|
+
assert @roleable_sample.add_role(:sample_role_1)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_should_add_assignment_but_not_role_nor_assignment_if_role_exists
|
77
|
+
Role.create(:name => 'sample_role_1')
|
78
|
+
assert_difference "RoleAssignment.count", 1 do
|
79
|
+
assert_no_difference "Role.count" do
|
80
|
+
@roleable_sample.add_role(:sample_role_1)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
assert_equal(true, @roleable_sample.has_role?(:sample_role_1))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_should_have_added_role
|
87
|
+
@roleable_sample.add_role(:sample_role_1)
|
88
|
+
assert_equal(true, @roleable_sample.has_role?(:sample_role_1))
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_should_not_have_not_added_role
|
92
|
+
@roleable_sample.add_role(:sample_role_1)
|
93
|
+
assert_equal(false, @roleable_sample.has_role?(:sample_role_2))
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_should_remove_role_assignment
|
97
|
+
@roleable_sample.add_role(:sample_role_1)
|
98
|
+
assert_difference "RoleAssignment.count", -1 do
|
99
|
+
assert_no_difference "Role.count" do
|
100
|
+
@roleable_sample.remove_role(:sample_role_1)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
assert_equal(false, @roleable_sample.has_role?(:sample_role_1))
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_should_return_true_when_role_removed
|
107
|
+
@roleable_sample.add_role(:sample_role_1)
|
108
|
+
assert @roleable_sample.has_role?(:sample_role_1), 'Precondition: has role'
|
109
|
+
assert @roleable_sample.remove_role(:sample_role_1)
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_should_not_remove_role_assignment_when_removing_other_role
|
113
|
+
@roleable_sample.add_role(:sample_role_1)
|
114
|
+
assert_no_difference "RoleAssignment.count" do
|
115
|
+
assert_no_difference "Role.count" do
|
116
|
+
@roleable_sample.remove_role(:sample_role_2)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
assert_equal(true, @roleable_sample.has_role?(:sample_role_1))
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_should_return_true_when_trying_to_remove_un_had_role
|
123
|
+
assert !@roleable_sample.has_role?(:sample_role_1), 'Precondition: does not have role'
|
124
|
+
assert @roleable_sample.remove_role(:sample_role_1)
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_should_return_true_when_asking_if_is_existing_role
|
128
|
+
@roleable_sample.add_role(:sample_role_1)
|
129
|
+
assert_equal(true, @roleable_sample.sample_role_1?)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_should_return_false_when_asking_if_is_unexisting_role
|
133
|
+
@roleable_sample.add_role(:sample_role_1)
|
134
|
+
assert_equal(false, @roleable_sample.sample_role_2?)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_should_raise_exception_when_asking_if_is_invalid_role
|
138
|
+
@roleable_sample.add_role(:sample_role_1)
|
139
|
+
assert_raise(NoMethodError) {
|
140
|
+
@roleable_sample.invalid_role?
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_should_clear_roles
|
145
|
+
@roleable_sample.add_role(:sample_role_1)
|
146
|
+
assert_difference "RoleAssignment.count", -1 do
|
147
|
+
@roleable_sample.clear_roles
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
require File.join(File.dirname(__FILE__), 'assertions')
|
5
|
+
require File.join(File.dirname(__FILE__), %w[.. rails init.rb])
|
6
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
7
|
+
require 'role'
|
8
|
+
require 'role_assignment'
|
9
|
+
|
10
|
+
Test::Unit::TestCase.send :include, ActiveSupport::Testing::Assertions
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(
|
13
|
+
:adapter => 'sqlite3', :database => ':memory:'
|
14
|
+
)
|
15
|
+
|
16
|
+
ActiveRecord::Schema.define(:version => 1) do
|
17
|
+
create_table :roleable_samples do |t|
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :roles do |t|
|
21
|
+
t.string :name
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :role_assignments do |t|
|
25
|
+
t.integer :roleable_id
|
26
|
+
t.string :roleable_type
|
27
|
+
t.integer :role_id
|
28
|
+
end
|
29
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: role_play
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Zylawy
|
8
|
+
- Mint Digital
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-12-04 00:00:00 +00:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
version:
|
26
|
+
description:
|
27
|
+
email: philnash@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- MIT-LICENSE
|
37
|
+
- Manifest
|
38
|
+
- README
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- generators/role_play/USAGE
|
42
|
+
- generators/role_play/role_play_generator.rb
|
43
|
+
- generators/role_play/templates/migration.rb
|
44
|
+
- install.rb
|
45
|
+
- lib/role.rb
|
46
|
+
- lib/role_assignment.rb
|
47
|
+
- lib/role_play.rb
|
48
|
+
- rails/init.rb
|
49
|
+
- role_play.gemspec
|
50
|
+
- tasks/has_roles_tasks.rake
|
51
|
+
- test/assertions.rb
|
52
|
+
- test/role_play_test.rb
|
53
|
+
- test/test_helper.rb
|
54
|
+
- uninstall.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/mintdigital/has_roles
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Simple roles for ActiveRecord
|
83
|
+
test_files:
|
84
|
+
- test/assertions.rb
|
85
|
+
- test/role_play_test.rb
|
86
|
+
- test/test_helper.rb
|