Skip to content

Commit

Permalink
Merge pull request #4834 from sul-dlss/repo-object-find
Browse files Browse the repository at this point in the history
Find objects persisted as RepositoryObjects
  • Loading branch information
justinlittman authored Apr 11, 2024
2 parents 9871e17 + ff9f0f8 commit 2fcb971
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
17 changes: 8 additions & 9 deletions app/services/cocina_object_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,19 @@ def self.version(druid)
new.version(druid)
end

# @param [String] druid to find
# @return [Cocina::Models::DROWithMetadata,Cocina::Models::CollectionWithMetadata,Cocina::Models::AdminPolicyWithMetadata]
def find(druid)
ar_to_cocina_find(druid)
# TODO: After migration, remove the nil-checks
cocina = RepositoryObject.find_by(external_identifier: druid)&.head_version&.to_cocina_with_metadata

cocina || ar_find(druid).to_cocina_with_metadata
end

def find_by_source_id(source_id)
ar_cocina_object = Dro.find_by_source_id(source_id) ||
# TODO: Nil check can be removed after migrating to RepositoryObject
ar_cocina_object = RepositoryObject.find_by(source_id:)&.head_version ||
Dro.find_by_source_id(source_id) ||
Collection.find_by_source_id(source_id)

raise CocinaObjectNotFoundError unless ar_cocina_object
Expand Down Expand Up @@ -205,13 +211,6 @@ def ar_check_lock(cocina_object)
raise StaleLockError, "Expected lock of #{ar_object.external_lock} but received #{cocina_object.lock}."
end

# Find a Cocina object persisted by ActiveRecord.
# @param [String] druid to find
# @return [Cocina::Models::DROWithMetadata,Cocina::Models::CollectionWithMetadata,Cocina::Models::AdminPolicyWithMetadata]
def ar_to_cocina_find(druid)
ar_find(druid).to_cocina_with_metadata
end

def bootstrap_ur_admin_policy(druid)
return unless Settings.enabled_features.create_ur_admin_policy
return unless druid == Settings.ur_admin_policy.druid
Expand Down
26 changes: 26 additions & 0 deletions spec/services/cocina_object_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
expect(store.find(ar_cocina_object.external_identifier)).to be_instance_of(Cocina::Models::CollectionWithMetadata)
end
end

context 'when object is a RepositoryObject' do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(build(:dro, id: repo_object.external_identifier)) }
let(:repo_object) { create(:repository_object) }

before do
repo_object.head_version.update!(version_attributes)
end

it 'returns Cocina::Models::DRO' do
expect(store.find(repo_object.external_identifier)).to be_instance_of(Cocina::Models::DROWithMetadata)
end
end
end

describe '#version' do
Expand Down Expand Up @@ -133,6 +146,19 @@
expect(store.find_by_source_id(ar_cocina_object.identification['sourceId'])).to be_instance_of(Cocina::Models::CollectionWithMetadata)
end
end

context 'when object is a RepositoryObject' do
let(:version_attributes) { RepositoryObjectVersion.to_model_hash(build(:dro, id: repo_object.external_identifier)) }
let(:repo_object) { create(:repository_object) }

before do
repo_object.head_version.update!(version_attributes)
end

it 'returns Cocina::Models::DRO' do
expect(store.find_by_source_id(repo_object.head_version.identification['sourceId'])).to be_instance_of(Cocina::Models::DROWithMetadata)
end
end
end

describe '#exists?' do
Expand Down

0 comments on commit 2fcb971

Please sign in to comment.