groupify 0.5.1 → 0.6.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.gitignore +1 -0
- data/.rspec +4 -0
- data/.travis.yml +7 -4
- data/Appraisals +24 -0
- data/Gemfile +6 -9
- data/README.md +181 -25
- data/Rakefile +1 -0
- data/gemfiles/rails_3.2.gemfile +18 -0
- data/gemfiles/rails_4.0.gemfile +18 -0
- data/gemfiles/rails_4.1.gemfile +18 -0
- data/groupify.gemspec +4 -4
- data/lib/groupify/adapter/active_record.rb +317 -59
- data/lib/groupify/adapter/mongoid.rb +304 -49
- data/lib/groupify/version.rb +1 -1
- data/spec/groupify/active_record_spec.rb +460 -148
- data/spec/groupify/mongoid_spec.rb +510 -162
- data/spec/spec_helper.rb +14 -3
- metadata +15 -13
- data/gemfiles/activesupport3.gemfile +0 -18
- data/gemfiles/activesupport4.gemfile +0 -24
- data/spec/groupify/mongoid.yml +0 -13
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
1
|
RSpec.configure do |config|
|
4
2
|
config.order = "random"
|
5
3
|
|
@@ -19,14 +17,19 @@ end
|
|
19
17
|
require 'mongoid'
|
20
18
|
puts "Mongoid version #{Mongoid::VERSION}"
|
21
19
|
|
22
|
-
require 'mongoid-rspec'
|
23
|
-
include Mongoid::Matchers
|
24
|
-
|
25
20
|
# Load mongoid config
|
26
|
-
Mongoid.load!('./spec/groupify/mongoid.yml', :test)
|
27
|
-
|
21
|
+
#Mongoid.load!('./spec/groupify/mongoid.yml', :test)
|
22
|
+
Mongoid::Config.sessions = {
|
23
|
+
default: {
|
24
|
+
database: 'groupify',
|
25
|
+
hosts: [ "localhost:27017" ]
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
if DEBUG
|
30
|
+
Moped.logger = Logger.new(STDOUT)
|
31
|
+
end
|
28
32
|
|
29
|
-
require 'groupify'
|
30
33
|
require 'groupify/adapter/mongoid'
|
31
34
|
|
32
35
|
class MongoidUser
|
@@ -39,6 +42,11 @@ end
|
|
39
42
|
class MongoidManager < MongoidUser
|
40
43
|
end
|
41
44
|
|
45
|
+
class MongoidWidget
|
46
|
+
include Mongoid::Document
|
47
|
+
acts_as_group_member :class_name => 'MongoidGroup'
|
48
|
+
end
|
49
|
+
|
42
50
|
class MongoidTask
|
43
51
|
include Mongoid::Document
|
44
52
|
|
@@ -54,203 +62,543 @@ end
|
|
54
62
|
class MongoidGroup
|
55
63
|
include Mongoid::Document
|
56
64
|
|
57
|
-
acts_as_group :members => [:mongoid_users, :mongoid_tasks], :default_members => :mongoid_users
|
65
|
+
acts_as_group :members => [:mongoid_users, :mongoid_tasks, :mongoid_widgets], :default_members => :mongoid_users
|
58
66
|
alias_method :users, :mongoid_users
|
59
67
|
alias_method :tasks, :mongoid_tasks
|
68
|
+
alias_method :widgets, :mongoid_widgets
|
60
69
|
end
|
61
70
|
|
62
71
|
class MongoidProject < MongoidGroup
|
72
|
+
acts_as_named_group_member
|
73
|
+
|
63
74
|
has_members :mongoid_issues
|
64
75
|
has_members :mongoid_managers
|
65
76
|
alias_method :issues, :mongoid_issues
|
66
77
|
alias_method :managers, :mongoid_managers
|
67
78
|
end
|
68
79
|
|
69
|
-
describe MongoidGroup do
|
70
|
-
it { should respond_to :members}
|
71
|
-
it { should respond_to :add }
|
72
|
-
end
|
73
|
-
|
74
|
-
describe MongoidUser do
|
75
|
-
it { should respond_to :groups}
|
76
|
-
it { should respond_to :in_group?}
|
77
|
-
it { should respond_to :in_any_group?}
|
78
|
-
it { should respond_to :in_all_groups?}
|
79
|
-
it { should respond_to :shares_any_group?}
|
80
|
-
|
81
|
-
# Mongoid specific
|
82
|
-
it { should have_and_belong_to_many(:groups).of_type(MongoidGroup) }
|
83
|
-
end
|
84
|
-
|
85
80
|
describe Groupify::Mongoid do
|
86
81
|
let!(:user) { MongoidUser.create! }
|
87
82
|
let!(:group) { MongoidGroup.create! }
|
83
|
+
let(:widget) { MongoidWidget.create! }
|
88
84
|
let(:task) { MongoidTask.create! }
|
89
85
|
let(:issue) { MongoidIssue.create! }
|
90
86
|
let(:manager) { MongoidManager.create! }
|
91
87
|
|
92
|
-
|
93
|
-
|
94
|
-
|
88
|
+
context 'when using groups' do
|
89
|
+
it "members and groups are empty when initialized" do
|
90
|
+
expect(user.groups).to be_empty
|
91
|
+
expect(MongoidUser.new.groups).to be_empty
|
95
92
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
93
|
+
expect(MongoidGroup.new.members).to be_empty
|
94
|
+
expect(group.members).to be_empty
|
95
|
+
end
|
96
|
+
|
97
|
+
it "adds a group to a member" do
|
98
|
+
user.groups << group
|
99
|
+
expect(user.groups).to include(group)
|
100
|
+
expect(group.members).to include(user)
|
101
|
+
expect(group.users).to include(user)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "adds a member to a group" do
|
105
|
+
expect(user.groups).to be_empty
|
106
|
+
group.add user
|
107
|
+
expect(user.groups).to include(group)
|
108
|
+
expect(group.members).to include(user)
|
109
|
+
end
|
112
110
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
111
|
+
it "adds multiple members to a group" do
|
112
|
+
group.add(user, task)
|
113
|
+
expect(group.users).to include(user)
|
114
|
+
expect(group.tasks).to include(task)
|
117
115
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
users = [MongoidUser.create!, MongoidUser.create!]
|
117
|
+
group.add(users)
|
118
|
+
expect(group.users).to include(*users)
|
119
|
+
end
|
122
120
|
|
123
|
-
|
124
|
-
|
125
|
-
|
121
|
+
it 'lists which member classes can belong to this group' do
|
122
|
+
expect(group.class.member_classes).to include(MongoidUser, MongoidTask)
|
123
|
+
expect(group.member_classes).to include(MongoidUser, MongoidTask)
|
126
124
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
it "finds members by group" do
|
131
|
-
group.add user
|
125
|
+
expect(MongoidProject.member_classes).to include(MongoidUser, MongoidTask, MongoidIssue)
|
126
|
+
end
|
132
127
|
|
133
|
-
|
134
|
-
|
128
|
+
it "finds members by group" do
|
129
|
+
group.add user
|
130
|
+
|
131
|
+
expect(MongoidUser.in_group(group).first).to eql(user)
|
132
|
+
end
|
135
133
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
134
|
+
it "finds the groups a member belongs to" do
|
135
|
+
group.add user
|
136
|
+
|
137
|
+
expect(MongoidGroup.with_member(user).first).to eq(group)
|
138
|
+
end
|
141
139
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
group.users.should_not include(user)
|
147
|
-
end
|
140
|
+
context 'when removing' do
|
141
|
+
it "removes members from a group" do
|
142
|
+
group.add user
|
143
|
+
group.add widget
|
148
144
|
|
149
|
-
|
150
|
-
|
151
|
-
group.add task
|
152
|
-
group.destroy
|
145
|
+
group.users.delete(user)
|
146
|
+
group.widgets.destroy(widget)
|
153
147
|
|
154
|
-
|
155
|
-
|
148
|
+
expect(group.widgets).to_not include(widget)
|
149
|
+
expect(group.users).to_not include(user)
|
156
150
|
|
157
|
-
|
158
|
-
|
159
|
-
|
151
|
+
expect(widget.groups).to_not include(group)
|
152
|
+
expect(user.groups).to_not include(group)
|
153
|
+
end
|
160
154
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
destination = MongoidProject.create!
|
155
|
+
it "removes groups from a member" do
|
156
|
+
group.add widget
|
157
|
+
group.add user
|
165
158
|
|
166
|
-
|
167
|
-
|
168
|
-
destination.add(task)
|
159
|
+
user.groups.delete(group)
|
160
|
+
widget.groups.destroy(group)
|
169
161
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
162
|
+
expect(group.widgets).to_not include(widget)
|
163
|
+
expect(group.users).to_not include(user)
|
164
|
+
|
165
|
+
expect(widget.groups).to_not include(group)
|
166
|
+
expect(user.groups).to_not include(group)
|
167
|
+
end
|
168
|
+
|
169
|
+
it "removes the membership relation when a member is destroyed" do
|
170
|
+
group.add user
|
171
|
+
user.destroy
|
172
|
+
expect(group).not_to be_destroyed
|
173
|
+
expect(group.users).not_to include(user)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "removes the membership relations when a group is destroyed" do
|
177
|
+
group.add user
|
178
|
+
group.add task
|
179
|
+
group.destroy
|
180
|
+
|
181
|
+
expect(user).not_to be_destroyed
|
182
|
+
expect(user.reload.groups).to be_empty
|
183
|
+
|
184
|
+
expect(task).not_to be_destroyed
|
185
|
+
expect(task.reload.groups).to be_empty
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context 'when checking group membership' do
|
190
|
+
it "members can check if they belong to any/all groups" do
|
191
|
+
user.groups << group
|
192
|
+
group2 = MongoidGroup.create!
|
193
|
+
user.groups << group2
|
194
|
+
group3 = MongoidGroup.create!
|
195
|
+
|
196
|
+
expect(user.groups).to include(group)
|
197
|
+
expect(user.groups).to include(group2)
|
198
|
+
|
199
|
+
expect(MongoidUser.in_group(group).first).to eql(user)
|
200
|
+
expect(MongoidUser.in_group(group2).first).to eql(user)
|
201
|
+
expect(user.in_group?(group)).to be true
|
202
|
+
|
203
|
+
expect(MongoidUser.in_any_group(group).first).to eql(user)
|
204
|
+
expect(MongoidUser.in_any_group(group3)).to be_empty
|
205
|
+
expect(user.in_any_group?(group2, group3)).to be true
|
206
|
+
expect(user.in_any_group?(group3)).to be false
|
207
|
+
|
208
|
+
expect(MongoidUser.in_all_groups(group, group2).first).to eql(user)
|
209
|
+
expect(MongoidUser.in_all_groups([group, group2]).first).to eql(user)
|
210
|
+
expect(user.in_all_groups?(group, group2)).to be true
|
211
|
+
expect(user.in_all_groups?(group, group3)).to be false
|
212
|
+
end
|
213
|
+
|
214
|
+
it "members can check if groups are shared" do
|
215
|
+
user.groups << group
|
216
|
+
widget.groups << group
|
217
|
+
user2 = MongoidUser.create!(:groups => [group])
|
218
|
+
|
219
|
+
expect(user.shares_any_group?(widget)).to be true
|
220
|
+
expect(MongoidWidget.shares_any_group(user).to_a).to include(widget)
|
221
|
+
expect(MongoidUser.shares_any_group(widget).to_a).to include(user, user2)
|
222
|
+
|
223
|
+
expect(user.shares_any_group?(user2)).to be true
|
224
|
+
expect(MongoidUser.shares_any_group(user).to_a).to include(user2)
|
225
|
+
end
|
176
226
|
end
|
177
227
|
|
178
|
-
|
179
|
-
source
|
180
|
-
|
228
|
+
context 'when merging' do
|
229
|
+
it "moves the members from source to destination and destroys the source" do
|
230
|
+
source = MongoidGroup.create!
|
231
|
+
destination = MongoidProject.create!
|
232
|
+
|
233
|
+
source.add(user)
|
234
|
+
source.add(manager)
|
235
|
+
destination.add(task)
|
236
|
+
|
237
|
+
destination.merge!(source)
|
238
|
+
expect(source.destroyed?).to be true
|
239
|
+
|
240
|
+
expect(destination.users.to_a).to include(user)
|
241
|
+
expect(destination.managers.to_a).to include(manager)
|
242
|
+
expect(destination.tasks.to_a).to include(task)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "fails to merge if the destination group cannot contain the source group's members" do
|
246
|
+
source = MongoidProject.create!
|
247
|
+
destination = MongoidGroup.create!
|
181
248
|
|
182
|
-
|
183
|
-
|
249
|
+
source.add(issue)
|
250
|
+
destination.add(user)
|
184
251
|
|
185
|
-
|
186
|
-
|
252
|
+
# Issues cannot be members of a MongoidGroup
|
253
|
+
expect {destination.merge!(source)}.to raise_error(ArgumentError)
|
254
|
+
end
|
255
|
+
|
256
|
+
it "merges incompatible groups as long as all the source members can be moved to the destination" do
|
257
|
+
source = MongoidProject.create!
|
258
|
+
destination = MongoidGroup.create!
|
259
|
+
|
260
|
+
source.add(user)
|
261
|
+
destination.add(task)
|
262
|
+
|
263
|
+
expect {destination.merge!(source)}.to_not raise_error
|
264
|
+
|
265
|
+
expect(source.destroyed?).to be true
|
266
|
+
expect(destination.users.to_a).to include(user)
|
267
|
+
expect(destination.tasks.to_a).to include(task)
|
268
|
+
end
|
187
269
|
end
|
188
270
|
|
189
|
-
it "
|
190
|
-
|
191
|
-
|
271
|
+
it "members can belong to many groups" do
|
272
|
+
user.groups << group
|
273
|
+
group2 = MongoidGroup.create!
|
274
|
+
user.groups << group2
|
192
275
|
|
193
|
-
|
194
|
-
destination.add(task)
|
276
|
+
group3 = user.groups.create!
|
195
277
|
|
196
|
-
|
278
|
+
user.save!
|
197
279
|
|
198
|
-
|
199
|
-
|
200
|
-
|
280
|
+
group4 = MongoidGroup.create!
|
281
|
+
|
282
|
+
expect(user.groups).to include(group, group2, group3)
|
283
|
+
|
284
|
+
expect(MongoidUser.in_group(group).first).to eql(user)
|
285
|
+
expect(MongoidUser.in_group(group2).first).to eql(user)
|
286
|
+
expect(user.in_group?(group)).to be true
|
287
|
+
expect(user.in_group?(group4)).to be false
|
288
|
+
|
289
|
+
expect(MongoidUser.in_any_group(group, group4).first).to eql(user)
|
290
|
+
expect(MongoidUser.in_any_group(group4)).to be_empty
|
291
|
+
expect(user.in_any_group?(group2, group4)).to be true
|
292
|
+
expect(user.in_any_group?(group4)).to be false
|
293
|
+
|
294
|
+
expect(MongoidUser.in_all_groups(group, group2).first).to eql(user)
|
295
|
+
expect(MongoidUser.in_all_groups([group, group3]).first).to eql(user)
|
296
|
+
expect(MongoidUser.in_all_groups([group2, group4])).to be_empty
|
297
|
+
expect(user.in_all_groups?(group, group3)).to be true
|
298
|
+
expect(user.in_all_groups?(group, group4)).to be false
|
299
|
+
|
300
|
+
expect(MongoidUser.in_only_groups(group, group2, group3).first).to eql(user)
|
301
|
+
expect(MongoidUser.in_only_groups(group, group2, group3, group4)).to be_empty
|
302
|
+
expect(MongoidUser.in_only_groups(group, group2)).to be_empty
|
303
|
+
expect(user.in_only_groups?(group, group2, group3)).to be true
|
304
|
+
expect(user.in_only_groups?(group, group2)).to be false
|
305
|
+
expect(user.in_only_groups?(group, group2, group3, group4)).to be false
|
201
306
|
end
|
202
307
|
end
|
203
308
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
309
|
+
context "when using membership types with groups" do
|
310
|
+
it 'adds groups to a member with a specific membership type' do
|
311
|
+
group.add(user, as: :admin)
|
312
|
+
|
313
|
+
expect(user.groups).to include(group)
|
314
|
+
expect(group.members).to include(user)
|
315
|
+
expect(group.users).to include(user)
|
316
|
+
|
317
|
+
expect(user.groups(:as => :admin)).to include(group)
|
318
|
+
expect(user.groups.as(:admin)).to include(group)
|
319
|
+
expect(group.members).to include(user)
|
320
|
+
expect(group.users).to include(user)
|
321
|
+
end
|
322
|
+
|
323
|
+
it 'adds members to a group with specific membership types' do
|
324
|
+
group.add(user, as: 'manager')
|
325
|
+
group.add(widget)
|
326
|
+
|
327
|
+
expect(user.groups).to include(group)
|
328
|
+
expect(group.members).to include(user)
|
329
|
+
expect(group.members.as(:manager)).to include(user)
|
330
|
+
expect(group.users).to include(user)
|
331
|
+
|
332
|
+
expect(user.groups(:as => :manager)).to include(group)
|
333
|
+
expect(group.members).to include(user)
|
334
|
+
expect(group.users).to include(user)
|
335
|
+
end
|
336
|
+
|
337
|
+
it "adds multiple members to a group with a specific membership type" do
|
338
|
+
manager = MongoidUser.create!
|
339
|
+
group.add(user, manager, as: :manager)
|
340
|
+
|
341
|
+
expect(group.users.as(:manager)).to include(user, manager)
|
342
|
+
expect(group.users(as: :manager)).to include(user, manager)
|
343
|
+
end
|
344
|
+
|
345
|
+
it "finds members by membership type" do
|
346
|
+
group.add user, as: 'manager'
|
347
|
+
expect(MongoidUser.as(:manager)).to include(user)
|
348
|
+
end
|
349
|
+
|
350
|
+
it "finds members by group with membership type" do
|
351
|
+
group.add user, as: 'employee'
|
352
|
+
manager = MongoidUser.create!
|
353
|
+
group.add(user, manager, as: :manager)
|
354
|
+
|
355
|
+
employees = MongoidUser.in_group(group).as('employee')
|
356
|
+
expect(employees).to include(user)
|
357
|
+
expect(employees).to_not include(manager)
|
358
|
+
end
|
359
|
+
|
360
|
+
it "finds the group a member belongs to with a membership type" do
|
361
|
+
group.add user, as: MongoidManager
|
362
|
+
user.groups.create!
|
363
|
+
|
364
|
+
expect(MongoidGroup.with_member(user).as(MongoidManager).to_a).to eq([group])
|
365
|
+
end
|
366
|
+
|
367
|
+
it "checks if members belong to any groups with a certain membership type" do
|
368
|
+
group2 = MongoidGroup.create!
|
369
|
+
group.add(user, as: 'employee')
|
370
|
+
group2.add(user)
|
371
|
+
|
372
|
+
expect(MongoidUser.in_any_group(group, group2).as('employee').first).to eql(user)
|
373
|
+
end
|
374
|
+
|
375
|
+
it "checks if members belong to all groups with a certain membership type" do
|
376
|
+
group2 = MongoidGroup.create!
|
377
|
+
group3 = MongoidGroup.create!
|
378
|
+
group.add(user, as: 'employee')
|
379
|
+
group2.add(user, as: 'employee')
|
380
|
+
group3.add(user, as: 'contractor')
|
381
|
+
|
382
|
+
expect(MongoidUser.in_all_groups(group, group2).as('employee').first).to eql(user)
|
383
|
+
expect(MongoidUser.in_all_groups([group, group2]).as('employee').first).to eql(user)
|
384
|
+
expect(MongoidUser.in_all_groups(group, group3).as('employee')).to be_empty
|
385
|
+
|
386
|
+
expect(user.in_all_groups?(group, group2, group3)).to be true
|
387
|
+
expect(user.in_all_groups?(group, group2, as: :employee)).to be true
|
388
|
+
expect(user.in_all_groups?(group, group3, as: 'employee')).to be false
|
389
|
+
end
|
390
|
+
|
391
|
+
it "checks if members belong to only groups with a certain membership type" do
|
392
|
+
group2 = MongoidGroup.create!
|
393
|
+
group3 = MongoidGroup.create!
|
394
|
+
group4 = MongoidGroup.create!
|
395
|
+
|
396
|
+
group.add(user, as: 'employee')
|
397
|
+
group2.add(user, as: 'employee')
|
398
|
+
group3.add(user, as: 'contractor')
|
399
|
+
group4.add(user, as: 'employee')
|
400
|
+
|
401
|
+
expect(MongoidUser.in_only_groups(group, group2, group4).as('employee').first).to eql(user)
|
402
|
+
expect(MongoidUser.in_only_groups([group, group2]).as('employee')).to be_empty
|
403
|
+
expect(MongoidUser.in_only_groups(group, group2, group3, group4).as('employee')).to be_empty
|
404
|
+
|
405
|
+
expect(user.in_only_groups?(group, group2, group3, group4))
|
406
|
+
expect(user.in_only_groups?(group, group2, group4, as: 'employee')).to be true
|
407
|
+
expect(user.in_only_groups?(group, group2, as: 'employee')).to be false
|
408
|
+
expect(user.in_only_groups?(group, group2, group3, group4, as: 'employee')).to be false
|
409
|
+
end
|
410
|
+
|
411
|
+
it "members can check if groups are shared with the same membership type" do
|
412
|
+
user2 = MongoidUser.create!
|
413
|
+
group.add(user, user2, widget, as: "Sub Group #1")
|
414
|
+
|
415
|
+
expect(user.shares_any_group?(widget, as: "Sub Group #1")).to be true
|
416
|
+
expect(MongoidWidget.shares_any_group(user).as("Sub Group #1").to_a).to include(widget)
|
417
|
+
expect(MongoidUser.shares_any_group(widget).as("Sub Group #1").to_a).to include(user, user2)
|
418
|
+
|
419
|
+
expect(user.shares_any_group?(user2, as: "Sub Group #1")).to be true
|
420
|
+
expect(MongoidUser.shares_any_group(user).as("Sub Group #1").to_a).to include(user2)
|
421
|
+
end
|
422
|
+
|
423
|
+
context "when removing" do
|
424
|
+
before(:each) do
|
425
|
+
group.add user, as: 'employee'
|
426
|
+
group.add user, as: 'manager'
|
427
|
+
end
|
428
|
+
|
429
|
+
it "removes all membership types when removing a member from a group" do
|
430
|
+
group.add user
|
431
|
+
|
432
|
+
group.users.destroy(user)
|
433
|
+
|
434
|
+
expect(user.groups).to_not include(group)
|
435
|
+
expect(user.groups.as('manager')).to_not include(group)
|
436
|
+
expect(user.groups.as('employee')).to_not include(group)
|
437
|
+
expect(group.users).to_not include(user)
|
438
|
+
end
|
439
|
+
|
440
|
+
it "removes a specific membership type from the member side" do
|
441
|
+
user.groups.destroy(group, as: 'manager')
|
442
|
+
expect(user.groups.as('manager')).to be_empty
|
443
|
+
expect(user.groups.as('employee')).to include(group)
|
444
|
+
expect(user.groups).to include(group)
|
445
|
+
end
|
446
|
+
|
447
|
+
it "removes a specific membership type from the group side" do
|
448
|
+
group.users.delete(user, as: :manager)
|
449
|
+
expect(user.groups.as('manager')).to be_empty
|
450
|
+
expect(user.groups.as('employee')).to include(group)
|
451
|
+
expect(user.groups).to include(group)
|
452
|
+
end
|
453
|
+
|
454
|
+
it "retains the member in the group if all membership types have been removed" do
|
455
|
+
user.groups.delete(group, as: 'manager')
|
456
|
+
user.groups.delete(group, as: 'employee')
|
457
|
+
expect(user.groups).to include(group)
|
458
|
+
end
|
459
|
+
end
|
247
460
|
end
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
461
|
+
|
462
|
+
context 'when using named groups' do
|
463
|
+
before(:each) do
|
464
|
+
user.named_groups.concat :admin, :user, :poster
|
465
|
+
user.save!
|
466
|
+
end
|
467
|
+
|
468
|
+
it "enforces uniqueness" do
|
469
|
+
user.named_groups << :admin
|
470
|
+
user.save!
|
471
|
+
expect(user.named_groups.count{|g| g == :admin}).to eq(1)
|
472
|
+
end
|
473
|
+
|
474
|
+
it "queries named groups" do
|
475
|
+
expect(user.named_groups).to include(:user, :admin)
|
476
|
+
end
|
477
|
+
|
478
|
+
it "removes named groups" do
|
479
|
+
user.named_groups.delete(:admin, :poster)
|
480
|
+
expect(user.named_groups).to include(:user)
|
481
|
+
expect(user.named_groups).to_not include(:admin, :poster)
|
482
|
+
|
483
|
+
user.named_groups.destroy(:user)
|
484
|
+
expect(user.named_groups).to be_empty
|
485
|
+
end
|
486
|
+
|
487
|
+
it "removes all named groups" do
|
488
|
+
user.named_groups.destroy_all
|
489
|
+
expect(user.named_groups).to be_empty
|
490
|
+
end
|
491
|
+
|
492
|
+
it "checks if a member belongs to one named group" do
|
493
|
+
expect(user.in_named_group?(:admin)).to be true
|
494
|
+
expect(MongoidUser.in_named_group(:admin).first).to eql(user)
|
495
|
+
end
|
496
|
+
|
497
|
+
it "checks if a member belongs to any named group" do
|
498
|
+
expect(user.in_any_named_group?(:admin, :user, :test)).to be true
|
499
|
+
expect(user.in_any_named_group?(:foo, :bar)).to be false
|
500
|
+
|
501
|
+
expect(MongoidUser.in_any_named_group(:admin, :test).first).to eql(user)
|
502
|
+
expect(MongoidUser.in_any_named_group(:test)).to be_empty
|
503
|
+
end
|
504
|
+
|
505
|
+
it "checks if a member belongs to all named groups" do
|
506
|
+
expect(user.in_all_named_groups?(:admin, :user)).to be true
|
507
|
+
expect(user.in_all_named_groups?(:admin, :user, :test)).to be false
|
508
|
+
expect(MongoidUser.in_all_named_groups(:admin, :user).first).to eql(user)
|
509
|
+
end
|
510
|
+
|
511
|
+
it "checks if a member belongs to only certain named groups" do
|
512
|
+
expect(user.in_only_named_groups?(:admin, :user, :poster)).to be true
|
513
|
+
expect(user.in_only_named_groups?(:admin, :user, :poster, :foo)).to be false
|
514
|
+
expect(user.in_only_named_groups?(:admin, :user)).to be false
|
515
|
+
expect(user.in_only_named_groups?(:admin, :user, :test)).to be false
|
516
|
+
|
517
|
+
expect(MongoidUser.in_only_named_groups(:admin, :user, :poster).first).to eql(user)
|
518
|
+
expect(MongoidUser.in_only_named_groups(:admin, :user, :poster, :foo)).to be_empty
|
519
|
+
expect(MongoidUser.in_only_named_groups(:admin)).to be_empty
|
520
|
+
end
|
521
|
+
|
522
|
+
it "checks if named groups are shared" do
|
523
|
+
user2 = MongoidUser.create!(:named_groups => [:admin])
|
524
|
+
|
525
|
+
expect(user.shares_any_named_group?(user2)).to be true
|
526
|
+
expect(MongoidUser.shares_any_named_group(user).to_a).to include(user2)
|
527
|
+
end
|
528
|
+
|
529
|
+
context 'and using membership types with named groups' do
|
530
|
+
before(:each) do
|
531
|
+
user.named_groups.concat :team1, :team2, as: 'employee'
|
532
|
+
user.named_groups.push :team3, as: 'manager'
|
533
|
+
user.named_groups.push :team1, as: 'developer'
|
534
|
+
end
|
535
|
+
|
536
|
+
it "queries named groups, filtering by membership type" do
|
537
|
+
expect(user.named_groups).to include(:team1, :team2, :team3)
|
538
|
+
expect(user.named_groups.as('manager')).to eq([:team3])
|
539
|
+
end
|
540
|
+
|
541
|
+
it "enforces uniqueness of named groups" do
|
542
|
+
user.named_groups << :team1
|
543
|
+
expect(user.named_groups.count{|g| g == :team1}).to eq(1)
|
544
|
+
expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
|
545
|
+
end
|
546
|
+
|
547
|
+
it "enforces uniqueness of group name and membership type for group memberships" do
|
548
|
+
user.named_groups.push :team1, as: 'employee'
|
549
|
+
expect(user.named_groups.count{|g| g == :team1}).to eq(1)
|
550
|
+
expect(user.named_groups.as('employee').count{|g| g == :team1}).to eq(1)
|
551
|
+
end
|
552
|
+
|
553
|
+
it "checks if a member belongs to one named group with a certain membership type" do
|
554
|
+
expect(user.in_named_group?(:team1, as: 'employee')).to be true
|
555
|
+
expect(MongoidUser.in_named_group(:team1).as('employee').first).to eql(user)
|
556
|
+
end
|
557
|
+
|
558
|
+
it "checks if a member belongs to any named group with a certain membership type" do
|
559
|
+
expect(user.in_any_named_group?(:team1, :team3, as: 'employee')).to be true
|
560
|
+
expect(MongoidUser.in_any_named_group(:team2, :team3).as('manager').first).to eql(user)
|
561
|
+
end
|
562
|
+
|
563
|
+
it "checks if a member belongs to all named groups with a certain membership type" do
|
564
|
+
expect(user.in_all_named_groups?(:team1, :team2, as: 'employee')).to be true
|
565
|
+
expect(user.in_all_named_groups?(:team1, :team3, as: 'employee')).to be false
|
566
|
+
expect(MongoidUser.in_all_named_groups(:team1, :team2).as('employee').first).to eql(user)
|
567
|
+
end
|
568
|
+
|
569
|
+
it "checks if a member belongs to only certain named groups with a certain membership type" do
|
570
|
+
expect(user.in_only_named_groups?(:team1, :team2, as: 'employee')).to be true
|
571
|
+
expect(user.in_only_named_groups?(:team1, as: 'employee')).to be false
|
572
|
+
expect(user.in_only_named_groups?(:team1, :team3, as: 'employee')).to be false
|
573
|
+
expect(user.in_only_named_groups?(:foo, as: 'employee')).to be false
|
574
|
+
|
575
|
+
expect(MongoidUser.in_only_named_groups(:team1, :team2).as('employee').first).to eql(user)
|
576
|
+
expect(MongoidUser.in_only_named_groups(:team1).as('employee')).to be_empty
|
577
|
+
expect(MongoidUser.in_only_named_groups(:foo).as('employee')).to be_empty
|
578
|
+
end
|
579
|
+
|
580
|
+
it "checks if a member shares any named groups with a certain membership type" do
|
581
|
+
project = MongoidProject.create!(:named_groups => [:team3])
|
582
|
+
|
583
|
+
expect(user.shares_any_named_group?(project, as: 'manager')).to be true
|
584
|
+
expect(MongoidUser.shares_any_named_group(project).as('manager').to_a).to include(user)
|
585
|
+
end
|
586
|
+
|
587
|
+
it "removes named groups with a certain membership type" do
|
588
|
+
user.named_groups.delete(:team1, as: :employee)
|
589
|
+
expect(user.named_groups.as(:employee)).to include(:team2)
|
590
|
+
expect(user.named_groups.as(:employee)).to_not include(:team1)
|
591
|
+
expect(user.named_groups.as(:developer)).to include(:team1)
|
592
|
+
expect(user.named_groups).to include(:team1)
|
593
|
+
end
|
594
|
+
|
595
|
+
it "removes all named group memberships if membership type is not specified" do
|
596
|
+
user.named_groups.destroy(:team1)
|
597
|
+
expect(user.named_groups).to_not include(:team1)
|
598
|
+
expect(user.named_groups.as(:employee)).to_not include(:team1)
|
599
|
+
expect(user.named_groups.as(:developer)).to_not include(:team1)
|
600
|
+
expect(user.named_groups.as(:employee)).to include(:team2)
|
601
|
+
end
|
602
|
+
end
|
255
603
|
end
|
256
|
-
end
|
604
|
+
end
|