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