roles_mongoid 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -5,7 +5,8 @@ A Mongoid implementation of [roles generic](http://github.com/kristianmandrup/ro
5
5
  ## Intro
6
6
 
7
7
  Implements the [roles generic](http://github.com/kristianmandrup/roles_generic) Roles API
8
- It also implements the following Role strategies:
8
+
9
+ Generic Role strategies implemented:
9
10
 
10
11
  * admin_flag
11
12
  * many_roles
@@ -14,6 +15,15 @@ It also implements the following Role strategies:
14
15
  * role_string
15
16
  * role_strings
16
17
 
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.
21
+
22
+ * embed_one_role
23
+ * embed_many_roles
24
+
25
+ Please join in the effort to implement and add these strategies to the mix.
26
+
17
27
  # Install
18
28
 
19
29
  <code>gem install roles_mongoid</code>
@@ -43,7 +53,8 @@ Apply :admin_flag Role strategy to User model using default roles and extra role
43
53
 
44
54
  Apply :one_role Role strategy to User model without default roles, only with roles :user, :special and :editor
45
55
 
46
- <code>$ rails g mongoid:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
56
+ <code>$ rails g mongoid:roles_migration User --strategy one_role --roles user special editor --no-default-roles</code>
57
+
47
58
  ## Note on Patches/Pull Requests
48
59
 
49
60
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -20,16 +20,32 @@ module Roles
20
20
  :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role'",
21
21
  :one_role => "referenced_in :one_role, :class_name => 'Role'",
22
22
 
23
+ :embed_many_roles => "embeds_many :many_roles, :class_name => 'Role'",
24
+ :embed_one_role => "embeds_one :one_role, :class_name => 'Role'",
25
+
23
26
  :roles_mask => "field :roles_mask, :type => Integer, :default => 0",
24
27
  :role_string => "field :role_string, :type => String",
25
28
  :role_strings => "field :role_strings, :type => Array",
26
29
  :roles_string => "field :roles_string, :type => String"
27
30
  }
28
31
 
29
- def strategy name, options=nil
30
- if options == :default && MAP[name]
32
+ def strategy name, options = {}
33
+ if (options == :default || options[:config] == :default) && MAP[name]
31
34
  instance_eval MAP[name]
32
35
  end
36
+ if !options.kind_of? Symbol
37
+ role_class = options[:role_class] ? options[:role_class].to_s.camelize.constantize : (Role if defined? Role)
38
+ end
39
+
40
+ case name
41
+ when :embed_one_role
42
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
43
+ role_class.embedded_in :user, :inverse_of => :one_role
44
+ when :embed_many_roles
45
+ raise ArgumentError, "#strategy class method must take :role_class option when using an embedded role strategy" if !role_class
46
+ role_class.embedded_in :user, :inverse_of => :many_roles
47
+ end
48
+
33
49
  set_role_strategy name, options
34
50
  end
35
51
  end
@@ -0,0 +1,22 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ end
5
+ end
6
+
7
+ class Role
8
+ include Mongoid::Document
9
+ field :name, :type => String
10
+
11
+ class << self
12
+ def find_roles(*role_names)
13
+ where(:name.in => role_names.flatten).to_a
14
+ end
15
+
16
+ def find_role role_name
17
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
18
+ res = find_roles(role_name)
19
+ res ? res.first : res
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,83 @@
1
+ require 'roles_mongoid/strategy/multi'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module EmbedManyRoles
5
+ def self.default_role_attribute
6
+ :many_roles
7
+ end
8
+
9
+ def self.included base
10
+ base.extend Roles::Generic::Role::ClassMethods
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def role_attribute
16
+ "#{strategy_class.roles_attribute_name.to_s.singularize}_ids".to_sym
17
+ end
18
+
19
+ def in_role(role_name)
20
+ in_any_role(role_name)
21
+ end
22
+
23
+ def in_any_role(*role_names)
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
27
+ rescue
28
+ return []
29
+ end
30
+ end
31
+ end
32
+
33
+ module Implementation
34
+ include Roles::Mongoid::Strategy::Multi
35
+
36
+ def has_roles?(*roles_names)
37
+ compare_roles = extract_roles(roles_names.flat_uniq)
38
+ (roles_list & compare_roles).not.empty?
39
+ end
40
+
41
+ def get_roles
42
+ self.send(role_attribute)
43
+ end
44
+
45
+ def roles
46
+ get_roles.to_a.map do |role|
47
+ role.respond_to?(:sym) ? role.to_sym : role
48
+ end
49
+ end
50
+
51
+ def roles_list
52
+ my_roles = [roles].flat_uniq
53
+ return [] if my_roles.empty?
54
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
55
+ end
56
+
57
+ # assign multiple roles
58
+ def roles=(*role_names)
59
+ role_names = role_names.flat_uniq
60
+ role_names = extract_roles(role_names)
61
+ return nil if role_names.empty?
62
+ valids = role_class.find_roles(role_names).to_a
63
+ vrs = select_valid_roles role_names
64
+ set_roles(vrs)
65
+ end
66
+
67
+ def new_roles *role_names
68
+ role_class.find_roles(extract_roles role_names)
69
+ end
70
+
71
+ def present_roles roles_names
72
+ roles_names.to_a.map{|role| role.name.to_s.to_sym}
73
+ end
74
+
75
+ def set_empty_roles
76
+ self.send("#{role_attribute}=", [])
77
+ end
78
+ end
79
+
80
+ extend Roles::Generic::User::Configuration
81
+ configure :type => :role_class
82
+ end
83
+ end
@@ -46,32 +46,6 @@ module RoleStrategy::Mongoid
46
46
  def present_roles roles_names
47
47
  roles_names.to_a
48
48
  end
49
-
50
- # # assign roles
51
- # def roles=(*new_roles)
52
- # new_roles = new_roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
53
- # if new_roles && new_roles.not.empty?
54
- # self.send("#{role_attribute}=", new_roles.compact.uniq)
55
- # end
56
- # end
57
- # alias_method :role=, :roles=
58
- #
59
- # def add_roles(*roles_to_add)
60
- # new_roles = roles_to_add.flatten.map{|r| r.to_s if valid_role?(r)}.compact
61
- # if new_roles && new_roles.not.empty?
62
- # self.send(role_attribute) << new_roles.compact.uniq
63
- # end
64
- # end
65
- # alias_method :add_role
66
- #
67
- # # query assigned roles
68
- # def roles
69
- # self.send(role_attribute).map{|r| r.to_sym}
70
- # end
71
- #
72
- # def roles_list
73
- # [roles].flatten
74
- # end
75
49
  end
76
50
 
77
51
  extend Roles::Generic::User::Configuration
@@ -0,0 +1,66 @@
1
+ require 'roles_mongoid/strategy/single'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module EmbedOneRole
5
+ def self.default_role_attribute
6
+ :one_role
7
+ end
8
+
9
+ def self.included base
10
+ base.extend Roles::Generic::Role::ClassMethods
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def role_attribute
16
+ strategy_class.roles_attribute_name.to_sym
17
+ end
18
+
19
+ def role_attribute_id
20
+ "#{role_attribute}_id"
21
+ end
22
+
23
+ def in_role(role_name)
24
+ in_any_role(role_name)
25
+ end
26
+
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)
32
+ rescue
33
+ return []
34
+ end
35
+ end
36
+ end
37
+
38
+ module Implementation
39
+ include Roles::Mongoid::Strategy::Single
40
+
41
+ def new_role role
42
+ role_class.find_role(extract_role role)
43
+ end
44
+
45
+ def new_roles *roles
46
+ new_role roles.flatten.first
47
+ end
48
+
49
+ def present_roles *roles
50
+ roles.map{|role| extract_role role}
51
+ end
52
+
53
+ def set_empty_role
54
+ self.send("#{role_attribute}=", nil)
55
+ end
56
+
57
+ 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
60
+ end
61
+ end
62
+
63
+ extend Roles::Generic::User::Configuration
64
+ configure :num => :single, :type => :role_class
65
+ end
66
+ end
@@ -57,29 +57,7 @@ module RoleStrategy::Mongoid
57
57
  def select_valid_roles *role_names
