hydra-works 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,6 +28,30 @@ module Hydra::Works
28
28
  type [Hydra::PCDM::Vocab::PCDMTerms.Collection, Vocab::WorksTerms.Collection]
29
29
  end
30
30
 
31
+ def parent_collections
32
+ in_collections + member_of_collections
33
+ end
34
+
35
+ def parent_collection_ids
36
+ in_collection_ids + member_of_collection_ids
37
+ end
38
+
39
+ def child_collections
40
+ collections + member_collections
41
+ end
42
+
43
+ def child_collection_ids
44
+ collection_ids + member_collection_ids
45
+ end
46
+
47
+ def child_works
48
+ works + member_works
49
+ end
50
+
51
+ def child_work_ids
52
+ work_ids + member_work_ids
53
+ end
54
+
31
55
  def ordered_works
32
56
  ordered_members.to_a.select(&:work?)
33
57
  end
@@ -44,6 +68,26 @@ module Hydra::Works
44
68
  works.map(&:id)
45
69
  end
46
70
 
71
+ def member_collections
72
+ return [] if id.nil?
73
+ member_objects = ActiveFedora::Base.where('member_of_collection_ids_ssim' => id)
74
+ member_objects.select(&:collection?).to_a
75
+ end
76
+
77
+ def member_collection_ids
78
+ member_collections.map(&:id)
79
+ end
80
+
81
+ def member_works
82
+ return [] if id.nil?
83
+ member_objects = ActiveFedora::Base.where('member_of_collection_ids_ssim' => id)
84
+ member_objects.select(&:work?).to_a
85
+ end
86
+
87
+ def member_work_ids
88
+ member_works.map(&:id)
89
+ end
90
+
47
91
  # @return [Boolean] whether this instance is a Hydra::Works Collection.
48
92
  def collection?
49
93
  true
@@ -43,10 +43,30 @@ module Hydra::Works
43
43
  true
44
44
  end
45
45
 
46
+ def parent_works
47
+ in_works + member_of_works
48
+ end
49
+
50
+ def parent_work_ids
51
+ in_work_ids + member_of_work_ids
52
+ end
53
+
46
54
  def in_works
47
55
  ordered_by.select { |parent| parent.class.included_modules.include?(Hydra::Works::WorkBehavior) }.to_a
48
56
  end
49
57
 
58
+ def in_work_ids
59
+ in_works.map(&:id)
60
+ end
61
+
62
+ def member_of_works
63
+ member_of.select(&:work?).to_a
64
+ end
65
+
66
+ def member_of_work_ids
67
+ member_of_works.map(&:id)
68
+ end
69
+
50
70
  private
51
71
 
52
72
  def remove_from_works
@@ -29,6 +29,38 @@ module Hydra::Works
29
29
  type [Hydra::PCDM::Vocab::PCDMTerms.Object, Vocab::WorksTerms.Work]
30
30
  end
31
31
 
32
+ def parent_collections
33
+ in_collections + member_of_collections
34
+ end
35
+
36
+ def parent_collection_ids
37
+ in_collection_ids + member_of_collection_ids
38
+ end
39
+
40
+ def parent_works
41
+ in_works + member_of_works
42
+ end
43
+
44
+ def parent_work_ids
45
+ in_work_ids + member_of_work_ids
46
+ end
47
+
48
+ def child_works
49
+ works + member_works
50
+ end
51
+
52
+ def child_work_ids
53
+ work_ids + member_work_ids
54
+ end
55
+
56
+ def child_file_sets
57
+ file_sets
58
+ end
59
+
60
+ def child_file_set_ids
61
+ file_set_ids
62
+ end
63
+
32
64
  def works
33
65
  members.select(&:work?)
34
66
  end
@@ -37,6 +69,16 @@ module Hydra::Works
37
69
  works.map(&:id)
38
70
  end
39
71
 
