roles_mongoid 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/Rakefile +11 -7
  2. data/VERSION +1 -1
  3. data/lib/roles_mongoid/base.rb +6 -4
  4. data/lib/roles_mongoid/role.rb +3 -1
  5. data/lib/roles_mongoid/strategy/multi/many_roles.rb +51 -35
  6. data/lib/roles_mongoid/strategy/multi/role_strings.rb +43 -22
  7. data/lib/roles_mongoid/strategy/multi/roles_mask.rb +67 -18
  8. data/lib/roles_mongoid/strategy/multi.rb +59 -0
  9. data/lib/roles_mongoid/strategy/shared.rb +29 -0
  10. data/lib/roles_mongoid/strategy/single/admin_flag.rb +20 -18
  11. data/lib/roles_mongoid/strategy/single/one_role.rb +55 -24
  12. data/lib/roles_mongoid/strategy/single/role_string.rb +54 -22
  13. data/lib/roles_mongoid/strategy/single.rb +34 -0
  14. data/roles_mongoid.gemspec +44 -34
  15. data/sandbox/single_role.rb +25 -11
  16. data/spec/{generators → roles_mongoid/generators}/roles_generator_spec.rb +0 -0
  17. data/spec/roles_mongoid/strategy/api_examples.rb +200 -0
  18. data/spec/roles_mongoid/strategy/multi/many_roles_spec.rb +40 -0
  19. data/spec/roles_mongoid/strategy/multi/role_strings_spec.rb +19 -0
  20. data/spec/roles_mongoid/strategy/multi/roles_mask_spec.rb +39 -0
  21. data/spec/roles_mongoid/strategy/single/admin_flag_spec.rb +18 -0
  22. data/spec/roles_mongoid/strategy/single/one_role_spec.rb +19 -0
  23. data/spec/roles_mongoid/strategy/single/role_string_spec.rb +19 -0
  24. data/spec/roles_mongoid/strategy/user_setup.rb +13 -0
  25. data/spec/spec_helper.rb +0 -13
  26. metadata +62 -43
  27. data/spec/roles_mongoid/admin_flag_spec.rb +0 -74
  28. data/spec/roles_mongoid/many_roles_spec.rb +0 -74
  29. data/spec/roles_mongoid/one_role_spec.rb +0 -74
  30. data/spec/roles_mongoid/role_string_spec.rb +0 -73
  31. data/spec/roles_mongoid/role_strings_spec.rb +0 -73
  32. data/spec/roles_mongoid/roles_mask_spec.rb +0 -73
data/Rakefile CHANGED
@@ -7,14 +7,18 @@ begin
7
7
  gem.email = "kmandrup@gmail.com"
8
8
  gem.homepage = "http://github.com/kristianmandrup/roles_mongoid"
9
9
  gem.authors = ["Kristian Mandrup"]
10
- gem.add_development_dependency "rspec", '~> 2.0.0.beta.22'
10
+ gem.add_development_dependency "rspec", '>= 2.1'
11
+
12
+ gem.add_dependency "mongoid", '>= 2.0.0.beta.19'
13
+ gem.add_dependency 'bson', '>= 1.1.1'
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'
18
+
19
+ gem.add_dependency 'rails3_artifactor', '~> 0.3.1'
20
+ gem.add_dependency 'logging_assist', '~> 0.1.6'
11
21
 
12
- gem.add_dependency "mongoid", '~> 2.0.0.beta.18'
13
- gem.add_dependency 'bson', '~> 1.0.7'
14
- gem.add_dependency "activesupport", '~> 3.0.0'
15
- gem.add_dependency "require_all", '~> 1.2.0'
16
- gem.add_dependency "sugar-high", '~> 0.2.11'
17
- gem.add_dependency "roles_generic", '~> 0.2.7'
18
22
 
19
23
  end
20
24
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.3.1
@@ -16,9 +16,11 @@ module Roles
16
16
 