58
58
  role_names = role_names.flat_uniq.select{|role| valid_role? role }
59
59
  has_role_class? ? role_class.find_roles(role_names).to_a : role_names
60
- end
61
-
62
- #
63
- # # assign roles
64
- # def roles=(*_roles)
65
- # _roles = get_roles(_roles)
66
- # return nil if _roles.none?
67
- #
68
- # role_relation = role_class.find_role(_roles.first)
69
- # self.send("#{role_attribute}=", role_relation)
70
- # one_role.save
71
- # end
72
- # alias_method :role=, :roles=
73
- #
74
- # # query assigned roles
75
- # def roles
76
- # role = self.send(role_attribute)
77
- # role ? [role.name.to_sym] : []
78
- # end
79
- #
80
- # def roles_list
81
- # self.roles.to_a
82
- # end
60
+ end
83
61
  end
84
62
 
85
63
  extend Roles::Generic::User::Configuration
@@ -50,29 +50,7 @@ module RoleStrategy::Mongoid
50
50
  self.send("#{role_attribute}=", "")
51
51
  end
52
52
 
53
- alias_method :present_roles, :present_role
54
-
55
- # def role_attribute
56
- # strategy_class.roles_attribute_name
57
- # end
58
- #
59
- # # assign roles
60
- # def roles=(*roles)
61
- # self.role = roles.select_labels.first.to_s
62
- # end
63
- #
64
- # def role= role_name
65
- # if role_name.kind_of_label? && valid_role?(role_name)
66
- # self.send("#{role_attribute}=", role_name.to_s)
67
- # end
68
- # end
69
- #
70
- # # query assigned roles
71
- # def roles
72
- # role = self.send(role_attribute)
73
- # [role.to_sym]
74
- # end
75
- # alias_method :roles_list, :roles
53
+ alias_method :present_roles, :present_role
76
54
  end
