roles_active_record 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/generators/active_record/roles/roles_generator.rb +38 -11
- data/lib/generators/active_record/roles/templates/many_roles/role.rb +2 -0
- data/lib/generators/active_record/roles/templates/one_role/role.rb +2 -0
- data/lib/roles_active_record/base.rb +1 -1
- data/lib/roles_active_record/many_roles.rb +2 -0
- data/lib/roles_active_record/one_role.rb +2 -0
- data/lib/roles_active_record/role.rb +5 -5
- data/roles_active_record.gemspec +2 -2
- data/spec/fixtures/many_roles_setup.rb +1 -3
- data/spec/fixtures/many_roles_setup_unique_check.rb +1 -3
- data/spec/fixtures/one_role_setup.rb +1 -3
- data/spec/fixtures/one_role_setup_unique_check.rb +1 -3
- data/spec/roles_active_record/strategy/multi/many_roles_spec.rb +3 -0
- data/spec/roles_active_record/strategy/single/one_role_spec.rb +3 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.3
|
@@ -9,10 +9,11 @@ module ActiveRecord
|
|
9
9
|
# argument name
|
10
10
|
|
11
11
|
class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
|
12
|
-
:desc => "Role strategy to use (admin_flag, role_string,
|
12
|
+
:desc => "Role strategy to use (admin_flag, role_string, one_role, many_roles, roles_mask)"
|
13
13
|
|
14
14
|
class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
|
15
15
|
class_option :role_class, :type => :string, :aliases => "-rc", :default => 'Role', :desc => "Role class"
|
16
|
+
class_option :user_class, :type => :string, :aliases => "-uc", :default => 'User', :desc => "User class"
|
16
17
|
class_option :user_role_class, :type => :string, :aliases => "-urc", :default => 'UserRole', :desc => "User Role join class"
|
17
18
|
|
18
19
|
class_option :default_roles, :type => :boolean, :default => true, :desc => "Use default roles :admin and :base"
|
@@ -23,15 +24,34 @@ module ActiveRecord
|
|
23
24
|
def apply_role_strategy
|
24
25
|
logger.add_logfile :logfile => logfile if logfile
|
25
26
|
logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
|
27
|
+
|
28
|
+
if !valid_strategy?
|
29
|
+
say "Strategy '#{strategy}' is not valid, at least not for Active Record"
|
30
|
+
return
|
31
|
+
end
|
32
|
+
|
33
|
+
if !has_model? :user
|
34
|
+
say "Could not apply roles strategy to #{name} model since the model file was not found"
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
26
38
|
begin
|
27
39
|
insert_into_model name do
|
28
40
|
insertion_text
|
29
41
|
end
|
42
|
+
|
43
|
+
copy_role_models if roles_model_strategy?
|
30
44
|
rescue
|
31
|
-
logger.debug "
|
32
|
-
say "
|
45
|
+
# logger.debug "Error applying roles strategy to #{name}"
|
46
|
+
say "Error applying roles strategy to #{name}"
|
33
47
|
end
|
34
48
|
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
extend Rails3::Assist::UseMacro
|
53
|
+
use_orm :active_record
|
54
|
+
include Rails3::Assist::BasicLogger
|
35
55
|
|
36
56
|
def copy_role_models
|
37
57
|
logger.debug 'copy_role_models'
|
@@ -42,12 +62,6 @@ module ActiveRecord
|
|
42
62
|
copy_many_roles_models
|
43
63
|
end
|
44
64
|
end
|
45
|
-
|
46
|
-
protected
|
47
|
-
|
48
|
-
extend Rails3::Assist::UseMacro
|
49
|
-
use_orm :active_record
|
50
|
-
include Rails3::Assist::BasicLogger
|
51
65
|
|
52
66
|
def copy_one_role_model
|
53
67
|
logger.debug "copy_one_role_model: #{role_class.underscore}"
|
@@ -62,6 +76,18 @@ module ActiveRecord
|
|
62
76
|
template 'many_roles/user_role.rb', "app/models/#{user_role_class.underscore}.rb"
|
63
77
|
end
|
64
78
|
|
79
|
+
def valid_strategy?
|
80
|
+
valid_strategies.include? strategy.to_sym
|
81
|
+
end
|
82
|
+
|
83
|
+
def valid_strategies
|
84
|
+
[:admin_flag, :role_string, :one_role, :many_roles, :roles_mask]
|
85
|
+
end
|
86
|
+
|
87
|
+
def roles_model_strategy?
|
88
|
+
[:one_role, :many_roles].include? strategy.to_sym
|
89
|
+
end
|
90
|
+
|
65
91
|
def user_class
|
66
92
|
options[:user_class].classify || 'User'
|
67
93
|
end
|
@@ -108,13 +134,14 @@ module ActiveRecord
|
|
108
134
|
end
|
109
135
|
|
110
136
|
def role_strategy_statement
|
111
|
-
"strategy :#{strategy}
|
137
|
+
"strategy :#{strategy} #{strategy_options}"
|
112
138
|
end
|
113
139
|
|
114
140
|
def strategy_options
|
115
141
|
if role_class != 'Role' || user_role_class != 'UserRole' && role_ref_strategy?
|
116
|
-
return ":role_class => :#{options[:role_class] || 'role'}, :user_role_class => :#{options[:user_role_class] || 'user_role'}"
|
142
|
+
return ", :role_class => :#{options[:role_class] || 'role'}, :user_role_class => :#{options[:user_role_class] || 'user_role'}"
|
117
143
|
end
|
144
|
+
''
|
118
145
|
end
|
119
146
|
|
120
147
|
def insertion_text
|
@@ -10,11 +10,11 @@ module Roles::Base
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
module RoleClass
|
17
|
+
module ClassMethods
|
18
18
|
def find_roles(*role_names)
|
19
19
|
where(:name.in => role_names.flatten)
|
20
20
|
end
|
@@ -23,6 +23,6 @@ class Role < ActiveRecord::Base
|
|
23
23
|
raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
|
24
24
|
res = find_roles(role_name)
|
25
25
|
res ? res.first : res
|
26
|
-
end
|
26
|
+
end
|
27
27
|
end
|
28
|
-
end
|
28
|
+
end
|
data/roles_active_record.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{roles_active_record}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.3"
|
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{
|
12
|
+
s.date = %q{2011-01-01}
|
13
13
|
s.description = %q{Makes it easy to set a role strategy on your User model in Active Record}
|
14
14
|
s.email = %q{kmandrup@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 3
|
9
|
+
version: 0.4.3
|
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:
|
17
|
+
date: 2011-01-01 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|