roles_mongoid_zhd 0.4.8

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/roles_mongoid.rb +10 -0
  13. data/lib/roles_mongoid/base.rb +91 -0
  14. data/lib/roles_mongoid/base_role.rb +27 -0
  15. data/lib/roles_mongoid/embedded_role.rb +24 -0
  16. data/lib/roles_mongoid/namespaces.rb +12 -0
  17. data/lib/roles_mongoid/role.rb +10 -0
  18. data/lib/roles_mongoid/strategy.rb +15 -0
  19. data/lib/roles_mongoid/strategy/multi.rb +59 -0
  20. data/lib/roles_mongoid/strategy/multi/embed_many_roles.rb +124 -0
  21. data/lib/roles_mongoid/strategy/multi/many_roles.rb +83 -0
  22. data/lib/roles_mongoid/strategy/multi/role_strings.rb +54 -0
  23. data/lib/roles_mongoid/strategy/multi/roles_mask.rb +130 -0
  24. data/lib/roles_mongoid/strategy/shared.rb +29 -0
  25. data/lib/roles_mongoid/strategy/single.rb +34 -0
  26. data/lib/roles_mongoid/strategy/single/admin_flag.rb +55 -0
  27. data/lib/roles_mongoid/strategy/single/embed_one_role.rb +83 -0
  28. data/lib/roles_mongoid/strategy/single/one_role.rb +66 -0
  29. data/lib/roles_mongoid/strategy/single/role_string.rb +61 -0
  30. data/roles_mongoid.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/roles_mongoid/generators/roles_generator_spec.rb +67 -0
  37. data/spec/roles_mongoid/strategy/api_examples.rb +200 -0
  38. data/spec/roles_mongoid/strategy/multi/embed_many_roles_spec.rb +15 -0
  39. data/spec/roles_mongoid/strategy/multi/many_roles_old_spec.rb +22 -0
  40. data/spec/roles_mongoid/strategy/multi/many_roles_spec.rb +19 -0
  41. data/spec/roles_mongoid/strategy/multi/role_strings_spec.rb +16 -0
  42. data/spec/roles_mongoid/strategy/multi/roles_mask_spec.rb +38 -0
  43. data/spec/roles_mongoid/strategy/single/admin_flag_spec.rb +18 -0
  44. data/spec/roles_mongoid/strategy/single/embed_one_role_spec.rb +18 -0
  45. data/spec/roles_mongoid/strategy/single/one_role_spec.rb +18 -0
  46. data/spec/roles_mongoid/strategy/single/role_string_spec.rb +18 -0
  47. data/spec/roles_mongoid/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 +257 -0
