roles_mongoid 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  A Mongoid implementation of [roles generic](http://github.com/kristianmandrup/roles_generic)
4
4
 
5
+ *Update Dec 21, 2010*
6
+
7
+ New embedded role strategies added with specs
8
+
9
+ * embed_one_role
10
+ * embed_many_roles
11
+
5
12
  ## Intro
6
13
 
7
14
  Implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
@@ -9,20 +16,16 @@ Implements the [roles generic](http://github.com/kristianmandrup/roles_generic)
9
16
  Generic Role strategies implemented:
10
17
 
11
18
  * admin_flag
12
- * many_roles
13
- * one_role
14
- * roles_mask
15
- * role_string
16
- * role_strings
17
19
 
18
- *Update Nov 24, 2010*
19
- Version 0.3.1 and up is a major refactoring to support Roles Generic 0.3 and above with a new and improved architecture and testing framework.
20
- Since 0.3.2 development of some basic infrastructure to support embedded role strategies in the future has been initiated.
20
+ * many_roles)
21
+ * one_role
21
22
 
22
- * embed_one_role
23
23
  * embed_many_roles
24
+ * embed_one_role
24
25
 
25
- Please join in the effort to implement and add these strategies to the mix.
26
+ * roles_mask
27
+ * role_string
28
+ * role_strings
26
29
 
27
30
  # Install
28
31
 
@@ -35,8 +38,13 @@ The library comes with a Rails 3 generator that lets you populate a user model w
35
38
  The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
36
39
 
37
40
  * admin_flag
41
+
38
42
  * many_roles
39
- * one_role
43
+ * one_role)
44
+
45
+ * embed_many_roles
46
+ * embed_one_role
47
+
40
48
  * roles_mask
41
49
  * role_string
42
50
  * role_strings
data/Rakefile CHANGED
@@ -9,12 +9,12 @@ begin
9
9
  gem.authors = ["Kristian Mandrup"]
10
10
  gem.add_development_dependency "rspec", '>= 2.0.1'
11
11
 
12
- gem.add_dependency "mongoid", '>= 2.0.0.beta.19'
12
+ gem.add_dependency "mongoid", '>= 2.0.0.beta.20'
13
13
  gem.add_dependency 'bson', '>= 1.1.4'
14
14
 
15
- gem.add_dependency "sugar-high", "~> 0.3.0"
16
- gem.add_dependency "require_all", '~> 1.2.0'
17
- gem.add_dependency "roles_generic", '>= 0.3.0'
15
+ gem.add_dependency "sugar-high", "~> 0.3.1"
16
+ gem.add_dependency "require_all", '~> 1.2.0'
17
+ gem.add_dependency "roles_generic", '>= 0.3.3'
18
18
 
19
19
  gem.add_dependency 'rails3_artifactor', '>= 0.3.1'
20
20
  gem.add_dependency 'logging_assist', '~> 0.1.6'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.3.6
@@ -9,7 +9,7 @@ module Mongoid
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, roles_string, role_strings, roles_mask, one_role, many_roles, embed_one_role, embed_many_roles)"
13
13
 
14
14
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
15
15
  class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
@@ -18,6 +18,10 @@ module Mongoid
18
18
  logger.add_logfile :logfile => logfile if logfile
19
19
  logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
20
20
 
21
+ if !valid_strategy?
22
+ say "Strategy #{strategy} is not currently supported, please try one of #{valid_strategies.join(', ')}", :red
23
+ end
24
+
21
25
  if !has_model_file?(user_model_name)
22
26
  say "User model #{user_model_name} not found", :red
23
27
  return
@@ -43,6 +47,14 @@ module Mongoid
43
47
 
44
48
  use_orm :mongoid
45
49
 
50
+ def valid_strategy?
51
+ valid_strategies.include? strategy.to_sym
52
+ end
53
+
54
+ def valid_strategies
55
+ [:admin_flag, :one_role, :embed_one_role, :role_string, :embed_many_roles, :many_roles, :role_strings, :roles_mask]
56
+ end
57
+
46
58
  def logfile
47
59
  options[:logfile]
48
60
  end
@@ -67,8 +79,17 @@ module Mongoid
67
79
  roles_to_add.map{|r| ":#{r}" }
68
80
  end
69
81
 
70
- def role_strategy_statement
71
- "strategy :#{strategy}, :default\n#{role_class_stmt}"
82
+ def role_strategy_statement
83
+ "strategy :#{strategy}, #{strategy_option_arg}"
84
+ end
85
+
86
+ def strategy_option_arg
87
+ case strategy
88
+ when 'embed_one_role', 'embed_many_roles'
89
+ ":role_class => :role, :config => :default"
90
+ else
91
+ ":default\n#{role_class_stmt}"
92
+ end
72
93
  end