72
+ def member_works
73
+ return [] if id.nil?
74
+ member_objects = ActiveFedora::Base.where('object_ids_ssim' => id)
75
+ member_objects.select(&:work?).to_a
76
+ end
77
+
78
+ def member_work_ids
79
+ member_works.map(&:id)
80
+ end
81
+
40
82
  def ordered_works
41
83
  ordered_members.to_a.select(&:work?)
42
84
  end
@@ -82,6 +124,18 @@ module Hydra::Works
82
124
  ordered_by.select { |parent| parent.class.included_modules.include?(Hydra::Works::WorkBehavior) }.to_a
83
125
  end
84
126
 
127
+ def in_work_ids
128
+ in_works.map(&:id)
129
+ end
130
+
131
+ def member_of_works
132
+ in_objects.to_a.select(&:work?)
133
+ end
134
+
135
+ def member_of_work_ids
136
+ member_of_works.map(&:id)
137
+ end
138
+
85
139
  private
86
140
 
87
141
  # Remove this object from parent works or collections
@@ -1,4 +1,5 @@
1
1
  require 'hydra-file_characterization'
2
+ require 'nokogiri'
2
3
 
3
4
  module Hydra::Works
4
5
  class CharacterizationService
@@ -1,5 +1,5 @@
1
1
  module Hydra
2
2
  module Works
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
@@ -50,7 +50,7 @@ module Hydra::Works
50
50
  private
51
51
 
52
52
  def warning(msg)
53
- ActiveFedora::Base.logger.warn(msg) if ActiveFedora::Base.logger
53
+ ActiveFedora::Base.logger&.warn(msg)
54
54
  end
55
55
  end
56
56
  end
@@ -148,4 +148,154 @@ describe Hydra::Works::Collection do
148
148
  end
149
149
  end
150
150
  end