@@ -0,0 +1,54 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new.db('testing')
5
+ end
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+
12
+ class Role
13
+ include Mongoid::Document
14
+ field :name, :type => String
15
+
16
+ class << self
17
+ def find_roles(*role_names)
18
+ where(:name.in => role_names.flatten).to_a #.map(&:_id)
19
+ end
20
+ alias_method :find_role, :find_roles
21
+ end
22
+ end
23
+
24
+ module Blip
25
+ def self.included(base)
26
+ base.extend ClassMethods
27
+ end
28
+
29
+ module ClassMethods
30
+ def do_it
31
+ instance_eval "references_many :many_roles, :stored_as => :array, :class_name => 'Role'"
32
+ end
33
+ end
34
+ end
35
+
36
+ class User
37
+ include Mongoid::Document
38
+ include Blip
39
+ field :name, :type => String
40
+
41
+ do_it
42
+
43
+ end
44
+
45
+ Role.create :name => 'guest'
46
+ Role.create :name => 'admin'
47
+
48
+ user = User.create :name => 'Kristian'
49
+
50
+ user.many_roles << Role.find_role(:guest)
51
+ user.many_roles << Role.find_role(:admin)
52
+
53
+ puts user.many_roles.inspect
54
+
@@ -0,0 +1,82 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new.db('testing')
5
+ end
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+
12
+ class Role
13
+ include Mongoid::Document
14
+ field :name, :type => String
15
+ references_many :users #one_role, :class_name => 'Role'
16
+
17
+ class << self
18
+ def find_roles(*role_names)
19
+ where(:name.in => role_names.flatten).to_a #.map(&:_id)
20
+ end
21
+ alias_method :find_role, :find_roles
22
+ end
23
+ end
24
+
25
+ module Blip
26
+ def self.included(base)
27
+ base.extend ClassMethods
28
+ end
29
+
30
+ module ClassMethods
31
+ def do_it
32
+ instance_eval "references_one :role" # :class_name => 'Role'
33
+ end
34
+ end
35
+ end
36
+
37
+ class User
38
+ include Mongoid::Document
39
+ # include Blip
40
+ field :name, :type => String
41
+ referenced_in :one_role, :class_name => 'Role'
42
+
43
+ # do_it
44
+ end
45
+
46
+ user = User.create :name => 'Sandy'
47
+ user2 = User.create :name => 'Mike'
48
+ role = Role.create :name => 'Guest'
49
+ role2 = Role.create :name => 'Admin'
50
+
51
+ user.save
52
+ user2.save
53
+
54
+ user.one_role = role2 #<< [role, role2]
55
+ role.users << [user, user2]
56
+ user2.one_role = role
57
+
58
+ # role.users << user
59
+ # role.users << user2
60
+
61
+ role.save
62
+
63
+ puts "user: #{user.inspect}"
64
+ puts "user2 #{user2.inspect}"
65
+ puts "role: #{role.inspect}"
66
+ puts "role users: #{role.users.to_a.inspect}"
67
+
68
+
69
+ # Role.create :name => 'guest'
70
+ # Role.create :name => 'admin'
71
+ #
72
+ # user = User.create :name => 'Kristian'
73
+ #
74
+ #
75
+ # user.role.create :name => :guest
76
+ # user.save
77
+
78
+ # user.role = Role.find_role(:guest).first
79
+ # user.save
80
+
81
+
82
+
data/sandbox/test.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new.db('testing')
5
+ end
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+ class User
12
+ include Mongoid::Document
13
+ field :name
14
+ references_many :roles, :stored_as => :array
15
+
16
+ def self.find_roles *ids
17
+ arr_ids = ids.flatten
18
+ all.to_a.select do |user|
19
+ user.roles.to_a.any? do |role|
20
+ arr_ids.include?(role._id)
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.get_roles(*ids)
26
+ arr = ids.flatten
27
+ criteria.in(:role_ids => arr).to_a
28
+ end
29
+
30
+ end
31
+
32
+ class Role
33
+ include Mongoid::Document
34
+ field :name
35
+ end
36
+
37
+ user = User.create(:name => 'Kristian')
38
+ user.roles.create(:name => 'guest')
39
+ user.save
40
+ #
41
+ puts "user roles: #{User.first.roles.map(&:_id)}"
42
+
43
+ role_id = user.roles.to_a.first._id
44
+
45
+ p role_id
46
+ p User.find_roles role_id
47
+ p User.get_roles role_id
@@ -0,0 +1,49 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new.db('testing')
5
+ end
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+
12
+ class User
13
+ include Mongoid::Document
14
+
15
+ field :username, :type => String
16
+ field :email, :type => String
17
+
18
+ def self.find_record(login)
19
+ found = where(:username => login).to_a
20
+ found = where(:email => login).to_a if found.empty?
21
+ found
22
+ end
23
+
24
+ def self.find_record_generic(attributes)
25
+ where(attributes).first
26
+ end
27
+
28
+ def self.find_record_alt(login)
29
+ where("function() {return this.username == '#{login}' || this.email == '#{login}'}")
30
+ end
31
+ end
32
+
33
+ User.create :username => 'guest', :email => 'guest@email.com'
34
+ User.create :username => 'admin', :email => 'admin@email.com'
35
+ User.create :username => 'blip', :email => 'guest@email.com'
36
+ User.create :username => 'blip', :email => 'guest@cool.com'
37
+
38
+ puts User.all
39
+
40
+ puts "Found match 'blip'"
41
+
42
+ # puts User.find_record('blip').first.inspect
43
+ puts User.find_record_alt('blip').first.inspect
44
+
45
+ # puts User.find_record_generic(:username => 'blip')
46
+
47
+ puts "Found match 'guest@email.com'"
48
+
49
+ puts User.find_record('guest@email.com').first.inspect
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'rails_artifactor'
3
+ require 'generator-spec'
4
+ require 'roles_generic'
5
+
6
+ RSpec::Generator.configure do |config|
7
+ config.debug = false
8
+ config.remove_temp_dir = false # true
9
+ config.default_rails_root(__FILE__)
10
+ config.lib = File.dirname(__FILE__) + '/../lib'
11
+ config.logger = :stdout
12
+ end
@@ -0,0 +1,67 @@
1
+ require 'generator_spec_helper'
2
+ # require_generator :mongoid => :roles
3
+
4
+ require 'generators/mongoid/roles/roles_generator'
5
+
6
+ # root_dir = RailsAssist::Directory.rails_root
7
+ root_dir = File.join(Rails.application.config.root_dir, 'rails')
8
+
9
+ describe 'role strategy generator: admin_flag' do
10
+ describe 'ORM: mongoid' do
11
+ use_orm :mongoid
12
+
13
+ before :each do
14
+ setup_generator 'mongoid_roles_generator' do
15
+ tests Mongoid::Generators::RolesGenerator
16
+ end
17
+ remove_model :user
18
+ end
19
+
20
+ after :each do
21
+ # remove_model :user
22
+ end
23
+
24
+ it "should configure 'admin_flag' strategy" do
25
+ create_model :user do
26
+ '# content'
27
+ end
28
+
29
+ with_generator do |g|
30
+ arguments = "User --strategy admin_flag --roles admin user"
31
+ puts "arguments: #{arguments}"
32
+ g.run_generator arguments.args
33
+ root_dir.should have_model_file :user do |clazz|
34
+ clazz.should include_module 'Mongoid::Document'
35
+ clazz.should include_module 'Roles::Mongoid'
36
+ puts "clazz: #{clazz}"
37
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
38
+ clazz.should have_call :strategy, :args => ":admin_flag"
39
+ end
40
+ end
41
+ end
42
+
43
+ it "should configure 'one_role' strategy" do
44
+ create_model :user do
45
+ '# content'
46
+ end
47
+ # puts read_model :user
48
+
49
+ with_generator do |g|
50
+ arguments = "User --strategy one_role --roles admin user"
51
+ puts "arguments: #{arguments}"
52
+ g.run_generator arguments.args
53
+
54
+ # puts read_model :user
55
+
56
+ root_dir.should have_model_file :user do |clazz|
57
+ clazz.should include_module 'Mongoid::Document'
58
+ clazz.should include_module 'Roles::Mongoid'
59
+ puts "clazz: #{clazz}"
60
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
61
+ clazz.should have_call :strategy, :args => ":one_role"
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
@@ -0,0 +1,200 @@
1
+ describe "Roles for Mongoid: #{api_name}" do
2
+ require "roles_mongoid/strategy/user_setup.rb"
3
+
4
+ before do
5
+ default_user_setup
6
+ end
7
+
8
+ describe '#in_role' do
9
+ it "should return first user matching role" do
10
+ if User.respond_to? :in_role
11
+ User.in_role(:guest).first.name.should == 'Guest user'
12
+ User.in_role(:admin).first.name.should == 'Admin user'
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#in_any_role' do
18
+ it "should return first user matching role" do
19
+ if User.respond_to? :in_roles
20
+ User.in_any_role(:guest, :user).first.name.should == 'Guest user'
21
+ User.in_any_role(:admin, :guest).should be_empty
22
+ end
23
+ end
24
+ end
25
+
26
+ it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
27
+ # mutation API
28
+ [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
29
+ @admin_user.respond_to?(api_method).should be_true
30
+ end
31
+
32
+ # inspection API
33
+ [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
34
+ @admin_user.respond_to?(api_method).should be_true
35
+ end
36
+
37
+ # class method API
38
+ [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
39
+ @admin_user.class.respond_to?(class_api_method).should be_true
40
+ end
41
+ end
42
+ #
43
+ describe '#valid_role?' do
44
+ it "should be true that the admin user has a valid role of :guest" do
45
+ # @admin_user.valid_role?(:guest).should be_true
46
+ end
47
+
48
+ it "should be true that the User class has a valid role of :guest" do
49
+ # User.valid_role?(:guest).should be_true
50
+ end
51
+ end
52
+
53
+ describe '#valid_roles' do
54
+ it "should be true that the admin user has a valid role of :guest" do
55
+ # @admin_user.valid_roles.should include(:guest, :admin)
56
+ end
57
+
58
+ it "should be true that the User class has a valid role of :guest" do
59
+ User.valid_roles.should include(:guest, :admin)
60
+ end
61
+ end
62
+
63
+ describe '#valid_roles?' do
64
+ it "should be true that the admin user has a valid role of :guest" do
65
+ @admin_user.valid_roles?(:guest, :admin).should be_true
66
+ end
67
+
68
+ it "should be true that the User class has a valid role of :guest" do
69
+ User.valid_roles?(:guest, :admin).should be_true
70
+ end
71
+ end
72
+
73
+ describe '#has_role?' do
74
+ it "should have admin user role to :admin and not to :user" do
75
+ @admin_user.has_role?(:user).should be_false
76
+ @admin_user.has_role?(:admin).should be_true
77
+ end
78
+
79
+ it "should be true that guest user has role :guest and not :admin" do
80
+ @guest_user.has_role?(:guest).should be_true
81
+ @guest_user.has_role?(:admin).should be_false
82
+ end
83
+ end
84
+
85
+ describe '#has?' do
86
+ it "should be true that the admin_user has the :admin role" do
87
+ @admin_user.has?(:admin).should be_true
88
+ end
89
+
90
+ it "should NOT be true that the admin_user has the :admin role" do
91
+ @guest_user.has?(:admin).should be_false
92
+ end
93
+ end
94
+
95
+ describe '#has_roles?' do
96
+ it "should be true that the admin_user has the roles :admin" do
97
+ @admin_user.has_roles?(:admin).should be_true
98
+ end
99
+
100
+ it "should NOT be true that the user has the roles :admin" do
101
+ @guest_user.has_roles?(:admin).should be_false
102
+ end
103
+ end
104
+
105
+ describe '#roles_list' do
106
+ it "should be true that the first role of admin_user is the :admin role" do
107
+ @admin_user.roles_list.should include(:admin)
108
+ end
109
+
110
+ it "should be true that the first role of admin_user is the :user role" do
111
+ case @normal_user.class.role_strategy.multiplicity
112
+ when :single
113
+ @normal_user.roles_list.should include(:guest)
114
+ # @normal_user.roles_list.should include(:user)
115
+ when :multi
116
+ @normal_user.roles_list.should include(:user, :guest)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#roles' do
122
+ it "should be true that the roles of admin_user is an array with the role :admin" do
123
+ roles = @admin_user.roles
124
+ if defined?(Role) && roles.kind_of?(Role)
125
+ roles.name.to_sym.should == :admin
126
+ elsif roles.kind_of? Array
127
+ if @normal_user.class.role_strategy.type == :complex
128
+ roles.first.name.to_sym.should == :admin
129
+ end
130
+ if @normal_user.class.role_strategy.name == :admin_flag
131
+ roles.first.should == true
132
+ end
133
+ else
134
+ roles.to_sym.should == :admin
135
+ end
136
+ end
137
+ end
138
+
139
+ describe '#admin?' do
140
+ it "should be true that admin_user is in the :admin role" do
141
+ @admin_user.admin?.should be_true
142
+ end
143
+
144
+ it "should NOT be true that the user is in the :admin role" do
145
+ @guest_user.admin?.should be_false
146
+ end
147
+ end
148
+
149
+ describe '#is?' do
150
+ it "should be true that admin_user is in the :admin role" do
151
+ @admin_user.is?(:admin).should be_true
152
+ end
153
+
154
+ it "should NOT be true that the user is in the :admin role" do
155
+ @guest_user.is?(:admin).should be_false
156
+ end
157
+ end
158
+
159
+ describe '#roles=' do
160
+ it "should set user role to :admin" do
161
+ @guest_user.roles = :admin
162
+ @guest_user.has_role?(:admin).should be_true
163
+ @guest_user.roles = :guest
164
+ end
165
+ end
166
+
167
+ describe '#exchange_roles' do
168
+ it "should exchange user role :user with role :admin" do
169
+ @guest_user.exchange_role :guest, :with => :admin
170
+ @guest_user.has?(:guest).should be_false
171
+ @guest_user.has?(:admin).should be_true
172
+ end
173
+
174
+ it "should exchange user role :admin with roles :user and :guest" do
175
+ case @admin_user.class.role_strategy.multiplicity
176
+ when :single
177
+ lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
178
+ when :multi
179
+ @admin_user.exchange_role :admin, :with => [:user, :guest]
180
+ @admin_user.has_role?(:user).should be_true
181
+ @admin_user.has_role?(:guest).should be_true
182
+ @admin_user.has?(:admin).should be_false
183
+ end
184
+ end
185
+ end
186
+
187
+ describe '#remove_roles' do
188
+ it "should remove user role :admin using #remove_roles" do
189
+ @admin_user.remove_roles :admin
190
+ @admin_user.has_role?(:admin).should_not be_true
191
+ end
192
+
193
+ it "should remove user role :admin using #remove_role" do
194
+ @guest_user.add_role :admin
195
+ @guest_user.has_role?(:admin).should be_true
196
+ @guest_user.remove_role :admin
197
+ @guest_user.has_role?(:admin).should_not be_true
198
+ end
199
+ end
200
+ end