73
94
 
74
95
  def role_class_stmt
@@ -1,3 +1,5 @@
1
+ puts "Embedded Role"
2
+
1
3
  module Roles::Base
2
4
  def valid_roles_are(*role_list)
3
5
  strategy_class.valid_roles = role_list.to_symbols
@@ -7,6 +9,8 @@ end
7
9
  class Role
8
10
  include Mongoid::Document
9
11
  field :name, :type => String
12
+
13
+ validates_uniqueness_of :name
10
14
 
11
15
  class << self
12
16
  def find_roles(*role_names)
@@ -15,9 +15,6 @@ end
15
15
  class Role
16
16
  include Mongoid::Document
17
17
  field :name, :type => String
18
- # references_many :users
19
-
20
- # references_many :users, :class_name => 'User', :stored_as => :array
21
18
 
22
19
  validates_uniqueness_of :name
23
20
 
@@ -22,8 +22,7 @@ module RoleStrategy::Mongoid
22
22
 
23
23
  def in_any_role(*role_names)
24
24
  begin
25
- role_ids = Role.where(:name.in => role_names.to_strings).to_a.map(&:_id)
26
- where(:"#{role_attribute}".in => role_ids).to_a
25
+ any_in("many_roles.name" => role_names)
27
26
  rescue
28
27
  return []
29
28
  end
@@ -33,6 +32,48 @@ module RoleStrategy::Mongoid
33
32
  module Implementation
34
33
  include Roles::Mongoid::Strategy::Multi
35
34
 
35
+ def set_role role
36
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
37
+ self.send("#{role_attribute}=", nil)
38
+ self.send("create_#{role_attribute}").create(:name => role)
39
+ end
40
+
41
+ def set_roles *roles
42
+ roles = roles.flat_uniq
43
+ self.send("#{role_attribute}=", nil)
44
+ roles.each do |role|
45
+ self.send("#{role_attribute}").create(:name => role)
46
+ end
47
+ end
48
+
49
+ def roles_diff *roles
50
+ self.roles_list - extract_roles(roles.flat_uniq)
51
+ end
52
+
53
+ def remove_roles *role_names
54
+ role_names = role_names.flat_uniq
55
+ set_empty_roles and return if roles_diff(role_names).empty?
56
+ roles_to_remove = select_valid_roles(role_names)
57
+
58
+ diff = roles_diff(role_names)
59
+ set_roles(diff)
60
+ true
61
+ end
62
+
63
+ def exchange_roles *role_names
64
+ options = last_option role_names
65
+ raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if !options || !options.kind_of?(Hash)
66
+ common = role_names.to_symbols & roles_list
67
+ if !common.empty?
68
+ with_roles = options[:with]
69
+ set_roles with_roles
70
+ end
71
+ end
72
+
73
+ def select_valid_roles *role_names
74
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
75
+ end
76
+
36
77
  def has_roles?(*roles_names)
37
78
  compare_roles = extract_roles(roles_names.flat_uniq)
38
79
  (roles_list & compare_roles).not.empty?
@@ -59,7 +100,7 @@ module RoleStrategy::Mongoid
59
100
  role_names = role_names.flat_uniq
60
101
  role_names = extract_roles(role_names)
61
102
  return nil if role_names.empty?
62
- valids = role_class.find_roles(role_names).to_a
103
+
63
104
  vrs = select_valid_roles role_names
64
105
  set_roles(vrs)
65
106
  end
@@ -3,6 +3,7 @@ module Roles::Mongoid
3
3
  module Shared
4
4
  def set_role role
5
5
  raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
6
+ puts "role: #{role.inspect}, attrib: #{role_attribute.inspect}"
6
7
  self.send("#{role_attribute}=", role)
7
8
  end
8
9
 
@@ -25,10 +25,8 @@ module RoleStrategy::Mongoid
25
25
  end
26
26
 
27
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)
28
+ begin
29
+ any_in("one_role.name" => role_names)
32
30
  rescue
33
31
  return []
34
32
  end
@@ -37,9 +35,29 @@ module RoleStrategy::Mongoid
37
35
 
38
36
  module Implementation
39
37
  include Roles::Mongoid::Strategy::Single