151
+
152
+ context 'relationships' do
153
+ context '#parent_collections and #parent_collection_ids' do
154
+ let(:parent_col1) { described_class.new(id: 'parent_col1') }
155
+ let(:parent_col2) { described_class.new(id: 'parent_col2') }
156
+ let(:collection) { described_class.new(id: 'collection') }
157
+
158
+ context 'when parents collection knows about child collections' do
159
+ before do
160
+ parent_col1.members = [collection]
161
+ parent_col2.members = [collection]
162
+ collection.save
163
+ parent_col1.save
164
+ parent_col2.save
165
+ end
166
+
167
+ it 'gets both parent collections' do
168
+ expect(collection.parent_collections).to match_array [parent_col1, parent_col2]
169
+ expect(collection.parent_collection_ids).to match_array [parent_col1.id, parent_col2.id]
170
+ end
171
+ end
172
+
173
+ context 'when child collection knows about parent collections' do
174
+ before do
175
+ collection.member_of_collections = [parent_col1, parent_col2]
176
+ end
177
+
178
+ it 'gets both parent collections' do
179
+ expect(collection.parent_collections).to match_array [parent_col1, parent_col2]
180
+ expect(collection.parent_collection_ids).to match_array [parent_col1.id, parent_col2.id]
181
+ end
182
+ end
183
+
184
+ context 'when some children know about parent and some parents know about child' do
185
+ before do
186
+ parent_col1.members = [collection]
187
+ collection.member_of_collections = [parent_col2]
188
+ collection.save
189
+ parent_col1.save
190
+ end
191
+
192
+ it 'gets both parent collections' do
193
+ expect(collection.parent_collections).to match_array [parent_col1, parent_col2]
194
+ expect(collection.parent_collection_ids).to match_array [parent_col1.id, parent_col2.id]
195
+ end
196
+ end
197
+ end
198
+
199
+ context '#child_collections and #child_collection_ids' do
200
+ let(:child_col1) { described_class.new(id: 'child_col1') }
201
+ let(:child_col2) { described_class.new(id: 'child_col2') }
202
+ let(:child_work) { Hydra::Works::Work.new(id: 'child_work') }
203
+ let(:collection) { described_class.new(id: 'collection') }
204
+
205
+ context 'when child collections knows about parent collections' do
206
+ before do
207
+ child_col1.member_of_collections = [collection]
208
+ child_col2.member_of_collections = [collection]
209
+ child_work.member_of_collections = [collection]
210
+ child_col1.save
211
+ child_col2.save
212
+ child_work.save
213
+ collection.save
214
+ end
215
+
216
+ it 'gets both child collections' do
217
+ expect(collection.child_collections).to match_array [child_col1, child_col2]
218
+ expect(collection.child_collection_ids).to match_array [child_col1.id, child_col2.id]
219
+ end
220
+ end
221
+
222
+ context 'when parent collection knows about child collections' do
223
+ before do
224
+ collection.members = [child_col1, child_col2, child_work]
225
+ end
226
+
227
+ it 'gets both child collections' do
228
+ expect(collection.child_collections).to match_array [child_col1, child_col2]
229
+ expect(collection.child_collection_ids).to match_array [child_col1.id, child_col2.id]
230
+ end
231
+ end
232
+
233
+ context 'when some children know about parent and some parents know about children' do
234
+ before do
235
+ collection.members = [child_col1]
236
+ child_col2.member_of_collections = [collection]
237
+ child_work.member_of_collections = [collection]
238
+ child_col2.save
239
+ child_work.save
240
+ collection.save
241
+ end
242
+
243
+ it 'gets both child collections' do
244
+ expect(collection.child_collections).to match_array [child_col1, child_col2]
245
+ expect(collection.child_collection_ids).to match_array [child_col1.id, child_col2.id]
246
+ end
247
+ end
248
+ end
249
+
250
+ context '#child_works and #child_work_ids' do
251
+ let(:work1) { Hydra::Works::Work.new(id: 'work1') }
252
+ let(:work2) { Hydra::Works::Work.new(id: 'work2') }
253
+ let(:child_collection) { described_class.new(id: 'child_collection') }
254
+ let(:collection) { described_class.new(id: 'collection') }
255
+
256
+ context 'when child works knows about parent collections' do
257
+ before do
258
+ work1.member_of_collections = [collection]
259
+ work2.member_of_collections = [collection]
260
+ child_collection.member_of_collections = [collection]
261
+ work1.save
262
+ work2.save
263
+ child_collection.save
264
+ collection.save
265
+ end
266
+
267
+ it 'gets both child works' do
268
+ expect(collection.child_works).to match_array [work1, work2]
269
+ expect(collection.child_work_ids).to match_array [work1.id, work2.id]
270
+ end
271
+ end
272
+
273
+ context 'when parent collection knows about child works' do
274
+ before do
275
+ collection.members = [work1, work2, child_collection]
276
+ end
277
+
278
+ it 'gets both child works' do
279
+ expect(collection.child_works).to match_array [work1, work2]
280
+ expect(collection.child_work_ids).to match_array [work1.id, work2.id]
281
+ end
282
+ end
283
+
284
+ context 'when some children know about parent and some parents know about children' do
285
+ before do
286
+ collection.members = [work1]
287
+ work2.member_of_collections = [collection]
288
+ child_collection.member_of_collections = [collection]
289
+ work2.save
290
+ child_collection.save
291
+ collection.save
292
+ end
293
+
294
+ it 'gets both child works' do
295
+ expect(collection.child_works).to match_array [work1, work2]
296
+ expect(collection.child_work_ids).to match_array [work1.id, work2.id]
297
+ end
298
+ end
299
+ end
300
+ end
151
301
  end
@@ -48,6 +48,17 @@ describe Hydra::Works::FileSet do
48
48
  it { is_expected.to eq [work] }
49
49
  end
50
50
 
51
+ describe '#in_work_ids' do
52
+ subject { file_set.in_work_ids }
53
+ let(:work) { Hydra::Works::Work.create }
54
+ before do
55
+ work.ordered_members << file_set
56
+ work.save
57
+ end
58
+
59
+ it { is_expected.to eq [work.id] }
60
+ end
61
+
51
62
  describe '#destroy' do
52
63
  let(:work) { Hydra::Works::Work.create }