17
17
  MAP = {
18
18
  :admin_flag => "field :admin_flag, :type => Boolean",
19
- :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role', :default => []",
20
- :one_role => "references_one :one_role, :class_name => 'Role'",
21
- :roles_mask => "field :roles_mask, :type => Integer, :default => 1",
19
+
20
+ :many_roles => "references_many :many_roles, :stored_as => :array, :class_name => 'Role'",
21
+ :one_role => "referenced_in :one_role, :class_name => 'Role'",
22
+
23
+ :roles_mask => "field :roles_mask, :type => Integer, :default => 0",
22
24
  :role_string => "field :role_string, :type => String",
23
25
  :role_strings => "field :role_strings, :type => Array",
24
26
  :roles_string => "field :roles_string, :type => String"
@@ -28,7 +30,7 @@ module Roles
28
30
  if options == :default && MAP[name]
29
31
  instance_eval MAP[name]
30
32
  end
31
- role_strategy name, options
33
+ set_role_strategy name, options
32
34
  end
33
35
  end
34
36
  end
@@ -12,7 +12,9 @@ end
12
12
  class Role
13
13
  include Mongoid::Document
14
14
  field :name, :type => String
15
- referenced_in :user
15
+ # references_many :users
16
+
17
+ # references_many :users, :class_name => 'User', :stored_as => :array
16
18
 
17
19
  class << self
18
20
  def find_roles(*role_names)
@@ -1,3 +1,5 @@
1
+ require 'roles_mongoid/strategy/multi'
2
+
1
3
  module RoleStrategy::Mongoid
2
4
  module ManyRoles
3
5
  def self.default_role_attribute
@@ -9,59 +11,73 @@ module RoleStrategy::Mongoid
9
11
  base.extend ClassMethods
10
12
  end
11
13
 
12
- module ClassMethods
14
+ module ClassMethods
13
15
  def role_attribute
14
16
  "#{strategy_class.roles_attribute_name.to_s.singularize}_ids".to_sym
15
- end
16
-
17
- def in_role(role_name)
18
- in_roles(role_name)
19
17
  end
20
18
 
21
- def in_roles(*role_names)
22
- begin
19
+ def in_role(role_name)
20
+ in_any_role(role_name)
21
+ end
22
+
23
+ def in_any_role(*role_names)
24
+ begin
23
25
  role_ids = Role.where(:name.in => role_names.to_strings).to_a.map(&:_id)
24
- criteria.in(role_attribute => role_ids).to_a
26
+ where(:"#{role_attribute}".in => role_ids).to_a
25
27
  rescue
26
28
  return []
27
29
  end
28
- end
30
+ end
29
31
  end
30
-
32
+
31
33
  module Implementation
32
- def role_attribute
33
- strategy_class.roles_attribute_name
34
- end
35
-
36
- # assign roles
37
- def roles=(*_roles)
38
- _roles = get_roles(_roles)
39
- return nil if roles.none?
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
40
56
 
41
- role_relations = role_class.find_roles(_roles)
42
- self.send("#{role_attribute}=", role_relations)
43
- save
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)
44
65
  end
45
66
 
46
- def add_roles(*roles)
47
- raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
48
- role_relations = role_class.find_roles(*roles)
49
- self.send(role_attribute) << role_relations
50
- save
67
+ def new_roles *role_names
68
+ role_class.find_roles(extract_roles role_names)
51
69
  end
52
70
 
53
- # query assigned roles
54
- def roles
55
- self.send(role_attribute)
71
+ def present_roles roles_names
72
+ roles_names.to_a.map{|role| role.name.to_s.to_sym}
56
73
  end
57
74
 
58
- def roles_list
59
- [roles].flatten.map{|r| r.name }.compact.to_symbols
75
+ def set_empty_roles
76
+ self.send("#{role_attribute}=", [])
60
77
  end
61
- end
62
-
78
+ end
79
+
63
80
  extend Roles::Generic::User::Configuration
64
- configure :type => :role_class
81
+ configure :type => :role_class
65
82
  end
66
83
  end
67
-
@@ -1,3 +1,5 @@
1
+ require 'roles_mongoid/strategy/multi'
2
+
1
3
  module RoleStrategy::Mongoid
2
4
  module RoleStrings