77
55
 
78
56
  extend Roles::Generic::User::Configuration
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongoid}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
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-11-23}
12
+ s.date = %q{2010-11-24}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Mongoid}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -29,16 +29,19 @@ Gem::Specification.new do |s|
29
29
  "lib/generators/mongoid/roles/roles_generator.rb",
30
30
  "lib/roles_mongoid.rb",
31
31
  "lib/roles_mongoid/base.rb",
32
+ "lib/roles_mongoid/embedded_role.rb",
32
33
  "lib/roles_mongoid/namespaces.rb",
33
34
  "lib/roles_mongoid/role.rb",
34
35
  "lib/roles_mongoid/strategy.rb",
35
36
  "lib/roles_mongoid/strategy/multi.rb",
37
+ "lib/roles_mongoid/strategy/multi/embed_many_roles.rb",
36
38
  "lib/roles_mongoid/strategy/multi/many_roles.rb",
37
39
  "lib/roles_mongoid/strategy/multi/role_strings.rb",
38
40
  "lib/roles_mongoid/strategy/multi/roles_mask.rb",
39
41
  "lib/roles_mongoid/strategy/shared.rb",
40
42
  "lib/roles_mongoid/strategy/single.rb",
41
43
  "lib/roles_mongoid/strategy/single/admin_flag.rb",
44
+ "lib/roles_mongoid/strategy/single/embed_one_role.rb",
42
45
  "lib/roles_mongoid/strategy/single/one_role.rb",
43
46
  "lib/roles_mongoid/strategy/single/role_string.rb",
44
47
  "roles_mongoid.gemspec",
@@ -48,10 +51,12 @@ Gem::Specification.new do |s|
48
51
  "spec/generator_spec_helper.rb",
49
52
  "spec/roles_mongoid/generators/roles_generator_spec.rb",
50
53
  "spec/roles_mongoid/strategy/api_examples.rb",
54
+ "spec/roles_mongoid/strategy/multi/embed_many_roles_spec.rb",
51
55
  "spec/roles_mongoid/strategy/multi/many_roles_spec.rb",
52
56
  "spec/roles_mongoid/strategy/multi/role_strings_spec.rb",
53
57
  "spec/roles_mongoid/strategy/multi/roles_mask_spec.rb",
54
58
  "spec/roles_mongoid/strategy/single/admin_flag_spec.rb",
59
+ "spec/roles_mongoid/strategy/single/embed_one_role_spec.rb",
55
60
  "spec/roles_mongoid/strategy/single/one_role_spec.rb",
56
61
  "spec/roles_mongoid/strategy/single/role_string_spec.rb",
57
62
  "spec/roles_mongoid/strategy/user_setup.rb",
@@ -68,10 +73,12 @@ Gem::Specification.new do |s|
68
73
  "spec/generator_spec_helper.rb",
69
74
  "spec/roles_mongoid/generators/roles_generator_spec.rb",
70
75
  "spec/roles_mongoid/strategy/api_examples.rb",
76
+ "spec/roles_mongoid/strategy/multi/embed_many_roles_spec.rb",
71
77
  "spec/roles_mongoid/strategy/multi/many_roles_spec.rb",
72
78
  "spec/roles_mongoid/strategy/multi/role_strings_spec.rb",
73
79
  "spec/roles_mongoid/strategy/multi/roles_mask_spec.rb",
74
80
  "spec/roles_mongoid/strategy/single/admin_flag_spec.rb",
81
+ "spec/roles_mongoid/strategy/single/embed_one_role_spec.rb",
75
82
  "spec/roles_mongoid/strategy/single/one_role_spec.rb",
76
83
  "spec/roles_mongoid/strategy/single/role_string_spec.rb",
77
84
  "spec/roles_mongoid/strategy/user_setup.rb",
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :embed_many_roles
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :embed_many_roles, :default
10
+ role_class :role
11
+
12
+ valid_roles_are :admin, :guest, :user
13
+ end
14
+
15
+ def api_name
16
+ :embed_many_roles
17
+ end
18
+
19
+ load 'roles_mongoid/strategy/api_examples.rb'
@@ -18,23 +18,3 @@ end
18
18
 