53
64
  before do
@@ -86,4 +97,40 @@ describe Hydra::Works::FileSet do
86
97
  expect(file_set.collection?).to be false
87
98
  end
88
99
  end
100
+
101
+ context 'relationships' do
102
+ context '#parent_works and #parent_work_ids' do
103
+ let(:parent_work) { Hydra::Works::Work.new(id: 'parent_work') }
104
+ let(:child_file_set1) { described_class.new(id: 'child_file_set1') }
105
+ let(:child_file_set2) { described_class.new(id: 'child_file_set2') }
106
+
107
+ context 'when parent work knows about child file sets' do
108
+ before do
109
+ parent_work.members = [child_file_set1, child_file_set2]
110
+ child_file_set1.save
111
+ child_file_set2.save
112
+ parent_work.save
113
+ end
114
+
115
+ it 'gets parent work' do
116
+ expect(child_file_set1.parent_works).to match_array [parent_work]
117
+ expect(child_file_set1.parent_work_ids).to match_array [parent_work.id]
118
+ end
119
+ end
120
+
121
+ context 'when child works know about parent works' do
122
+ # before do
123
+ # # NOOP: The :member_of relationship is not defined for works. It only uses the :members relationship.
124
+ # child_file_set1.member_of = [parent_work]
125
+ # child_file_set2.member_of = [parent_work]
126
+ # end
127
+
128
+ it 'gets parent work' do
129
+ skip 'Pending implementation of the :member_of relationship for works'
130
+ expect(child_file_set1.parent_works).to match_array [parent_work]
131
+ expect(child_file_set1.parent_work_ids).to match_array [parent_work.id]
132
+ end
133
+ end
134
+ end
135
+ end
89
136
  end
@@ -3,14 +3,14 @@ require 'spec_helper'
3
3
  describe Hydra::Works::Work do
4
4
  subject { described_class.new }
5
5
 
6
- let(:work1) { described_class.new }
7
- let(:work2) { described_class.new }
8
- let(:work3) { described_class.new }
9
- let(:work4) { described_class.new }
10
- let(:work5) { described_class.new }
6
+ let(:work1) { described_class.new(id: 'wk1') }
7
+ let(:work2) { described_class.new(id: 'wk2') }
8
+ let(:work3) { described_class.new(id: 'wk3') }
9
+ let(:work4) { described_class.new(id: 'wk4') }
10
+ let(:work5) { described_class.new(id: 'wk5') }
11
11
 
12
- let(:file_set1) { Hydra::Works::FileSet.new }
13
- let(:file_set2) { Hydra::Works::FileSet.new }
12
+ let(:file_set1) { Hydra::Works::FileSet.new(id: 'fs1') }
13
+ let(:file_set2) { Hydra::Works::FileSet.new(id: 'fs2') }
14
14
 
15
15
  let(:object1) { Hydra::PCDM::Object.new }
16
16
  let(:object2) { Hydra::PCDM::Object.new }
@@ -262,9 +262,11 @@ describe Hydra::Works::Work do
262
262
  end
263
263
  it 'has a parent collection' do
264
264
  expect(work2.in_collections).to eq [collection1]
265
+ expect(work2.in_collection_ids).to eq [collection1.id]
265
266
  end
266
267
  it 'has a parent work' do
267
268
  expect(work2.in_works).to eq [work1]
269
+ expect(work2.in_work_ids).to eq [work1.id]
268
270
  end
269
271
  end
270
272
 
@@ -279,4 +281,208 @@ describe Hydra::Works::Work do
279
281
  expect(work1.member_of_collection_ids).to eq [collection1.id]
280
282
  end
281
283
  end
