Skip to content

Commit f9e1fa1

Browse files
authored
Merge pull request #1058 from sul-dlss/write_virtual_object
Write virtual object memberships into the Cocina
2 parents d7293b7 + 1eb6c22 commit f9e1fa1

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Publish
4+
# Adds the constituent (virtual object) relationships to the cocina object
5+
class PublicCocinaGenerator
6+
# @param [Cocina::Models::DRO, Cocina::Models::Collection] cocina
7+
def initialize(cocina:)
8+
@cocina = cocina
9+
end
10+
11+
def self.generate(cocina:)
12+
new(cocina: cocina).generate
13+
end
14+
15+
def generate
16+
related_resources = @cocina.description.relatedResource
17+
18+
augmented_description = @cocina.description.new(relatedResource: related_resources + constituent_resources)
19+
@cocina.new(description: augmented_description)
20+
end
21+
22+
private
23+
24+
def constituent_resources
25+
VirtualObject.for(druid: @cocina.externalIdentifier).map do |virtual_object|
26+
Cocina::Models::RelatedResource.new(title: [{ value: virtual_object[:title] }],
27+
type: 'part of',
28+
purl: purl_url(virtual_object[:id]),
29+
displayLabel: 'Appears in')
30+
end
31+
end
32+
33+
def purl_url(druid)
34+
"https://#{Settings.purl.hostname}/#{druid.delete_prefix('druid:')}"
35+
end
36+
end
37+
end

app/services/versioned_files_service/update_action.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def call
2525
# For each provided content file, get the md5 from the cocina object. If the content file does not already exist for that md5, then write a new content file named by the md5.
2626
move_content_files
2727
# Write the cocina to cocina path for the version (overwriting if already exists).
28-
write_cocina(version:, cocina:)
28+
write_cocina(version:, cocina: Publish::PublicCocinaGenerator.generate(cocina:))
2929
# Write the public xml to public xml path for the version (overwriting if already exists).
3030
write_public_xml(version:, public_xml:)
3131
# Update the version manifest.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe Publish::PublicCocinaGenerator do
6+
subject(:doc) { described_class.generate(cocina:) }
7+
8+
let(:constituents) { [] }
9+
10+
let(:cocina) do
11+
Cocina::Models.build({
12+
type: "https://cocina.sul.stanford.edu/models/book",
13+
externalIdentifier: druid,
14+
label: "Test DRO",
15+
version: 1,
16+
access:,
17+
administrative: { "hasAdminPolicy" => "druid:hy787xj5878" },
18+
description:,
19+
identification:,
20+
structural:
21+
})
22+
end
23+
let(:druid) { "druid:bc123df4567" }
24+
25+
let(:access) { {} }
26+
let(:identification) { { sourceId: 'sul:123' } }
27+
let(:structural) { {} }
28+
let(:description) do
29+
{ title: [{ value: 'stuff' }], purl: 'https://purl.stanford.edu/bc123df4567' }
30+
end
31+
32+
describe '#generate' do
33+
context 'when the object is the constituent of a virtual object' do
34+
let(:constituents) { [{ id: 'druid:hj097bm8879', title: 'Test DRO' }] }
35+
36+
before do
37+
allow(VirtualObject).to receive(:for).and_return(constituents)
38+
end
39+
40+
it 'writes the relationships into cocina' do
41+
expect(doc.description.title.first.value).to eq 'stuff'
42+
expect(doc.description.purl).to eq 'https://purl.stanford.edu/bc123df4567'
43+
related_resource = doc.description.relatedResource.first
44+
expect(related_resource.title.first.value).to eq 'Test DRO'
45+
expect(related_resource.displayLabel).to eq 'Appears in'
46+
expect(related_resource.purl).to eq 'https://purl.stanford.edu/hj097bm8879'
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)