mongoid_roles_zhd 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.bundle/config +2 -0
  2. data/.document +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +17 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +99 -0
  7. data/Rakefile +26 -0
  8. data/VERSION +1 -0
  9. data/lib/generators/mongoid/roles/core_ext.rb +11 -0
  10. data/lib/generators/mongoid/roles/roles_generator.rb +173 -0
  11. data/lib/generators/mongoid/roles/templates/role.rb +8 -0
  12. data/lib/mongoid_roles.rb +10 -0
  13. data/lib/mongoid_roles/base.rb +91 -0
  14. data/lib/mongoid_roles/base_role.rb +27 -0
  15. data/lib/mongoid_roles/embedded_role.rb +24 -0
  16. data/lib/mongoid_roles/namespaces.rb +12 -0
  17. data/lib/mongoid_roles/role.rb +10 -0
  18. data/lib/mongoid_roles/strategy.rb +15 -0
  19. data/lib/mongoid_roles/strategy/multi.rb +59 -0
  20. data/lib/mongoid_roles/strategy/multi/embed_many_roles.rb +124 -0
  21. data/lib/mongoid_roles/strategy/multi/many_roles.rb +83 -0
  22. data/lib/mongoid_roles/strategy/multi/role_strings.rb +54 -0
  23. data/lib/mongoid_roles/strategy/multi/roles_mask.rb +130 -0
  24. data/lib/mongoid_roles/strategy/shared.rb +29 -0
  25. data/lib/mongoid_roles/strategy/single.rb +34 -0
  26. data/lib/mongoid_roles/strategy/single/admin_flag.rb +55 -0
  27. data/lib/mongoid_roles/strategy/single/embed_one_role.rb +83 -0
  28. data/lib/mongoid_roles/strategy/single/one_role.rb +66 -0
  29. data/lib/mongoid_roles/strategy/single/role_string.rb +61 -0
  30. data/mongoid_roles.gemspec +125 -0
  31. data/sandbox/roles_assign.rb +54 -0
  32. data/sandbox/single_role.rb +82 -0
  33. data/sandbox/test.rb +47 -0
  34. data/sandbox/test_query.rb +49 -0
  35. data/spec/generator_spec_helper.rb +12 -0
  36. data/spec/mongoid_roles/generators/roles_generator_spec.rb +67 -0
  37. data/spec/mongoid_roles/strategy/api_examples.rb +200 -0
  38. data/spec/mongoid_roles/strategy/multi/embed_many_roles_spec.rb +15 -0
  39. data/spec/mongoid_roles/strategy/multi/many_roles_old_spec.rb +22 -0
  40. data/spec/mongoid_roles/strategy/multi/many_roles_spec.rb +19 -0
  41. data/spec/mongoid_roles/strategy/multi/role_strings_spec.rb +16 -0
  42. data/spec/mongoid_roles/strategy/multi/roles_mask_spec.rb +20 -0
  43. data/spec/mongoid_roles/strategy/single/admin_flag_spec.rb +18 -0
  44. data/spec/mongoid_roles/strategy/single/embed_one_role_spec.rb +18 -0
  45. data/spec/mongoid_roles/strategy/single/one_role_spec.rb +18 -0
  46. data/spec/mongoid_roles/strategy/single/role_string_spec.rb +18 -0
  47. data/spec/mongoid_roles/strategy/user_setup.rb +13 -0
  48. data/spec/spec_helper.rb +15 -0
  49. data/tmp/rails/app/models/user.rb +8 -0
  50. data/tmp/rails/config/routes.rb +2 -0
  51. metadata +256 -0