19
19
  load 'roles_mongoid/strategy/api_examples.rb'
20
20
 
21
- #
22
- # require 'spec_helper'
23
- # use_roles_strategy :many_roles
24
- #
25
- # class User
26
- # include Mongoid::Document
27
- # include Roles::Mongoid
28
- #
29
- # strategy :many_roles, :default
30
- # role_class :role
31
- # valid_roles_are :admin, :guest
32
- #
33
- # field :name, :type => String
34
- # end
35
- #
36
- # describe "Roles for Mongoid: :many_roles strategy" do
37
- # load "roles_mongoid/user_setup"
38
- # load "roles_generic/rspec/api"
39
- # end
40
- #
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ use_roles_strategy :embed_one_role
4
+
5
+ class User
6
+ include Mongoid::Document
7
+ include Roles::Mongoid
8
+
9
+ strategy :embed_one_role, :role_class => :role, :config => :default
10
+ role_class :role
11
+
12
+ valid_roles_are :admin, :guest, :user
13
+ end
14
+
15
+ def api_name
16
+ :embed_one_role
17
+ end
18
+
19
+ load 'roles_mongoid/strategy/api_examples.rb'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
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-11-23 00:00:00 +01:00
17
+ date: 2010-11-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -160,16 +160,19 @@ files:
160
160
  - lib/generators/mongoid/roles/roles_generator.rb
161
161
  - lib/roles_mongoid.rb
162
162
  - lib/roles_mongoid/base.rb
163
+ - lib/roles_mongoid/embedded_role.rb
163
164
  - lib/roles_mongoid/namespaces.rb
164
165
  - lib/roles_mongoid/role.rb
165
166
  - lib/roles_mongoid/strategy.rb
166
167
  - lib/roles_mongoid/strategy/multi.rb
168
+ - lib/roles_mongoid/strategy/multi/embed_many_roles.rb
167
169
  - lib/roles_mongoid/strategy/multi/many_roles.rb
168
170
  - lib/roles_mongoid/strategy/multi/role_strings.rb
169
171
  - lib/roles_mongoid/strategy/multi/roles_mask.rb
170
172
  - lib/roles_mongoid/strategy/shared.rb
171
173
  - lib/roles_mongoid/strategy/single.rb
172
174
  - lib/roles_mongoid/strategy/single/admin_flag.rb
175
+ - lib/roles_mongoid/strategy/single/embed_one_role.rb
173
176
  - lib/roles_mongoid/strategy/single/one_role.rb
174
177
  - lib/roles_mongoid/strategy/single/role_string.rb
175
178
  - roles_mongoid.gemspec
@@ -179,10 +182,12 @@ files:
179
182
  - spec/generator_spec_helper.rb
180
183
  - spec/roles_mongoid/generators/roles_generator_spec.rb
181
184
  - spec/roles_mongoid/strategy/api_examples.rb
185
+ - spec/roles_mongoid/strategy/multi/embed_many_roles_spec.rb
182
186
  - spec/roles_mongoid/strategy/multi/many_roles_spec.rb
183
187
  - spec/roles_mongoid/strategy/multi/role_strings_spec.rb
184
188
  - spec/roles_mongoid/strategy/multi/roles_mask_spec.rb
185
189
  - spec/roles_mongoid/strategy/single/admin_flag_spec.rb
190
+ - spec/roles_mongoid/strategy/single/embed_one_role_spec.rb
186
191
  - spec/roles_mongoid/strategy/single/one_role_spec.rb
187
192
  - spec/roles_mongoid/strategy/single/role_string_spec.rb
188
193
  - spec/roles_mongoid/strategy/user_setup.rb
@@ -225,10 +230,12 @@ test_files:
225
230
  - spec/generator_spec_helper.rb
226
231
  - spec/roles_mongoid/generators/roles_generator_spec.rb
227
232
  - spec/roles_mongoid/strategy/api_examples.rb
233
+ - spec/roles_mongoid/strategy/multi/embed_many_roles_spec.rb
228
234
  - spec/roles_mongoid/strategy/multi/many_roles_spec.rb
229
235
  - spec/roles_mongoid/strategy/multi/role_strings_spec.rb
230
236
  - spec/roles_mongoid/strategy/multi/roles_mask_spec.rb
231
237
  - spec/roles_mongoid/strategy/single/admin_flag_spec.rb
238
+ - spec/roles_mongoid/strategy/single/embed_one_role_spec.rb
232
239
  - spec/roles_mongoid/strategy/single/one_role_spec.rb
233
240
  - spec/roles_mongoid/strategy/single/role_string_spec.rb
234
241
  - spec/roles_mongoid/strategy/user_setup.rb