roles_mongo_mapper 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -9,13 +9,19 @@ module MongoMapper
9
9
  class_option :strategy, :type => :string, :default => 'role_string',
10
10
  :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
11
11
 
12
- class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
13
- class_option :roles, :type => :array, :default => [], :desc => "Valid roles"
12
+ class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
13
+ class_option :roles, :type => :array, :default => [], :desc => "Valid roles"
14
+ class_option :role_class, :type => :string, :aliases => "-rc", :default => 'Role', :desc => "Role class"
14
15
 
15
16
  def apply_role_strategy
16
17
  logger.add_logfile :logfile => logfile if logfile
17
18
  logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
18
19
 
20
+ if !valid_strategy?
21
+ say "Strategy '#{strategy}' is not valid. The valid strategies are: #{valid_strategies.join(', ')}", :red
22
+ return
23
+ end
24
+
19
25
  if !has_model_file?(user_model_name)
20
26
  say "User model #{user_model_name} not found", :red
21
27
  return
@@ -25,13 +31,11 @@ module MongoMapper
25
31
  insert_into_model user_model_name, :after => /include MongoMapper::\w+/ do
26
32
  insertion_text
27
33
  end
28
-
29
- unless read_model(:user) =~ /use_roles_strategy/
30
- inject_into_file model_file(:user), "use_roles_strategy :#{strategy}\n\n", :before => "class"
31
- end
32
34
  rescue Exception => e
33
35
  logger.debug"Error: #{e.message}"
34
- end
36
+ end
37
+
38
+ copy_role_class if role_class_strategy?
35
39
  end
36
40
 
37
41
  protected
@@ -41,6 +45,26 @@ module MongoMapper
41
45
 
42
46
  use_orm :mongo_mapper
43
47
 
48
+ def role_class_strategy?
49
+ # :embed_one_role, :embed_many_roles
50
+ [:one_role, :many_roles].include? strategy.to_sym
51
+ end
52
+
53
+ def valid_strategy?
54
+ valid_strategies.include? strategy.to_sym
55
+ end
56
+
57
+ def valid_strategies
58
+ # :embed_many_roles, :embed_one_role,
59
+ [:admin_flag, :one_role, :role_string, :many_roles, :role_strings, :roles_mask]
60
+ end
61
+
62
+
63
+ def copy_role_class
64
+ logger.debug "copy_role_class: #{role_class.underscore}"
65
+ template 'role.rb', "app/models/#{role_class.underscore}.rb"
66
+ end
67
+
44
68
  def logfile
45
69
  options[:logfile]
46
70
  end
@@ -65,19 +89,24 @@ module MongoMapper
65
89
  roles_to_add.map{|r| ":#{r}" }
66
90
  end
67
91
 
68
- def role_strategy_statement
69
- "strategy :#{strategy}, :default\n#{role_class_stmt}"
92
+ def role_class
93
+ options[:role_class].classify || 'Role'
70
94
  end
71
95
 
72
- def role_class_stmt
73
- " role_class :role" if [:one_role, :many_roles].include? (strategy.to_sym)
96
+ def role_strategy_statement
97
+ "strategy :#{strategy} #{strategy_options}"
74
98
  end
75
99
 
76
- def roles_statement
100
+ def valid_roles_statement
77
101
  return '' if has_valid_roles_statement?
78
102
  roles ? "valid_roles_are #{roles.join(', ')}" : ''
79
103
  end
80
104
 
105
+ def strategy_options
106
+ return ", :role_class => :#{role_class.to_s.underscore}" if role_class_strategy? && role_class.to_s != 'Role'
107
+ ''
108
+ end
109
+
81
110
  def has_valid_roles_statement?
82
111
  !(read_model(user_model_name) =~ /valid_roles_are/).nil?
83
112
  end
@@ -85,7 +114,7 @@ module MongoMapper
85
114
  def insertion_text
