roles_active_record 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
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, roles_string, role_strings, one_role, many_roles, roles_mask)"
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 "Model #{name} not found"
32
- say "Model #{name} not found"
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}, #{strategy_options}"
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
@@ -4,4 +4,6 @@ class <%= role_class %> < ActiveRecord::Base
4
4
  has_many :<%= user_role_class.pluralize.underscore %>
5
5
 
6
6
  validates :name, :uniqueness => true
7
+
8
+ extend RoleClass::ClassMethods
7
9
  end
@@ -3,4 +3,6 @@ class <%= role_class %> < ActiveRecord::Base
3
3
  has_many :<%= user_class.pluralize.underscore %>
4
4
 
5
5
  validates :name, :uniqueness => true
6
+
7
+ extend RoleClass::ClassMethods
6
8
  end
@@ -18,7 +18,7 @@ module Roles::ActiveRecord
18
18
  end
19
19
 
20
20
  def valid_multi_strategies
21
- [:many_roles, :roles_mask, :role_strings]
21
+ [:many_roles, :roles_mask]
22
22
  end
23
23
 
24
24
  def strategies_with_role_class
@@ -3,6 +3,8 @@ class Role < ActiveRecord::Base
3
3
  has_many :users, :through => :user_roles
4
4
  has_many :user_roles
5
5
  validates :name, :uniqueness => true
6
+
7
+ extend RoleClass::ClassMethods
6
8
  end
7
9
 
8
10
  class UserRole < ActiveRecord::Base
@@ -2,4 +2,6 @@ class Role < ActiveRecord::Base
2
2
  scope :named, lambda{|role_names| where(:name.in => role_names.flatten)}
3
3
  has_many :users
4
4
  validates :name, :uniqueness => true
5
+
6
+ extend RoleClass::ClassMethods
5
7
  end
@@ -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
- class Role < ActiveRecord::Base
17
- class << self
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
@@ -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.2"
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{2010-12-26}
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 = [
@@ -1,8 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
  include Roles::ActiveRecord
3
3
 
4
- strategy :many_roles, :default
5
- role_class :role
6
-
4
+ strategy :many_roles
7
5
  valid_roles_are :admin, :guest, :user
8
6
  end
@@ -1,8 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
  include Roles::ActiveRecord
3
3
 
4
- strategy :many_roles, :default
5
- role_class :role
6
-
4
+ strategy :many_roles
7
5
  valid_roles_are :admin, :guest, :user, :guest
8
6
  end
@@ -1,8 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
  include Roles::ActiveRecord
3
3
 
4
- strategy :one_role, :default
5
- # role_class :role
6
-
4
+ strategy :one_role
7
5
  valid_roles_are :admin, :guest, :user
8
6
  end
@@ -1,8 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
  include Roles::ActiveRecord
3
3
 
4
- strategy :one_role, :default
5
- role_class :role
6
-
4
+ strategy :one_role
7
5
  valid_roles_are :admin, :guest, :user, :guest
8
6
  end
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Role < ActiveRecord::Base
4
+ end
5
+
3
6
  def api_migrate
4
7
  migrate('many_roles')
5
8
  end
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Role < ActiveRecord::Base
4
+ end
5
+
3
6
  def api_fixture
4
7
  load 'fixtures/one_role_setup.rb'
5
8
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 2
9
- version: 0.4.2
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: 2010-12-26 00:00:00 +01:00
17
+ date: 2011-01-01 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency