roles_generic 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/generators/roles_model/roles/roles_generator.rb +14 -10
- data/lib/roles_generic/base.rb +4 -0
- data/lib/roles_generic/generic/user/class_methods.rb +2 -1
- data/lib/roles_generic/generic.rb +23 -4
- data/lib/roles_generic/namespaces.rb +1 -2
- data/{spec/model → lib/roles_generic}/role.rb +14 -1
- data/lib/roles_generic/strategy/multi/many_roles.rb +8 -4
- data/lib/roles_generic/strategy/single/admin_flag.rb +8 -4
- data/lib/roles_generic/strategy/single/one_role.rb +1 -0
- data/roles_generic.gemspec +3 -4
- data/spec/roles_generic/admin_flag_spec.rb +3 -3
- data/spec/roles_generic/many_roles_spec.rb +7 -12
- data/spec/roles_generic/one_role_spec.rb +27 -32
- data/spec/roles_generic/role_string_spec.rb +2 -2
- data/spec/roles_generic/role_strings_spec.rb +3 -3
- data/spec/roles_generic/roles_mask_spec.rb +3 -3
- data/spec/roles_generic/roles_string_spec.rb +3 -3
- metadata +4 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -31,36 +31,40 @@ module RolesModel
|
|
31
31
|
|
32
32
|
protected
|
33
33
|
|
34
|
-
def strategy
|
35
|
-
options[:strategy]
|
36
|
-
end
|
37
|
-
|
38
34
|
def orm
|
39
35
|
@orm ||= options[:orm].to_s.to_sym
|
40
36
|
end
|
41
37
|
|
38
|
+
def default_roles
|
39
|
+
[:admin, :guest]
|
40
|
+
end
|
41
|
+
|
42
|
+
def roles_to_add
|
43
|
+
default_roles.concat(options[:roles]).compact.uniq
|
44
|
+
end
|
45
|
+
|
42
46
|
def roles
|
43
|
-
@roles ||=
|
47
|
+
@roles ||= roles_to_add.map{|r| ":#{r}" }
|
44
48
|
end
|
45
49
|
|
46
50
|
def role_strategy_statement
|
47
|
-
"
|
51
|
+
"strategy :#{strategy}\n"
|
48
52
|
end
|
49
53
|
|
50
54
|
def roles_statement
|
51
|
-
roles ? "
|
55
|
+
roles ? "valid_roles #{roles.join(',')}" : ''
|
52
56
|
end
|
53
57
|
|
54
58
|
def insertion_text
|
55
59
|
%Q{
|
56
|
-
include Roles
|
60
|
+
include Roles::#{orm.to_s.camelize}
|
57
61
|
#{role_strategy_statement}
|
58
62
|
#{roles_statement}
|
59
63
|
}
|
60
64
|
end
|
61
65
|
|
62
|
-
def
|
63
|
-
options[:
|
66
|
+
def strategy
|
67
|
+
options[:strategy]
|
64
68
|
end
|
65
69
|
end
|
66
70
|
end
|
data/lib/roles_generic/base.rb
CHANGED
@@ -3,10 +3,29 @@ require_all File.dirname(__FILE__) + '/generic'
|
|
3
3
|
module Roles
|
4
4
|
module Generic
|
5
5
|
def self.included(base)
|
6
|
-
base.extend Roles::Base
|
7
|
-
base.
|
6
|
+
base.extend Roles::Base
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.orm_name = :generic
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
MAP = {
|
14
|
+
:admin_flag => "attr_accessor :admin_flag",
|
15
|
+
:many_roles => "attr_accessor :many_roles",
|
16
|
+
:one_role => "attr_accessor :one_role",
|
17
|
+
:roles_mask => "attr_accessor :roles_mask",
|
18
|
+
:role_string => "attr_accessor :role_string",
|
19
|
+
:role_strings => "attr_accessor :role_strings",
|
20
|
+
:roles_string => "attr_accessor :roles_string"
|
21
|
+
}
|
22
|
+
|
23
|
+
def strategy name, options=nil
|
24
|
+
if options == :default && MAP[name]
|
25
|
+
instance_eval MAP[name]
|
26
|
+
end
|
27
|
+
role_strategy name, options
|
28
|
+
end
|
8
29
|
end
|
9
30
|
end
|
10
31
|
end
|
11
|
-
|
12
|
-
|
@@ -1,8 +1,21 @@
|
|
1
|
+
module Roles::Base
|
2
|
+
def valid_roles_are(*role_list)
|
3
|
+
strategy_class.valid_roles = role_list.to_symbols
|
4
|
+
if role_class_name
|
5
|
+
role_list.each do |name|
|
6
|
+
role_class_name.new(name)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
1
12
|
class Role
|
2
13
|
attr_accessor :name
|
3
14
|
|
4
15
|
def self.find_role role_name
|
5
|
-
roles.to_a.select
|
16
|
+
roles.to_a.select do |r|
|
17
|
+
r.name == role_name
|
18
|
+
end.first
|
6
19
|
end
|
7
20
|
|
8
21
|
def self.find_roles *role_names
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'set'
|
2
|
+
require 'roles_generic/role'
|
2
3
|
|
3
4
|
module RoleStrategy::Generic
|
4
5
|
module ManyRoles
|
@@ -7,20 +8,23 @@ module RoleStrategy::Generic
|
|
7
8
|
end
|
8
9
|
|
9
10
|
module Implementation
|
11
|
+
def role_attribute
|
12
|
+
strategy_class.roles_attribute_name
|
13
|
+
end
|
14
|
+
|
10
15
|
# assign roles
|
11
16
|
def roles=(*roles)
|
12
17
|
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
13
18
|
|
14
19
|
role_relations = role_class.find_roles(*roles)
|
15
|
-
|
16
|
-
|
17
|
-
self.send("#{strategy_class.roles_attribute_name}=", role_relation) if role_relation.kind_of?(role_class)
|
20
|
+
role_relations.each do |role_relation|
|
21
|
+
self.send("#{role_attribute}=", role_relation) if role_relation.kind_of?(role_class)
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
21
25
|
# query assigned roles
|
22
26
|
def roles
|
23
|
-
self.send(
|
27
|
+
self.send(role_attribute)
|
24
28
|
end
|
25
29
|
|
26
30
|
def roles_list
|
@@ -4,23 +4,27 @@ module RoleStrategy::Generic
|
|
4
4
|
:admin_flag
|
5
5
|
end
|
6
6
|
|
7
|
-
module Implementation
|
7
|
+
module Implementation
|
8
|
+
def role_attribute
|
9
|
+
strategy_class.roles_attribute_name
|
10
|
+
end
|
11
|
+
|
8
12
|
# assign roles
|
9
13
|
def roles=(*new_roles)
|
10
14
|
first_role = new_roles.flatten.first
|
11
|
-
self.send("#{
|
15
|
+
self.send("#{role_attribute}=", new_roles.flatten.first.admin?) if valid_role? first_role
|
12
16
|
end
|
13
17
|
|
14
18
|
# query assigned roles
|
15
19
|
def roles
|
16
|
-
role = self.send(
|
20
|
+
role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
17
21
|
[role]
|
18
22
|
end
|
19
23
|
alias_method :roles_list, :roles
|
20
24
|
|
21
25
|
end # Implementation
|
22
26
|
|
23
|
-
extend
|
27
|
+
extend Roles::Generic::User::Configuration
|
24
28
|
configure :num => :single
|
25
29
|
end
|
26
30
|
end
|
data/roles_generic.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{roles_generic}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.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-09-
|
12
|
+
s.date = %q{2010-09-06}
|
13
13
|
s.description = %q{Generic role strategies sharing the same API. Easy to insert in any model}
|
14
14
|
s.email = %q{kmandrup@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/roles_generic/generic/user/configuration.rb",
|
35
35
|
"lib/roles_generic/generic/user/implementation.rb",
|
36
36
|
"lib/roles_generic/namespaces.rb",
|
37
|
+
"lib/roles_generic/role.rb",
|
37
38
|
"lib/roles_generic/strategy.rb",
|
38
39
|
"lib/roles_generic/strategy/multi/many_roles.rb",
|
39
40
|
"lib/roles_generic/strategy/multi/role_strings.rb",
|
@@ -51,7 +52,6 @@ Gem::Specification.new do |s|
|
|
51
52
|
"spec/generators/role_strings_generator_spec.rb",
|
52
53
|
"spec/generators/roles_mask_generator_spec.rb",
|
53
54
|
"spec/generators/roles_string_generator_spec.rb",
|
54
|
-
"spec/model/role.rb",
|
55
55
|
"spec/roles_generic/admin_flag_spec.rb",
|
56
56
|
"spec/roles_generic/many_roles_spec.rb",
|
57
57
|
"spec/roles_generic/one_role_spec.rb",
|
@@ -82,7 +82,6 @@ Gem::Specification.new do |s|
|
|
82
82
|
"spec/generators/role_strings_generator_spec.rb",
|
83
83
|
"spec/generators/roles_mask_generator_spec.rb",
|
84
84
|
"spec/generators/roles_string_generator_spec.rb",
|
85
|
-
"spec/model/role.rb",
|
86
85
|
"spec/roles_generic/admin_flag_spec.rb",
|
87
86
|
"spec/roles_generic/many_roles_spec.rb",
|
88
87
|
"spec/roles_generic/one_role_spec.rb",
|
@@ -4,11 +4,11 @@ use_roles_strategy :admin_flag
|
|
4
4
|
class User
|
5
5
|
include Roles::Generic
|
6
6
|
|
7
|
-
attr_accessor :name
|
7
|
+
attr_accessor :name
|
8
8
|
|
9
|
-
|
9
|
+
strategy :admin_flag, :default
|
10
10
|
|
11
|
-
|
11
|
+
valid_roles_are :admin, :user
|
12
12
|
|
13
13
|
def initialize name, *new_roles
|
14
14
|
self.name = name
|
@@ -1,35 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
use_roles_strategy :many_roles
|
3
3
|
|
4
|
-
require 'model/role'
|
5
|
-
|
6
4
|
class User
|
7
5
|
include Roles::Generic
|
8
|
-
|
6
|
+
strategy :many_roles, :default
|
9
7
|
|
10
8
|
role_class :role
|
11
9
|
|
12
|
-
attr_accessor :name
|
10
|
+
attr_accessor :name
|
13
11
|
|
14
|
-
|
12
|
+
valid_roles_are :admin, :user
|
15
13
|
|
16
14
|
def initialize name, *new_roles
|
17
15
|
self.name = name
|
18
16
|
self.roles = new_roles
|
19
17
|
end
|
20
|
-
end
|
18
|
+
end
|
21
19
|
|
22
20
|
|
23
21
|
describe "Generic ManyRoles role strategy" do
|
24
22
|
context "default setup" do
|
25
23
|
|
26
|
-
before :each do
|
27
|
-
Role.new :user
|
28
|
-
Role.new :admin
|
29
|
-
|
24
|
+
before :each do
|
30
25
|
@admin_user = User.new 'Admin user', :admin
|
31
|
-
@user = User.new 'User', :user
|
32
|
-
end
|
26
|
+
@user = User.new 'User', :user
|
27
|
+
end
|
33
28
|
|
34
29
|
it "should have admin user role to :admin" do
|
35
30
|
@admin_user.roles_list.first.should == :admin
|
@@ -1,17 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
use_roles_strategy :one_role
|
3
3
|
|
4
|
-
require 'model/role'
|
5
|
-
|
6
4
|
class User
|
7
5
|
include Roles::Generic
|
8
|
-
|
6
|
+
strategy :one_role, :default
|
9
7
|
|
10
8
|
role_class :role
|
11
9
|
|
12
|
-
attr_accessor :name
|
10
|
+
attr_accessor :name
|
13
11
|
|
14
|
-
|
12
|
+
valid_roles_are :admin, :user
|
15
13
|
|
16
14
|
def initialize name, *new_roles
|
17
15
|
self.name = name
|
@@ -23,10 +21,7 @@ end
|
|
23
21
|
describe "Generic OneRole role strategy" do
|
24
22
|
context "default setup" do
|
25
23
|
|
26
|
-
before :each do
|
27
|
-
Role.new :user
|
28
|
-
Role.new :admin
|
29
|
-
|
24
|
+
before :each do
|
30
25
|
@admin_user = User.new 'Admin user', :admin
|
31
26
|
@user = User.new 'User', :user
|
32
27
|
end
|
@@ -44,28 +39,28 @@ describe "Generic OneRole role strategy" do
|
|
44
39
|
@admin_user.has?(:admin).should be_true
|
45
40
|
end
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
42
|
+
it "should have user role to :user" do
|
43
|
+
@user.roles.should == [:user]
|
44
|
+
@user.admin?.should be_false
|
45
|
+
|
46
|
+
@user.has_role?(:user).should be_true
|
47
|
+
@user.has_role?(:admin).should be_false
|
48
|
+
@user.is?(:admin).should be_false
|
49
|
+
|
50
|
+
@user.has_roles?(:admin).should be_false
|
51
|
+
@user.has?(:admin).should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set user role to :admin using roles=" do
|
55
|
+
@user.roles = :admin
|
56
|
+
@user.role.should == :admin
|
57
|
+
@user.has_role?(:admin).should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should set user role to :admin using role=" do
|
61
|
+
@user.roles = :admin
|
62
|
+
@user.role.should == :admin
|
63
|
+
@user.has_role?(:admin).should be_true
|
64
|
+
end
|
70
65
|
end
|
71
66
|
end
|
@@ -4,11 +4,11 @@ use_roles_strategy :role_strings
|
|
4
4
|
class User
|
5
5
|
include Roles::Generic
|
6
6
|
|
7
|
-
attr_accessor :name
|
7
|
+
attr_accessor :name
|
8
8
|
|
9
|
-
|
9
|
+
strategy :role_strings, :default
|
10
10
|
|
11
|
-
|
11
|
+
valid_roles_are :admin, :user
|
12
12
|
|
13
13
|
def initialize name, *new_roles
|
14
14
|
self.name = name
|
@@ -4,11 +4,11 @@ use_roles_strategy :roles_mask
|
|
4
4
|
class User
|
5
5
|
include Roles::Generic
|
6
6
|
|
7
|
-
attr_accessor :name
|
7
|
+
attr_accessor :name
|
8
8
|
|
9
|
-
|
9
|
+
strategy :roles_mask, :default
|
10
10
|
|
11
|
-
|
11
|
+
valid_roles_are :admin, :user
|
12
12
|
|
13
13
|
def initialize name, *new_roles
|
14
14
|
self.name = name
|
@@ -4,11 +4,11 @@ use_roles_strategy :roles_string
|
|
4
4
|
class User
|
5
5
|
include Roles::Generic
|
6
6
|
|
7
|
-
attr_accessor :name
|
7
|
+
attr_accessor :name
|
8
8
|
|
9
|
-
|
9
|
+
strategy :roles_string, :default
|
10
10
|
|
11
|
-
|
11
|
+
valid_roles_are :admin, :user
|
12
12
|
|
13
13
|
def initialize name, *new_roles
|
14
14
|
self.name = name
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kristian Mandrup
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-06 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -136,6 +136,7 @@ files:
|
|
136
136
|
- lib/roles_generic/generic/user/configuration.rb
|
137
137
|
- lib/roles_generic/generic/user/implementation.rb
|
138
138
|
- lib/roles_generic/namespaces.rb
|
139
|
+
- lib/roles_generic/role.rb
|
139
140
|
- lib/roles_generic/strategy.rb
|
140
141
|
- lib/roles_generic/strategy/multi/many_roles.rb
|
141
142
|
- lib/roles_generic/strategy/multi/role_strings.rb
|
@@ -153,7 +154,6 @@ files:
|
|
153
154
|
- spec/generators/role_strings_generator_spec.rb
|
154
155
|
- spec/generators/roles_mask_generator_spec.rb
|
155
156
|
- spec/generators/roles_string_generator_spec.rb
|
156
|
-
- spec/model/role.rb
|
157
157
|
- spec/roles_generic/admin_flag_spec.rb
|
158
158
|
- spec/roles_generic/many_roles_spec.rb
|
159
159
|
- spec/roles_generic/one_role_spec.rb
|
@@ -210,7 +210,6 @@ test_files:
|
|
210
210
|
- spec/generators/role_strings_generator_spec.rb
|
211
211
|
- spec/generators/roles_mask_generator_spec.rb
|
212
212
|
- spec/generators/roles_string_generator_spec.rb
|
213
|
-
- spec/model/role.rb
|
214
213
|
- spec/roles_generic/admin_flag_spec.rb
|
215
214
|
- spec/roles_generic/many_roles_spec.rb
|
216
215
|
- spec/roles_generic/one_role_spec.rb
|