@@ -0,0 +1,29 @@
1
+ module Mongoid::Roles
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
@@ -0,0 +1,34 @@
1
+ require 'mongoid_roles/strategy/shared'
2
+
3
+ class Symbol
4
+ def admin?
5
+ self == :admin
6
+ end
7
+ end
8
+
9
+ module Mongoid::Roles
10
+ module Strategy
11
+ module Single
12
+ include Shared
13
+ # assigns first valid role from list of roles
14
+ def add_roles *role_names
15
+ new_roles = select_valid_roles(role_names)
16
+ new_role = new_roles.first if !new_roles.empty?
17
+ set_role new_role
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
+ roles = role_names.flat_uniq
24
+ set_empty_role if roles_diff(roles).empty?
25
+ true
26
+ end
27
+
28
+ def roles_list
29
+ raise 'the method #roles should be present' if !respond_to? :roles
30
+ present_roles(roles)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,55 @@
1
+ require 'mongoid_roles/strategy/single'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module AdminFlag
5
+ def self.default_role_attribute
6
+ :admin_flag
7
+ end
8
+
9
+ def self.included base
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def role_attribute
15
+ strategy_class.roles_attribute_name.to_sym
16
+ end
17
+
18
+ def in_role(role_name)
19
+ case role_name.downcase.to_sym
20
+ when :admin
21
+ where(role_attribute => true)
22
+ else
23
+ where(role_attribute => false)
24
+ end
25
+ end
26
+ end
27
+
28
+ module Implementation
29
+ include Mongoid::Roles::Strategy::Single
30
+
31
+ def new_role role
32
+ role = role.kind_of?(Array) ? role.flatten.first : role
33
+ role.admin?
34
+ end
35
+
36
+ def get_role
37
+ self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
38
+ end
39
+
40
+ def present_roles *roles
41
+ roles = roles.flat_uniq
42
+ roles.map{|role| role ? :admin : :guest}
43
+ end
44
+
45
+ def set_empty_role
46
+ self.send("#{role_attribute}=", false)
47
+ end
48
+
49
+
50
+ end # Implementation
51
+
52
+ extend Roles::Generic::User::Configuration
53
+ configure :num => :single
54
+ end
55
+ end
@@ -0,0 +1,83 @@
1
+ require 'mongoid_roles/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
+ any_in("one_role.name" => role_names)
30
+ rescue
31
+ return []
32
+ end
33
+ end
34
+ end
35
+
36
+ module Implementation
37
+ include Mongoid::Roles::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
58
+
59
+ def new_role role
60
+ role_class.find_role(extract_role role)
61
+ end
62
+
63
+ def new_roles *roles
64
+ new_role roles.flatten.first
65
+ end
66
+
67
+ def present_roles *roles
68
+ roles.map{|role| extract_role role}
69
+ end
70
+
71
+ def set_empty_role
72
+ self.send("#{role_attribute}=", nil)
73
+ end
74
+
75
+ def select_valid_roles *role_names
76
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
77
+ end
78
+ end
79
+
80
+ extend Roles::Generic::User::Configuration
81
+ configure :num => :single, :type => :role_class
82
+ end
83
+ end
@@ -0,0 +1,66 @@
1
+ require 'mongoid_roles/strategy/single'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module OneRole
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 Mongoid::Roles::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
@@ -0,0 +1,61 @@
1
+ require 'mongoid_roles/strategy/single'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module RoleString
5
+ def self.default_role_attribute
6
+ :role_string
7
+ end
8
+
9
+ def self.included base
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def role_attribute
15
+ strategy_class.roles_attribute_name.to_sym
16
+ end
17
+
18
+ def in_role(role_name)
19
+ in_any_role(role_name)
20
+ end
21
+
22
+ def in_any_role(*role_names)
23
+ begin
24
+ where(:"#{role_attribute}".in => role_names)
25
+ rescue
26
+ return []
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ module Implementation
33
+ include Mongoid::Roles::Strategy::Single
34
+
35
+ def new_role role
36
+ role = role.kind_of?(Array) ? role.first : role
37
+ role.to_s
38
+ end
39
+
40
+ def new_roles *roles
41
+ new_role roles.flatten.first
42
+ end
43
+
44
+ def present_role role
45
+ return role if role.kind_of? Array
46
+ role.split(',').map(&:to_sym)
47
+ end
48
+
49
+ def set_empty_role
50
+ self.send("#{role_attribute}=", "")
51
+ end
52
+
53
+ alias_method :present_roles, :present_role
54
+ end
55
+
56
+ extend Roles::Generic::User::Configuration
57
+ configure :num => :single
58
+ end
59
+ end
60
+
61
+
@@ -0,0 +1,125 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongoid_roles_zhd}
8
+ s.version = "0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{ZhangHanDong}]
12
+ s.date = %q{2011-06-16}
13
+ s.description = %q{Makes it easy to set a role strategy on your User model in Mongoid}
14
+ s.email = %q{blackanger.z@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".bundle/config",
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "LICENSE",
25
+ "README.textile",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/generators/mongoid/roles/core_ext.rb",
29
+ "lib/generators/mongoid/roles/roles_generator.rb",
30
+ "lib/generators/mongoid/roles/templates/role.rb",
31
+ "lib/mongoid_roles.rb",
32
+ "lib/mongoid_roles/base.rb",
33
+ "lib/mongoid_roles/base_role.rb",
34
+ "lib/mongoid_roles/embedded_role.rb",
35
+ "lib/mongoid_roles/namespaces.rb",
36
+ "lib/mongoid_roles/role.rb",
37
+ "lib/mongoid_roles/strategy.rb",
38
+ "lib/mongoid_roles/strategy/multi.rb",
39
+ "lib/mongoid_roles/strategy/multi/embed_many_roles.rb",
40
+ "lib/mongoid_roles/strategy/multi/many_roles.rb",
41
+ "lib/mongoid_roles/strategy/multi/role_strings.rb",
42
+ "lib/mongoid_roles/strategy/multi/roles_mask.rb",
43
+ "lib/mongoid_roles/strategy/shared.rb",
44
+ "lib/mongoid_roles/strategy/single.rb",
45
+ "lib/mongoid_roles/strategy/single/admin_flag.rb",
46
+ "lib/mongoid_roles/strategy/single/embed_one_role.rb",
47
+ "lib/mongoid_roles/strategy/single/one_role.rb",
48
+ "lib/mongoid_roles/strategy/single/role_string.rb",
49
+ "mongoid_roles.gemspec",
50
+ "sandbox/roles_assign.rb",
51
+ "sandbox/single_role.rb",
52
+ "sandbox/test.rb",
53
+ "sandbox/test_query.rb",
54
+ "spec/generator_spec_helper.rb",
55
+ "spec/mongoid_roles/generators/roles_generator_spec.rb",
56
+ "spec/mongoid_roles/strategy/api_examples.rb",
57
+ "spec/mongoid_roles/strategy/multi/embed_many_roles_spec.rb",
58
+ "spec/mongoid_roles/strategy/multi/many_roles_old_spec.rb",
59
+ "spec/mongoid_roles/strategy/multi/many_roles_spec.rb",
60
+ "spec/mongoid_roles/strategy/multi/role_strings_spec.rb",
61
+ "spec/mongoid_roles/strategy/multi/roles_mask_spec.rb",
62
+ "spec/mongoid_roles/strategy/single/admin_flag_spec.rb",
63
+ "spec/mongoid_roles/strategy/single/embed_one_role_spec.rb",
64
+ "spec/mongoid_roles/strategy/single/one_role_spec.rb",
65
+ "spec/mongoid_roles/strategy/single/role_string_spec.rb",
66
+ "spec/mongoid_roles/strategy/user_setup.rb",
67
+ "spec/spec_helper.rb",
68
+ "tmp/rails/app/models/user.rb",
69
+ "tmp/rails/config/routes.rb"
70
+ ]
71
+ s.homepage = %q{https://github.com/ZhangHanDong/roles_mongoid}
72
+ s.require_paths = [%q{lib}]
73
+ s.rubygems_version = %q{1.8.0}
74
+ s.summary = %q{Implementation of Roles generic API for Mongoid}
75
+ s.test_files = [
76
+ "spec/generator_spec_helper.rb",
77
+ "spec/mongoid_roles/generators/roles_generator_spec.rb",
78
+ "spec/mongoid_roles/strategy/api_examples.rb",
79
+ "spec/mongoid_roles/strategy/multi/embed_many_roles_spec.rb",
80
+ "spec/mongoid_roles/strategy/multi/many_roles_old_spec.rb",
81
+ "spec/mongoid_roles/strategy/multi/many_roles_spec.rb",
82
+ "spec/mongoid_roles/strategy/multi/role_strings_spec.rb",
83
+ "spec/mongoid_roles/strategy/multi/roles_mask_spec.rb",
84
+ "spec/mongoid_roles/strategy/single/admin_flag_spec.rb",
85
+ "spec/mongoid_roles/strategy/single/embed_one_role_spec.rb",
86
+ "spec/mongoid_roles/strategy/single/one_role_spec.rb",
87
+ "spec/mongoid_roles/strategy/single/role_string_spec.rb",
88
+ "spec/mongoid_roles/strategy/user_setup.rb",
89
+ "spec/spec_helper.rb"
90
+ ]
91
+
92
+ if s.respond_to? :specification_version then
93
+ s.specification_version = 3
94
+
95
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
96
+ s.add_development_dependency(%q<rspec>, [">= 2.4.1"])
97
+ s.add_development_dependency(%q<generator-spec>, [">= 0.7.3"])
98
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.0.2"])
99
+ s.add_runtime_dependency(%q<bson>, [">= 1.3.1"])
100
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.4.0"])
101
+ s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
102
+ s.add_runtime_dependency(%q<roles_generic>, ["~> 0.3.9"])
103
+ s.add_runtime_dependency(%q<rails_artifactor>, [">= 0.3.3"])
104
+ else
105
+ s.add_dependency(%q<rspec>, [">= 2.4.1"])
106
+ s.add_dependency(%q<generator-spec>, [">= 0.7.3"])
107
+ s.add_dependency(%q<mongoid>, [">= 2.0.2"])
108
+ s.add_dependency(%q<bson>, [">= 1.3.1"])
109
+ s.add_dependency(%q<sugar-high>, ["~> 0.4.0"])
110
+ s.add_dependency(%q<require_all>, ["~> 1.2.0"])
111
+ s.add_dependency(%q<roles_generic>, ["~> 0.3.9"])
112
+ s.add_dependency(%q<rails_artifactor>, [">= 0.3.3"])
113
+ end
114
+ else
115
+ s.add_dependency(%q<rspec>, [">= 2.4.1"])
116
+ s.add_dependency(%q<generator-spec>, [">= 0.7.3"])
117
+ s.add_dependency(%q<mongoid>, [">= 2.0.2"])
118
+ s.add_dependency(%q<bson>, [">= 1.3.1"])
119
+ s.add_dependency(%q<sugar-high>, ["~> 0.4.0"])
120
+ s.add_dependency(%q<require_all>, ["~> 1.2.0"])
121
+ s.add_dependency(%q<roles_generic>, ["~> 0.3.9"])
122
+ s.add_dependency(%q<rails_artifactor>, [">= 0.3.3"])
123
+ end
124
+ end
125
+