3
5
  def self.default_role_attribute
@@ -14,10 +16,10 @@ module RoleStrategy::Mongoid
14
16
  end
15
17
 
16
18
  def in_role(role_name)
17
- in_roles(role_name)
19
+ in_any_role(role_name)
18
20
  end
19
21
 
20
- def in_roles(*role_names)
22
+ def in_any_role(*role_names)
21
23
  begin
22
24
  where(role_attribute.in => role_names).to_a
23
25
  rescue
@@ -27,30 +29,49 @@ module RoleStrategy::Mongoid
27
29
  end
28
30
 
29
31
  module Implementation
30
- # assign roles
31
- def roles=(*new_roles)
32
- new_roles = new_roles.flatten.map{|r| r.to_s if valid_role?(r)}.compact
33
- if new_roles && new_roles.not.empty?
34
- self.send("#{role_attribute}=", new_roles.compact.uniq)
35
- end
36
- end
37
- alias_method :role=, :roles=
32
+ include Roles::Mongoid::Strategy::Multi
33
+
34
+ def new_roles *roles
35
+ ::Set.new select_valid_roles(roles)
36
+ end
38
37
 
39
- def add_roles(*roles_to_add)
40
- roles_to_add = roles_to_add.flatten.map{|r| r.to_s if valid_role?(r)}.compact
41
- if new_roles && new_roles.not.empty?
42
- self.send(role_attribute) << new_roles.compact.uniq
43
- end
44
- end
38
+ def select_valid_roles *roles
39
+ roles.flat_uniq.select{|role| valid_role? role }.map(&:to_sym)
40
+ end
41
+
42
+ def set_empty_roles
43
+ self.send("#{role_attribute}=", [])
44
+ end
45
45
 
46
- # query assigned roles
47
- def roles
48
- self.send(role_attribute).map{|r| r.to_sym}
46
+ def present_roles roles_names
47
+ roles_names.to_a
49
48
  end
50
49
 
51
- def roles_list
52
- [roles].flatten
53
- end
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
54
75
  end
55
76
 
56
77
  extend Roles::Generic::User::Configuration
@@ -1,3 +1,5 @@
1
+ require 'roles_mongoid/strategy/multi'
2
+
1
3
  module RoleStrategy::Mongoid
2
4
  module RolesMask
3
5
  def self.default_role_attribute
@@ -20,7 +22,7 @@ module RoleStrategy::Mongoid
20
22
  end
21
23
  end
22
24
 
23
- def in_roles(*roles)
25
+ def in_any_role(*roles)
24
26
  all.select do |user|
25
27
  roles.flatten.any? do |role|
26
28
  mask = calc_index(role.to_s)
@@ -35,6 +37,8 @@ module RoleStrategy::Mongoid
35
37
  end
36
38
 
37
39
  module Implementation
40
+ include Roles::Mongoid::Strategy::Multi
41
+
38
42
  class Roles < ::Set # :nodoc:
39
43
  attr_reader :model_instance
40
44
 
@@ -49,30 +53,75 @@ module RoleStrategy::Mongoid
49
53
  end
50
54
  end
51
55
 
52
- def role_attribute
53
- strategy_class.roles_attribute_name
54
- end
56
+ protected
57
+
58
+ def calc_index(r)
59
+ 2**strategy_class.valid_roles.index(r)
60
+ end
55
61
 
56
- # assign roles
57
- def roles=(*roles)
58
- self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
62
+ def get_roles
63
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })
59
64
  end
60
- alias_method :role=, :roles=
61
65
 
62
- # query assigned roles
63
- def roles
64
- strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
66
+ def new_roles *role_names
67
+ role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
68
+ role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
65
69
  end
66
70
 
67
- def roles_list
68
- roles.to_a
69
- end
70
-
71
- protected
71
+ def set_empty_roles
72
+ self.send("#{role_attribute}=", 0)
73
+ end
74
+
75
+ def present_roles *role_names
76
+ role_names.to_a.to_symbols
77
+ end
72
78
 