38
+
39
+ def set_role role
40
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
41
+ self.send("create_#{role_attribute}", :name => role)
42
+ end
43
+
44
+ def set_roles *roles
45
+ roles = roles.flat_uniq
46
+ self.send("create_#{role_attribute}", :name => roles.first)
47
+ end
48
+
49
+ def exchange_roles *role_names
50
+ options = last_option role_names
51
+ raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if !options || !options.kind_of?(Hash)
52
+ common = role_names.to_symbols & roles_list
53
+ if !common.empty?
54
+ with_roles = options[:with]
55
+ set_role with_roles
56
+ end
57
+ end
40
58
 
41
59
  def new_role role
42
- role_class.find_role(extract_role role)
60
+ role_class.find_role(extract_role role)
43
61
  end
44
62
 
45
63
  def new_roles *roles
@@ -55,8 +73,7 @@ module RoleStrategy::Mongoid
55
73
  end
56
74
 
57
75
  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
76
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
60
77
  end
61
78
  end
62
79
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongoid}
8
- s.version = "0.3.5"
8
+ s.version = "0.3.6"
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"]
@@ -95,11 +95,11 @@ Gem::Specification.new do |s|
95
95
  s.add_runtime_dependency(%q<sugar-high>, [">= 0.3.0"])
96
96
  s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.2"])
97
97
  s.add_development_dependency(%q<rspec>, [">= 2.0.1"])
98
- s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
98
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
99
99
  s.add_runtime_dependency(%q<bson>, [">= 1.1.4"])
100
- s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.0"])
100
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.1"])
101
101
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
102
- s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.0"])
102
+ s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.3"])
103
103
  s.add_runtime_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
104
104
  s.add_runtime_dependency(%q<logging_assist>, ["~> 0.1.6"])
105
105
  else
@@ -110,11 +110,11 @@ Gem::Specification.new do |s|
110
110
  s.add_dependency(%q<sugar-high>, [">= 0.3.0"])
111
111
  s.add_dependency(%q<roles_generic>, [">= 0.3.2"])
112
112
  s.add_dependency(%q<rspec>, [">= 2.0.1"])
113
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
113
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
114
114
  s.add_dependency(%q<bson>, [">= 1.1.4"])
115
- s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
115
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.1"])
116
116
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
117
- s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
117
+ s.add_dependency(%q<roles_generic>, [">= 0.3.3"])
118
118
  s.add_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
119
119
  s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
120
120
  end
@@ -126,11 +126,11 @@ Gem::Specification.new do |s|
126
126
  s.add_dependency(%q<sugar-high>, [">= 0.3.0"])
127
127
  s.add_dependency(%q<roles_generic>, [">= 0.3.2"])
128
128
  s.add_dependency(%q<rspec>, [">= 2.0.1"])
129
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.19"])
129
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
130
130
  s.add_dependency(%q<bson>, [">= 1.1.4"])
131
- s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
131
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.1"])
132
132
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
133
- s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
133
+ s.add_dependency(%q<roles_generic>, [">= 0.3.3"])
134
134
  s.add_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
135
135
  s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
136
136
  end
@@ -6,8 +6,7 @@ class User
6
6
  include Mongoid::Document
7
7
  include Roles::Mongoid
8
8
 
9
- strategy :embed_many_roles, :default
10
- role_class :role
9
+ strategy :embed_many_roles, :role_class => :role, :config => :default
11
10
 
12
11
  valid_roles_are :admin, :guest, :user
13
12
  end
@@ -17,3 +17,4 @@ def api_name
17
17
  end
18
18
 
19
19
  load 'roles_mongoid/strategy/api_examples.rb'
20
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 5
9
- version: 0.3.5
8
+ - 6
9
+ version: 0.3.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -136,8 +136,8 @@ dependencies:
136
136
  - 0
137
137
  - 0
138
138
  - beta
139
- - 19
140
- version: 2.0.0.beta.19
139
+ - 20
140
+ version: 2.0.0.beta.20
141
141
  type: :runtime
142
142
  prerelease: false
143
143
  version_requirements: *id008
@@ -166,8 +166,8 @@ dependencies:
166
166
  segments:
167
167
  - 0
168
168
  - 3
169
- - 0
170
- version: 0.3.0
169
+ - 1
170
+ version: 0.3.1
171
171
  type: :runtime
172
172
  prerelease: false
173
173
  version_requirements: *id010
@@ -196,8 +196,8 @@ dependencies:
196
196
  segments:
197
197
  - 0
198
198
  - 3
199
- - 0
200
- version: 0.3.0
199
+ - 3
200
+ version: 0.3.3
201
201
  type: :runtime
202
202
  prerelease: false
203
203
  version_requirements: *id012