roles_mongoid 0.2.4 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +11 -7
- data/VERSION +1 -1
- data/lib/roles_mongoid/base.rb +6 -4
- data/lib/roles_mongoid/role.rb +3 -1
- data/lib/roles_mongoid/strategy/multi/many_roles.rb +51 -35
- data/lib/roles_mongoid/strategy/multi/role_strings.rb +43 -22
- data/lib/roles_mongoid/strategy/multi/roles_mask.rb +67 -18
- data/lib/roles_mongoid/strategy/multi.rb +59 -0
- data/lib/roles_mongoid/strategy/shared.rb +29 -0
- data/lib/roles_mongoid/strategy/single/admin_flag.rb +20 -18
- data/lib/roles_mongoid/strategy/single/one_role.rb +55 -24
- data/lib/roles_mongoid/strategy/single/role_string.rb +54 -22
- data/lib/roles_mongoid/strategy/single.rb +34 -0
- data/roles_mongoid.gemspec +44 -34
- data/sandbox/single_role.rb +25 -11
- data/spec/{generators → roles_mongoid/generators}/roles_generator_spec.rb +0 -0
- data/spec/roles_mongoid/strategy/api_examples.rb +200 -0
- data/spec/roles_mongoid/strategy/multi/many_roles_spec.rb +40 -0
- data/spec/roles_mongoid/strategy/multi/role_strings_spec.rb +19 -0
- data/spec/roles_mongoid/strategy/multi/roles_mask_spec.rb +39 -0
- data/spec/roles_mongoid/strategy/single/admin_flag_spec.rb +18 -0
- data/spec/roles_mongoid/strategy/single/one_role_spec.rb +19 -0
- data/spec/roles_mongoid/strategy/single/role_string_spec.rb +19 -0
- data/spec/roles_mongoid/strategy/user_setup.rb +13 -0
- data/spec/spec_helper.rb +0 -13
- metadata +62 -43
- data/spec/roles_mongoid/admin_flag_spec.rb +0 -74
- data/spec/roles_mongoid/many_roles_spec.rb +0 -74
- data/spec/roles_mongoid/one_role_spec.rb +0 -74
- data/spec/roles_mongoid/role_string_spec.rb +0 -73
- data/spec/roles_mongoid/role_strings_spec.rb +0 -73
- data/spec/roles_mongoid/roles_mask_spec.rb +0 -73
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_mongoid/strategy/single'
|
2
|
+
|
1
3
|
module RoleStrategy::Mongoid
|
2
4
|
module AdminFlag
|
3
5
|
def self.default_role_attribute
|
@@ -24,26 +26,26 @@ module RoleStrategy::Mongoid
|
|
24
26
|
end
|
25
27
|
|
26
28
|
module Implementation
|
27
|
-
|
28
|
-
strategy_class.roles_attribute_name
|
29
|
-
end
|
30
|
-
|
31
|
-
# assign roles
|
32
|
-
def roles=(*new_roles)
|
33
|
-
first_role = new_roles.flatten.first
|
34
|
-
if valid_role?(first_role)
|
35
|
-
self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
|
36
|
-
else
|
37
|
-
raise ArgumentError, "The role #{first_role} is not a valid role"
|
38
|
-
end
|
39
|
-
end
|
29
|
+
include Roles::Mongoid::Strategy::Single
|
40
30
|
|
41
|
-
|
42
|
-
|
43
|
-
role
|
44
|
-
[role]
|
31
|
+
def new_role role
|
32
|
+
role = role.kind_of?(Array) ? role.flatten.first : role
|
33
|
+
role.admin?
|
45
34
|
end
|
46
|
-
|
35
|
+
|
36
|
+
def get_role
|
37
|
+
self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
38
|
+
end
|
39
|
+
|
40
|
+
def present_roles *roles
|
41
|
+
roles = roles.flat_uniq
|
42
|
+
roles.map{|role| role ? :admin : :guest}
|
43
|
+
end
|
44
|
+
|
45
|
+
def set_empty_role
|
46
|
+
self.send("#{role_attribute}=", false)
|
47
|
+
end
|
48
|
+
|
47
49
|
|
48
50
|
end # Implementation
|
49
51
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_mongoid/strategy/single'
|
2
|
+
|
1
3
|
module RoleStrategy::Mongoid
|
2
4
|
module OneRole
|
3
5
|
def self.default_role_attribute
|
@@ -13,42 +15,71 @@ module RoleStrategy::Mongoid
|
|
13
15
|
def role_attribute
|
14
16
|
strategy_class.roles_attribute_name.to_sym
|
15
17
|
end
|
18
|
+
|
19
|
+
def role_attribute_id
|
20
|
+
"#{role_attribute}_id"
|
21
|
+
end
|
16
22
|
|
17
|
-
def in_role(role_name)
|
18
|
-
|
23
|
+
def in_role(role_name)
|
24
|
+
in_any_role(role_name)
|
19
25
|
end
|
20
26
|
|
21
|
-
def
|
22
|
-
begin
|
23
|
-
|
24
|
-
|
27
|
+
def in_any_role(*role_names)
|
28
|
+
begin
|
29
|
+
the_roles = Role.where(:name.in => role_names.to_strings).to_a
|
30
|
+
role_ids = the_roles.map(&:_id).map(&:to_s)
|
31
|
+
where(:"#{role_attribute_id}".in => role_ids)
|
25
32
|
rescue
|
26
33
|
return []
|
27
34
|
end
|
28
35
|
end
|
29
36
|
end
|
30
37
|
|
31
|
-
module Implementation
|
32
|
-
|
33
|
-
def roles=(*_roles)
|
34
|
-
_roles = get_roles(_roles)
|
35
|
-
return nil if _roles.none?
|
36
|
-
|
37
|
-
role_relation = role_class.find_role(_roles.first)
|
38
|
-
self.send("#{role_attribute}=", role_relation)
|
39
|
-
one_role.save
|
40
|
-
end
|
41
|
-
alias_method :role=, :roles=
|
38
|
+
module Implementation
|
39
|
+
include Roles::Mongoid::Strategy::Single
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def new_role role
|
42
|
+
role_class.find_role(extract_role role)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_roles *roles
|
46
|
+
new_role roles.flatten.first
|
47
|
+
end
|
48
|
+
|
49
|
+
def present_roles *roles
|
50
|
+
roles.map{|role| extract_role role}
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_empty_role
|
54
|
+
self.send("#{role_attribute}=", nil)
|
47
55
|
end
|
56
|
+
|
57
|
+
def select_valid_roles *role_names
|
58
|
+
role_names = role_names.flat_uniq.select{|role| valid_role? role }
|
59
|
+
has_role_class? ? role_class.find_roles(role_names).to_a : role_names
|
60
|
+
end
|
48
61
|
|
49
|
-
|
50
|
-
|
51
|
-
|
62
|
+
#
|
63
|
+
# # assign roles
|
64
|
+
# def roles=(*_roles)
|
65
|
+
# _roles = get_roles(_roles)
|
66
|
+
# return nil if _roles.none?
|
67
|
+
#
|
68
|
+
# role_relation = role_class.find_role(_roles.first)
|
69
|
+
# self.send("#{role_attribute}=", role_relation)
|
70
|
+
# one_role.save
|
71
|
+
# end
|
72
|
+
# alias_method :role=, :roles=
|
73
|
+
#
|
74
|
+
# # query assigned roles
|
75
|
+
# def roles
|
76
|
+
# role = self.send(role_attribute)
|
77
|
+
# role ? [role.name.to_sym] : []
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# def roles_list
|
81
|
+
# self.roles.to_a
|
82
|
+
# end
|
52
83
|
end
|
53
84
|
|
54
85
|
extend Roles::Generic::User::Configuration
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_mongoid/strategy/single'
|
2
|
+
|
1
3
|
module RoleStrategy::Mongoid
|
2
4
|
module RoleString
|
3
5
|
def self.default_role_attribute
|
@@ -11,36 +13,66 @@ module RoleStrategy::Mongoid
|
|
11
13
|
module ClassMethods
|
12
14
|
def role_attribute
|
13
15
|
strategy_class.roles_attribute_name.to_sym
|
14
|
-
end
|
15
|
-
|
16
|
-
def in_role(role_name)
|
17
|
-
|
16
|
+
end
|
17
|
+
|
18
|
+
def in_role(role_name)
|
19
|
+
in_any_role(role_name)
|
18
20
|
end
|
21
|
+
|
22
|
+
def in_any_role(*role_names)
|
23
|
+
begin
|
24
|
+
where(:"#{role_attribute}".in => role_names)
|
25
|
+
rescue
|
26
|
+
return []
|
27
|
+
end
|
28
|
+
end
|
19
29
|
end
|
20
30
|
|
21
31
|
|
22
|
-
module Implementation
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
32
|
+
module Implementation
|
33
|
+
include Roles::Mongoid::Strategy::Single
|
34
|
+
|
35
|
+
def new_role role
|
36
|
+
role = role.kind_of?(Array) ? role.first : role
|
37
|
+
role.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def new_roles *roles
|
41
|
+
new_role roles.flatten.first
|
42
|
+
end
|
31
43
|
|
32
|
-
def role
|
33
|
-
if
|
34
|
-
|
35
|
-
end
|
44
|
+
def present_role role
|
45
|
+
return role if role.kind_of? Array
|
46
|
+
role.split(',').map(&:to_sym)
|
36
47
|
end
|
37
48
|
|
38
|
-
|
39
|
-
|
40
|
-
role = self.send(role_attribute)
|
41
|
-
[role.to_sym]
|
49
|
+
def set_empty_role
|
50
|
+
self.send("#{role_attribute}=", "")
|
42
51
|
end
|
43
|
-
|
52
|
+
|
53
|
+
alias_method :present_roles, :present_role
|
54
|
+
|
55
|
+
# def role_attribute
|
56
|
+
# strategy_class.roles_attribute_name
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# # assign roles
|
60
|
+
# def roles=(*roles)
|
61
|
+
# self.role = roles.select_labels.first.to_s
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# def role= role_name
|
65
|
+
# if role_name.kind_of_label? && valid_role?(role_name)
|
66
|
+
# self.send("#{role_attribute}=", role_name.to_s)
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# # query assigned roles
|
71
|
+
# def roles
|
72
|
+
# role = self.send(role_attribute)
|
73
|
+
# [role.to_sym]
|
74
|
+
# end
|
75
|
+
# alias_method :roles_list, :roles
|
44
76
|
end
|
45
77
|
|
46
78
|
extend Roles::Generic::User::Configuration
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'roles_mongoid/strategy/shared'
|
2
|
+
|
3
|
+
class Symbol
|
4
|
+
def admin?
|
5
|
+
self == :admin
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Roles::Mongoid
|
10
|
+
module Strategy
|
11
|
+
module Single
|
12
|
+
include Shared
|
13
|
+
# assigns first valid role from list of roles
|
14
|
+
def add_roles *role_names
|
15
|
+
new_roles = select_valid_roles(role_names)
|
16
|
+
new_role = new_roles.first if !new_roles.empty?
|
17
|
+
set_role new_role
|
18
|
+
end
|
19
|
+
|
20
|
+
# should remove the current single role (set = nil)
|
21
|
+
# only if it is contained in the list of roles to be removed
|
22
|
+
def remove_roles *role_names
|
23
|
+
roles = role_names.flat_uniq
|
24
|
+
set_empty_role if roles_diff(roles).empty?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def roles_list
|
29
|
+
raise 'the method #roles should be present' if !respond_to? :roles
|
30
|
+
present_roles(roles)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/roles_mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{roles_mongoid}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-23}
|
13
13
|
s.description = %q{Makes it easy to set a role strategy on your User model in Mongoid}
|
14
14
|
s.email = %q{kmandrup@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,9 +32,12 @@ Gem::Specification.new do |s|
|
|
32
32
|
"lib/roles_mongoid/namespaces.rb",
|
33
33
|
"lib/roles_mongoid/role.rb",
|
34
34
|
"lib/roles_mongoid/strategy.rb",
|
35
|
+
"lib/roles_mongoid/strategy/multi.rb",
|
35
36
|
"lib/roles_mongoid/strategy/multi/many_roles.rb",
|
36
37
|
"lib/roles_mongoid/strategy/multi/role_strings.rb",
|
37
38
|
"lib/roles_mongoid/strategy/multi/roles_mask.rb",
|
39
|
+
"lib/roles_mongoid/strategy/shared.rb",
|
40
|
+
"lib/roles_mongoid/strategy/single.rb",
|
38
41
|
"lib/roles_mongoid/strategy/single/admin_flag.rb",
|
39
42
|
"lib/roles_mongoid/strategy/single/one_role.rb",
|
40
43
|
"lib/roles_mongoid/strategy/single/role_string.rb",
|
@@ -43,13 +46,15 @@ Gem::Specification.new do |s|
|
|
43
46
|
"sandbox/single_role.rb",
|
44
47
|
"sandbox/test.rb",
|
45
48
|
"spec/generator_spec_helper.rb",
|
46
|
-
"spec/generators/roles_generator_spec.rb",
|
47
|
-
"spec/roles_mongoid/
|
48
|
-
"spec/roles_mongoid/many_roles_spec.rb",
|
49
|
-
"spec/roles_mongoid/
|
50
|
-
"spec/roles_mongoid/
|
51
|
-
"spec/roles_mongoid/
|
52
|
-
"spec/roles_mongoid/
|
49
|
+
"spec/roles_mongoid/generators/roles_generator_spec.rb",
|
50
|
+
"spec/roles_mongoid/strategy/api_examples.rb",
|
51
|
+
"spec/roles_mongoid/strategy/multi/many_roles_spec.rb",
|
52
|
+
"spec/roles_mongoid/strategy/multi/role_strings_spec.rb",
|
53
|
+
"spec/roles_mongoid/strategy/multi/roles_mask_spec.rb",
|
54
|
+
"spec/roles_mongoid/strategy/single/admin_flag_spec.rb",
|
55
|
+
"spec/roles_mongoid/strategy/single/one_role_spec.rb",
|
56
|
+
"spec/roles_mongoid/strategy/single/role_string_spec.rb",
|
57
|
+
"spec/roles_mongoid/strategy/user_setup.rb",
|
53
58
|
"spec/spec_helper.rb",
|
54
59
|
"tmp/rails/app/models/user.rb",
|
55
60
|
"tmp/rails/config/routes.rb"
|
@@ -61,13 +66,15 @@ Gem::Specification.new do |s|
|
|
61
66
|
s.summary = %q{Implementation of Roles generic API for Mongoid}
|
62
67
|
s.test_files = [
|
63
68
|
"spec/generator_spec_helper.rb",
|
64
|
-
"spec/generators/roles_generator_spec.rb",
|
65
|
-
"spec/roles_mongoid/
|
66
|
-
"spec/roles_mongoid/many_roles_spec.rb",
|
67
|
-
"spec/roles_mongoid/
|
68
|
-
"spec/roles_mongoid/
|
69
|
-
"spec/roles_mongoid/
|
70
|
-
"spec/roles_mongoid/
|
69
|
+
"spec/roles_mongoid/generators/roles_generator_spec.rb",
|
70
|
+
"spec/roles_mongoid/strategy/api_examples.rb",
|
71
|
+
"spec/roles_mongoid/strategy/multi/many_roles_spec.rb",
|
72
|
+
"spec/roles_mongoid/strategy/multi/role_strings_spec.rb",
|
73
|
+
"spec/roles_mongoid/strategy/multi/roles_mask_spec.rb",
|
74
|
+
"spec/roles_mongoid/strategy/single/admin_flag_spec.rb",
|
75
|
+
"spec/roles_mongoid/strategy/single/one_role_spec.rb",
|
76
|
+
"spec/roles_mongoid/strategy/single/role_string_spec.rb",
|
77
|
+
"spec/roles_mongoid/strategy/user_setup.rb",
|
71
78
|
"spec/spec_helper.rb"
|
72
79
|
]
|
73
80
|
|
@@ -76,30 +83,33 @@ Gem::Specification.new do |s|
|
|
76
83
|
s.specification_version = 3
|
77
84
|
|
78
85
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
79
|
-
s.add_development_dependency(%q<rspec>, ["
|
80
|
-
s.add_runtime_dependency(%q<mongoid>, ["
|
81
|
-
s.add_runtime_dependency(%q<bson>, ["
|
82
|
-
s.add_runtime_dependency(%q<
|
86
|
+
s.add_development_dependency(%q<rspec>, [">= 2.1"])
|
87
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
|
88
|
+
s.add_runtime_dependency(%q<bson>, [">= 1.1.1"])
|
89
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.0"])
|
83
90
|
s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
|
84
|
-
s.add_runtime_dependency(%q<
|
85
|
-
s.add_runtime_dependency(%q<
|
91
|
+
s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.0"])
|
92
|
+
s.add_runtime_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
|
93
|
+
s.add_runtime_dependency(%q<logging_assist>, ["~> 0.1.6"])
|
86
94
|
else
|
87
|
-
s.add_dependency(%q<rspec>, ["
|
88
|
-
s.add_dependency(%q<mongoid>, ["
|
89
|
-
s.add_dependency(%q<bson>, ["
|
90
|
-
s.add_dependency(%q<
|
95
|
+
s.add_dependency(%q<rspec>, [">= 2.1"])
|
96
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
|
97
|
+
s.add_dependency(%q<bson>, [">= 1.1.1"])
|
98
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
|
91
99
|
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
|
92
|
-
s.add_dependency(%q<
|
93
|
-
s.add_dependency(%q<
|
100
|
+
s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
|
101
|
+
s.add_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
|
102
|
+
s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
|
94
103
|
end
|
95
104
|
else
|
96
|
-
s.add_dependency(%q<rspec>, ["
|
97
|
-
s.add_dependency(%q<mongoid>, ["
|
98
|
-
s.add_dependency(%q<bson>, ["
|
99
|
-
s.add_dependency(%q<
|
105
|
+
s.add_dependency(%q<rspec>, [">= 2.1"])
|
106
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
|
107
|
+
s.add_dependency(%q<bson>, [">= 1.1.1"])
|
108
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
|
100
109
|
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
|
101
|
-
s.add_dependency(%q<
|
102
|
-
s.add_dependency(%q<
|
110
|
+
s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
|
111
|
+
s.add_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
|
112
|
+
s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
|
103
113
|
end
|
104
114
|
end
|
105
115
|
|
data/sandbox/single_role.rb
CHANGED
@@ -12,7 +12,7 @@ end
|
|
12
12
|
class Role
|
13
13
|
include Mongoid::Document
|
14
14
|
field :name, :type => String
|
15
|
-
|
15
|
+
references_many :users #one_role, :class_name => 'Role'
|
16
16
|
|
17
17
|
class << self
|
18
18
|
def find_roles(*role_names)
|
@@ -36,27 +36,41 @@ end
|
|
36
36
|
|
37
37
|
class User
|
38
38
|
include Mongoid::Document
|
39
|
-
include Blip
|
39
|
+
# include Blip
|
40
40
|
field :name, :type => String
|
41
|
-
|
42
|
-
# do_it
|
41
|
+
referenced_in :one_role, :class_name => 'Role'
|
43
42
|
|
43
|
+
# do_it
|
44
44
|
end
|
45
45
|
|
46
|
-
user = User.create
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
user.role = role
|
46
|
+
user = User.create :name => 'Sandy'
|
47
|
+
user2 = User.create :name => 'Mike'
|
48
|
+
role = Role.create :name => 'Guest'
|
49
|
+
role2 = Role.create :name => 'Admin'
|
51
50
|
|
52
51
|
user.save
|
52
|
+
user2.save
|
53
|
+
|
54
|
+
user.one_role = role2 #<< [role, role2]
|
55
|
+
role.users << [user, user2]
|
56
|
+
user2.one_role = role
|
57
|
+
|
58
|
+
# role.users << user
|
59
|
+
# role.users << user2
|
60
|
+
|
61
|
+
role.save
|
62
|
+
|
63
|
+
puts "user: #{user.inspect}"
|
64
|
+
puts "user2 #{user2.inspect}"
|
65
|
+
puts "role: #{role.inspect}"
|
66
|
+
puts "role users: #{role.users.to_a.inspect}"
|
67
|
+
|
53
68
|
|
54
69
|
# Role.create :name => 'guest'
|
55
70
|
# Role.create :name => 'admin'
|
56
71
|
#
|
57
72
|
# user = User.create :name => 'Kristian'
|
58
73
|
#
|
59
|
-
# puts user.inspect
|
60
74
|
#
|
61
75
|
# user.role.create :name => :guest
|
62
76
|
# user.save
|
@@ -64,5 +78,5 @@ user.save
|
|
64
78
|
# user.role = Role.find_role(:guest).first
|
65
79
|
# user.save
|
66
80
|
|
67
|
-
|
81
|
+
|
68
82
|
|