Skip to content

Commit

Permalink
Create RepositoryObjects in CreateObjectService
Browse files Browse the repository at this point in the history
Fixes #4837
  • Loading branch information
jcoyne committed Apr 12, 2024
1 parent 7b4f977 commit 2a88cc5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/models/repository_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?(current_scope_prefix) || super
end

# @param [Cocina::Models::DRO, Cocina::Models::Collection, Cocina::Models::AdminPolicy] cocina_object a Cocina
# model instance, either a DRO, collection, or APO.
def create_from(cocina_object:)
args = {
external_identifier: cocina_object.externalIdentifier,
object_type: cocina_object.class.name.demodulize.underscore
}
args[:source_id] = cocina_object.identification.sourceId if cocina_object.respond_to?(:identification)
create!(**args).update_opened_version_from(cocina_object:)
end

private

def current_scope_prefix
Expand Down
2 changes: 2 additions & 0 deletions app/services/create_object_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def create(cocina_request_object, assign_doi: false)
cocina_object = cocina_from_request(updated_cocina_request_object, druid, assign_doi)
cocina_object = assign_doi(cocina_object) if assign_doi
cocina_object_with_metadata = CocinaObjectStore.store(cocina_object, skip_lock: true)
RepositoryObject.create_from(cocina_object:)

add_project_tag(druid, cocina_request_object)
# This creates version 1 (Initial Version)
ObjectVersion.initial_version(druid:)
Expand Down
2 changes: 1 addition & 1 deletion app/services/update_object_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update

cocina_object_without_metadata = Cocina::Models.without_metadata(cocina_object)

# TODO: after migration to RepositoryObject we can remove these nil checks
# TODO: after migration to RepositoryObject we can remove this nil check
RepositoryObject.find_by(external_identifier: druid)&.update_opened_version_from(cocina_object: cocina_object_without_metadata)

event_factory.create(druid:, event_type: 'update', data: { success: true, request: cocina_object_without_metadata.to_h })
Expand Down
2 changes: 1 addition & 1 deletion spec/services/create_object_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
it 'persists it' do
expect do
expect(store.create(requested_cocina_object)).to be_a Cocina::Models::DROWithMetadata
end.to change(Dro, :count).by(1)
end.to change(Dro, :count).by(1).and change(RepositoryObject, :count).by(1)

expect(Notifications::ObjectCreated).to have_received(:publish)
expect(Cocina::ObjectValidator).to have_received(:validate).with(requested_cocina_object)
Expand Down

0 comments on commit 2a88cc5

Please sign in to comment.