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
data/lib/groupify/version.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
108
|
-
|
109
|
-
|
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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
144
|
+
users = [User.create!, User.create!]
|
145
|
+
group.add(users)
|
146
|
+
expect(group.users).to include(*users)
|
147
|
+
end
|
137
148
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
-
|
149
|
-
|
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
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
165
|
-
|
166
|
-
group.add widget
|
167
|
-
group.destroy
|
173
|
+
group.users.delete(user)
|
174
|
+
group.widgets.destroy(widget)
|
168
175
|
|
169
|
-
|
170
|
-
|
176
|
+
expect(group.widgets).to_not include(widget)
|
177
|
+
expect(group.users).to_not include(user)
|
171
178
|
|
172
|
-
|
173
|
-
|
174
|
-
|
179
|
+
expect(widget.groups).to_not include(group)
|
180
|
+
expect(user.groups).to_not include(group)
|
181
|
+
end
|
175
182
|
|
176
|
-
|
177
|
-
|
178
|
-
|
183
|
+
it "removes groups from a member" do
|
184
|
+
group.add widget
|
185
|
+
group.add user
|
179
186
|
|
180
|
-
|
181
|
-
|
182
|
-
destination = Organization.create!
|
187
|
+
user.groups.delete(group)
|
188
|
+
widget.groups.destroy(group)
|
183
189
|
|
184
|
-
|
185
|
-
|
190
|
+
expect(group.widgets).to_not include(widget)
|
191
|
+
expect(group.users).to_not include(user)
|
186
192
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
-
|
195
|
-
|
196
|
-
destination = Group.create!
|
209
|
+
expect(user).not_to be_destroyed
|
210
|
+
expect(user.reload.groups).to be_empty
|
197
211
|
|
198
|
-
|
199
|
-
|
212
|
+
expect(widget).not_to be_destroyed
|
213
|
+
expect(widget.reload.groups).to be_empty
|
214
|
+
end
|
215
|
+
end
|
200
216
|
|
201
|
-
|
202
|
-
|
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
|
-
|
206
|
-
|
207
|
-
|
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
|
-
|
210
|
-
|
278
|
+
source.add(manager)
|
279
|
+
destination.add(user)
|
211
280
|
|
212
|
-
|
281
|
+
# Managers cannot be members of a Group
|
282
|
+
expect {destination.merge!(source)}.to raise_error(ArgumentError)
|
283
|
+
end
|
213
284
|
|
214
|
-
source
|
215
|
-
|
216
|
-
|
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
|
-
|
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
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
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
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
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
|
-
|
587
|
+
|
588
|
+
|
589
|
+
end
|