284
+
285
+ context 'relationships' do
286
+ context '#parent_collections and #parent_collection_ids' do
287
+ let(:col1) { Hydra::Works::Collection.new(id: 'col1') }
288
+ let(:col2) { Hydra::Works::Collection.new(id: 'col2') }
289
+
290
+ context 'when parents collection knows about child works' do
291
+ before do
292
+ col1.members = [work1]
293
+ col2.members = [work1]
294
+ work1.save
295
+ col1.save
296
+ col2.save
297
+ end
298
+
299
+ it 'get both parent collections' do
300
+ expect(work1.parent_collections).to match_array [col1, col2]
301
+ expect(work1.parent_collection_ids).to match_array [col1.id, col2.id]
302
+ end
303
+ end
304
+
305
+ context 'when child works knows about parent collections' do
306
+ before do
307
+ work1.member_of_collections = [col1, col2]
308
+ end
309
+
310
+ it 'gets both parent collections' do
311
+ expect(work1.parent_collections).to match_array [col1, col2]
312
+ expect(work1.parent_collection_ids).to match_array [col1.id, col2.id]
313
+ end
314
+ end
315
+
316
+ context 'when some children know about parent and some parents know about child' do
317
+ before do
318
+ col1.members = [work1]
319
+ work1.member_of_collections = [col2]
320
+ work1.save
321
+ col1.save
322
+ end
323
+
324
+ it 'gets both parent collections' do
325
+ expect(work1.parent_collections).to match_array [col1, col2]
326
+ expect(work1.parent_collection_ids).to match_array [col1.id, col2.id]
327
+ end
328
+ end
329
+ end
330
+
331
+ context '#parent_works and #parent_work_ids' do
332
+ let(:parent_col1) { Hydra::Works::Collection.new(id: 'col1') }
333
+ let(:parent_work1) { work1 }
334
+ let(:parent_work2) { work2 }
335
+ let(:child_work) { work3 }
336
+
337
+ context 'when parent work knows about child works' do
338
+ before do
339
+ parent_col1.members = [child_work] # collection is included to make sure parent_works only returns works
340
+ parent_work1.members = [child_work]
341
+ parent_work2.members = [child_work]
342
+ child_work.save
343
+ parent_col1.save
344
+ parent_work1.save
345
+ parent_work2.save
346
+ end
347
+
348
+ it 'gets both parent works' do
349
+ expect(child_work.parent_works).to match_array [parent_work1, parent_work2]
350
+ expect(child_work.parent_work_ids).to match_array [parent_work1.id, parent_work2.id]
351
+ end
352
+ end
353
+
354
+ context 'when child works know about parent works' do
355
+ # before do
356
+ # # NOOP: The :member_of relationship is not defined for works. It only uses the :members relationship.
357
+ # child_work.member_of = [parent_work1, parent_work2]
358
+ # end
359
+
360
+ it 'gets both parent works' do
361
+ skip 'Pending implementation of the :member_of relationship for works'
362
+ expect(child_work.parent_works).to match_array [parent_work1, parent_work2]
363
+ expect(child_work.parent_work_ids).to match_array [parent_work1.id, parent_work2.id]
364
+ end
365
+ end
366
+
367
+ context 'when some children know about parent and some parents know about child' do
368
+ # before do
369
+ # parent_work1.members = [child_work]
370
+ # child_work.save
371
+ # parent_work1.save
372
+ # # NOOP: The :member_of relationship is not defined for works. It only uses the :members relationship.
373
+ # child_work.member_of = [parent_work2]
374
+ # end
375
+
376
+ it 'gets both parent works' do
377
+ skip 'Pending implementation of the :member_of relationship for works'
378
+ expect(child_work.parent_works).to match_array [parent_work1, parent_work2]
379
+ expect(child_work.parent_work_ids).to match_array [parent_work1.id, parent_work2.id]
380
+ end
381
+ end
382
+ end
383
+
384
+ context '#child_works and #child_work_ids' do
385
+ context 'when parent works know about child works' do
386
+ let(:child_work1) { work1 }
387
+ let(:child_work2) { work2 }
388
+ let(:parent_work) { work3 }
389
+ let(:child_fileset) { file_set1 } # fileset is included to make sure child_works only returns works
390
+
391
+ before do
392
+ parent_work.members = [child_work1, child_work2, child_fileset]
393
+ parent_work.save
394
+ end
395
+
396
+ it 'gets both child works' do
397
+ expect(parent_work.child_works).to match_array [child_work1, child_work2]
398
+ expect(parent_work.child_work_ids).to match_array [child_work1.id, child_work2.id]
399
+ end
400
+ end
401
+
402
+ context 'when child knows about parent' do
403
+ before do
404
+ # before do
405
+ # # NOOP: The :member_of relationship is not defined for works. It only uses the :members relationship.
406
+ # child_work1.member_of = [parent_work]
407
+ # child_work2.member_of = [parent_work]
408
+ # end
409
+ end
410
+
411
+ it 'gets both child works' do
412
+ skip 'Pending implementation of the :member_of relationship for works'
413
+ expect(parent_work.child_works).to match_array [child_work1, child_work2]
414
+ expect(parent_work.child_work_ids).to match_array [child_work1.id, child_work2.id]
415
+ end
416
+ end
417
+
418
+ context 'when some children know about parent and some parents know about child' do
419
+ before do
420
+ # before do
421
+ # # NOOP: The :member_of relationship is not defined for works. It only uses the :members relationship.
422
+ # child_work1.member_of = [parent_work]
423
+ # parent_work.members = [child_work2, child_fileset]
424
+ # parent_work.save
425
+ # end
426
+ end
427
+
428
+ it 'gets both child works' do
429
+ skip 'Pending implementation of the :member_of relationship for works'
430
+ expect(parent_work.child_works).to match_array [child_work1, child_work2]
431
+ expect(parent_work.child_work_ids).to match_array [child_work1.id, child_work2.id]
432
+ end
433
+ end
434
+ end
435
+
436
+ context '#child_file_sets and #child_file_set_ids' do
437
+ context 'when parents knows about child' do
438
+ let(:child_fileset1) { file_set1 }
439
+ let(:child_fileset2) { file_set2 }
440
+ let(:parent_work) { work1 }
441
+ let(:child_work) { work2 } # work is included to make sure child_file_sets only returns file_sets
442
+
443
+ before do
444
+ parent_work.members = [child_fileset1, child_fileset2, child_work]
445
+ parent_work.save
446
+ end
447
+
448
+ it 'gets both child filesets' do
449
+ expect(parent_work.child_file_sets).to match_array [child_fileset1, child_fileset2]
450
+ expect(parent_work.child_file_set_ids).to match_array [child_fileset1.id, child_fileset2.id]
451
+ end
452
+ end
453
+
454
+ context 'when child knows about parent' do
455
+ before do
456
+ # before do
457
+ # # NOOP: The :member_of relationship is not defined for file sets. It only uses the :members relationship.
458
+ # child_fileset1.member_of = [parent_work]
459
+ # child_fileset2.member_of = [parent_work]
460
+ # end
461
+ end
462
+
463
+ it 'gets both child filesets' do
464
+ skip 'Pending implementation of the :member_of relationship for works'
465
+ expect(parent_work.child_file_sets).to match_array [child_fileset1, child_fileset2]
466
+ expect(parent_work.child_file_set_ids).to match_array [child_fileset1.id, child_fileset2.id]
467
+ end
468
+ end
469
+
470
+ context 'when some children know about parent and some parents know about child' do
471
+ before do
472
+ # before do
473
+ # # NOOP: The :member_of relationship is not defined for file sets. It only uses the :members relationship.
474
+ # child_fileset1.member_of = [parent_work]
475
+ # parent_work.members = [child_fileset2, child_work]
476
+ # parent_work.save
477
+ # end
478
+ end
479
+
480
+ it 'gets both child filesets' do
481
+ skip 'Pending implementation of the :member_of relationship for works'
482
+ expect(parent_work.child_file_sets).to match_array [child_fileset1, child_fileset2]
483
+ expect(parent_work.child_file_set_ids).to match_array [child_fileset1.id, child_fileset2.id]
484
+ end
485
+ end
486
+ end
487
+ end
282
488
  end