86
115
  %Q{include Roles::#{orm.to_s.camelize}
87
116
  #{role_strategy_statement}
88
- #{roles_statement}}
117
+ #{valid_roles_statement}}
89
118
  end
90
119
 
91
120
  def strategy
@@ -0,0 +1,11 @@
1
+ class <%= role_class %>
2
+ include MongoMapper::Document
3
+ key :name, String
4
+
5
+ validates_uniqueness_of :name
6
+
7
+ scope :by_name, lambda { |name| where(:name => name.to_s) }
8
+ scope :by_names, lambda { |*names| where(:name => names.to_strings) }
9
+
10
+ extend RoleClass::ClassMethods
11
+ end
@@ -15,9 +15,7 @@ module Roles::MongoMapper
15
15
 
16
16
  MAP = {
17
17
  :admin_flag => "key :admin_flag, Boolean",
18
- # :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role', :default => []",
19
- # :one_role => "references_one :one_role, :class_name => 'Role'",
20
-
18
+
21
19
  # :embed_many_roles => "many :many_roles, :class_name => 'Role'",
22
20
  # :embed_one_role => "one :one_role, :class_name => 'Role'",
23
21
 
@@ -44,18 +42,27 @@ module Roles::MongoMapper
44
42
  raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
45
43
  use_roles_strategy strategy_name
46
44
 
47
- if strategies_with_role_class.include? strategy_name
48
- if !options.kind_of? Symbol
49
- @role_class_name = get_role_class(strategy_name, options)
50
- else
51
- @role_class_name = default_role_class(strategy_name)
52
- end
53
- end
45
+ set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
54
46
 
55
47
  if default_options?(options) && MAP[strategy_name]
56
48
  instance_eval statement(MAP[strategy_name])
57
49
  end
58
50
 
51
+ # one_role reference
52
+ if strategy_name == :one_role
53
+ key :one_role_id, ObjectId
54
+ key :one_role, @role_class_name
55
+ end
56
+
57
+ # many_roles references
58
+ if strategy_name == :many_roles
59
+ instance_eval %{
60
+ key :many_roles_ids, Array, :typecast => 'ObjectId'
61
+ many :many_roles, :class_name => '#{@role_class_name}', :in => :many_roles_ids
62
+ ensure_index :many_role_ids
63
+ }
64
+ end
65
+
59
66
  # case name
60
67
  # when :embed_one_role
61
68
  # raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !@role_class_name
@@ -70,6 +77,10 @@ module Roles::MongoMapper
70
77
 
71
78
  private
72
79
 
80
+ def set_role_class strategy_name, options = {}
81
+ @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
82
+ end
83
+
73
84
  def default_options? options = {}
74
85
  return true if options == :default
75
86
  if options.kind_of? Hash
@@ -84,7 +95,7 @@ module Roles::MongoMapper
84
95
 
85
96
  def default_role_class strategy_name
86
97
  if defined? ::Role
87
- require "roles_mongo_mapper/role"
98
+ require "roles_mongo_mapper/role_class"
88
99
  return ::Role
89
100
  end
90
101
  raise "Default Role class not defined"
@@ -9,19 +9,11 @@ module Roles::Base
9
9
  end
10
10
  end
11
11
  end
12
- end
12
+ end
13
13
  end
14
14
 
15
- class Role
16
- include MongoMapper::Document
17
- key :name, String
18
-
19
- validates_uniqueness_of :name
20
-
21
- scope :by_name, lambda { |name| where(:name => name.to_s) }
22
- scope :by_names, lambda { |*names| where(:name => names.to_strings) }
23
-
24
- class << self
15
+ module RoleClass
16
+ module ClassMethods
25
17
  def find_roles(*role_names)
26
18
  role_names.flatten!
27
19
  by_names(role_names).all
@@ -33,6 +25,4 @@ class Role
33
25
  by_name(role_name).first
34
26
  end
35
27
  end
36
- end
37
-
38
-
28
+ end
@@ -0,0 +1,13 @@
1
+ class Role
2
+ include MongoMapper::Document
3
+ key :name, String
4
+
5
+ validates_uniqueness_of :name
6
+
7
+ scope :by_name, lambda { |name| where(:name => name.to_s) }
8
+ scope :by_names, lambda { |*names| where(:name => names.to_strings) }
9
+
10
+ extend RoleClass::ClassMethods
11
+ end
12
+
13
+
@@ -11,9 +11,9 @@ module RoleStrategy::MongoMapper
11
11
  base.extend ClassMethods
12
12
 
13
13
  # TODO - refactor into strategy method in base.rb
14
- base.key :many_roles_ids, Array, :typecast => 'ObjectId'
15
- base.many :many_roles, :class_name => 'Role', :in => :many_roles_ids
16
- base.ensure_index :many_role_ids
14
+ # base.key :many_roles_ids, Array, :typecast => 'ObjectId'
15
+ # base.many :many_roles, :class_name => 'Role', :in => :many_roles_ids
16
+ # base.ensure_index :many_role_ids
17
17
  end
18
18
 
19
19
  module ClassMethods
@@ -9,8 +9,8 @@ module RoleStrategy::MongoMapper
9
9
  def self.included base
10
10
  base.extend Roles::Generic::Role::ClassMethods
11
11
  base.extend ClassMethods
12
- base.key :one_role_id, ObjectId
13
- base.key :one_role, Role
12
+ # base.key :one_role_id, ObjectId
13
+ # base.key :one_role, Role
14
14
  end
15
15
 
16
16
  module ClassMethods
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongo_mapper}
8
- s.version = "0.3.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-27}
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 MongoMapper}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,11 +24,13 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/generators/mongo_mapper/roles/roles_generator.rb",
27
+ "lib/generators/mongo_mapper/roles/templates/role.rb",
27
28
  "lib/roles_mongo_mapper.rb",
28
29
  "lib/roles_mongo_mapper/base.rb",
29
30
  "lib/roles_mongo_mapper/embedded_role.rb",
30
31
  "lib/roles_mongo_mapper/namespaces.rb",
31
32
  "lib/roles_mongo_mapper/role.rb",
33
+ "lib/roles_mongo_mapper/role_class.rb",
32
34
  "lib/roles_mongo_mapper/strategy.rb",
33
35
  "lib/roles_mongo_mapper/strategy/multi.rb",
34
36
  "lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb",
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Role
4
+ end
5
+
3
6
  class User
4
7
  include MongoMapper::Document
5
8
  include Roles::MongoMapper
@@ -1,4 +1,8 @@
1
1
  require 'spec_helper'
2
+
3
+ class Role
4
+ end
5
+
2
6
  class User
3
7
  include MongoMapper::Document
4
8
  include Roles::MongoMapper
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.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-12-27 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
@@ -167,11 +167,13 @@ files:
167
167
  - Rakefile
168
168
  - VERSION
169
169
  - lib/generators/mongo_mapper/roles/roles_generator.rb
170
+ - lib/generators/mongo_mapper/roles/templates/role.rb
170
171
  - lib/roles_mongo_mapper.rb
171
172
  - lib/roles_mongo_mapper/base.rb
172
173
  - lib/roles_mongo_mapper/embedded_role.rb
173
174
  - lib/roles_mongo_mapper/namespaces.rb
174
175
  - lib/roles_mongo_mapper/role.rb
176
+ - lib/roles_mongo_mapper/role_class.rb
175
177
  - lib/roles_mongo_mapper/strategy.rb
176
178
  - lib/roles_mongo_mapper/strategy/multi.rb
177
179
  - lib/roles_mongo_mapper/strategy/multi/embed_many_roles.rb