73
- def calc_index(r)
74
- 2**strategy_class.valid_roles.index(r)
79
+ def set_roles *roles
80
+ roles = roles.flat_uniq
81
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
82
+ vrs = new_roles roles
83
+ self.send("#{role_attribute}=", vrs)
75
84
  end
85
+
86
+
87
+ # class Roles < ::Set # :nodoc:
88
+ # attr_reader :model_instance
89
+ #
90
+ # def initialize(sender, *roles)
91
+ # super(*roles)
92
+ # @model_instance = sender
93
+ # end
94
+ #
95
+ # def <<(role)
96
+ # model_instance.roles = super.to_a
97
+ # self
98
+ # end
99
+ # end
100
+ #
101
+ # def role_attribute
102
+ # strategy_class.roles_attribute_name
103
+ # end
104
+ #
105
+ # # assign roles
106
+ # def roles=(*roles)
107
+ # self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
108
+ # end
109
+ # alias_method :role=, :roles=
110
+ #
111
+ # # query assigned roles
112
+ # def roles
113
+ # strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
114
+ # end
115
+ #
116
+ # def roles_list
117
+ # roles.to_a
118
+ # end
119
+ #
120
+ # protected
121
+ #
122
+ # def calc_index(r)
123
+ # 2**strategy_class.valid_roles.index(r)
124
+ # end
76
125
  end
77
126
 
78
127
  extend Roles::Generic::User::Configuration
@@ -0,0 +1,59 @@
1
+ require 'roles_mongoid/strategy/shared'
2
+
3
+ module Roles::Mongoid
4
+ module Strategy
5
+ module Multi
6
+ # assign multiple roles
7
+ def roles=(*role_names)
8
+ extracted_roles = extract_roles(role_names)
9
+ return nil if extracted_roles.empty?
10
+ set_roles(select_valid_roles extracted_roles)
11
+ end
12
+
13
+ def add_roles *roles
14
+ new_roles = select_valid_roles(roles)
15
+ if !new_roles.empty?
16
+ self.roles = self.roles + new_roles
17
+ end
18
+ end
19
+
20
+ # should remove the current single role (set = nil)
21
+ # only if it is contained in the list of roles to be removed
22
+ def remove_roles *role_names
23
+ role_names = role_names.flat_uniq
24
+ set_empty_roles and return if roles_diff(role_names).empty?
25
+ roles_to_remove = select_valid_roles(role_names)
26
+ diff = roles_diff(role_names)
27
+ diff = role_class.find_roles(diff).to_a if self.class.role_strategy.type == :complex
28
+ set_roles(diff)
29
+ true
30
+ end
31
+
32
+ # query assigned roles
33
+ def roles
34
+ get_roles.to_a.map do |role|
35
+ role.respond_to?(:sym) ? role.to_sym : role
36
+ end
37
+ end
38
+
39
+ def roles_list
40
+ my_roles = [roles].flat_uniq
41
+ return [] if my_roles.empty?
42
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
43
+ end
44
+
45
+ protected
46
+
47
+ def set_roles *roles
48
+ roles = roles.flat_uniq
49
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
50
+ # self.send("#{role_attribute}").send(:<<, roles)
51
+ self.send("#{role_attribute}=", roles)
52
+ end
53
+
54
+ def get_roles
55
+ self.send(role_attribute)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ module Roles::Mongoid
2
+ module Strategy
3
+ module Shared
4
+ def set_role role
5
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
6
+ self.send("#{role_attribute}=", role)
7
+ end
8
+
9
+ def set_roles *roles
10
+ roles = roles.flat_uniq
11
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
12
+ self.send("#{role_attribute}=", roles)
13
+ end
14
+
15
+ def get_role
16
+ r = self.send(role_attribute)
17
+ end
18
+
19
+ def get_roles
20
+ r = self.send(role_attribute)
21
+ end
22
+
23
+ def select_valid_roles *roles
24
+ roles.flat_uniq.select{|role| valid_role? role }
25
+ has_role_class? ? role_class.find_roles(roles).to_a.flat_uniq : roles.flat_uniq
26
+ end
27
+ end
28
+ end
29
+ end