Skip to content

Commit

Permalink
Merge pull request #3656 from samvera/more_collection_model_methods
Browse files Browse the repository at this point in the history
define member_object_ids for valkyrie resources
  • Loading branch information
revgum authored Mar 18, 2019
2 parents c7ede6b + ebfa277 commit 36cf01c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,4 @@ This software has been developed by and is brought to you by the Samvera communi
[Samvera website](http://samvera.org/).

![Samvera Logo](https://wiki.duraspace.org/download/thumbnails/87459292/samvera-fall-font2-200w.png?version=1&modificationDate=1498550535816&api=v2)

18 changes: 11 additions & 7 deletions app/models/concerns/hyrax/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def collection_type=(new_collection_type)
end

# Add members using the members association.
# TODO: Confirm if this is ever used. I believe all relationships are done through
# add_member_objects using the member_of_collections relationship. Deprecate?
def add_members(new_member_ids)
return if new_member_ids.blank?
members << ActiveFedora::Base.find(new_member_ids)
Expand Down Expand Up @@ -80,6 +82,15 @@ def member_objects
ActiveFedora::Base.where("member_of_collection_ids_ssim:#{id}")
end

# Use this query to get the ids of the member objects (since the containment
# association has been flipped)
# Valkyrie Version: Wings::CollectionBehavior#child_collections_and_works_ids aliased to #member_object_ids
# lib/wings/models/concerns/collection_behavior.rb
def member_object_ids
return [] unless id
member_objects.map(&:id)
end

def to_s
title.present? ? title.join(' | ') : 'No Title'
end
Expand Down Expand Up @@ -113,13 +124,6 @@ def bytes
member_object_ids.collect { |work_id| size_for_work(work_id) }.sum
end

# Use this query to get the ids of the member objects (since the containment
# association has been flipped)
def member_object_ids
return [] unless id
ActiveFedora::Base.search_with_conditions("member_of_collection_ids_ssim:#{id}").map(&:id)
end

# @api public
# Retrieve the permission template for this collection.
# @return [Hyrax::PermissionTemplate]
Expand Down
7 changes: 7 additions & 0 deletions lib/wings/models/concerns/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ def child_collections_and_works(valkyrie: false)
af_collections_and_works.map(&:valkyrie_resource)
end
alias member_objects child_collections_and_works

##
# @return [Enumerable<ActiveFedora::Base> | Enumerable<Valkyrie::Resource>] an enumerable over the children of this collection
def child_collections_and_works_ids(valkyrie: false)
child_collections_and_works(valkyrie: valkyrie).map(&:id)
end
alias member_object_ids child_collections_and_works_ids
end
end
36 changes: 36 additions & 0 deletions spec/wings/models/concerns/collection_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,40 @@
end
end
end

describe '#child_collections_and_works_ids' do
let(:pcdm_object) { collection1 }
let(:parent_collection_resource) { resource }

before do
collection2.member_of_collections = [collection1]
collection3.member_of_collections = [collection1]
work1.member_of_collections = [collection1]
work2.member_of_collections = [collection1]
collection2.save!
collection3.save!
work1.save!
work2.save!
collection1.save!
end

context 'when valkyrie resources requested' do
it 'returns ids of works only as valkyrie resources through pcdm_valkyrie_behavior' do
resource_ids = parent_collection_resource.child_collections_and_works_ids(valkyrie: true)
expect(resource_ids).to match_valkyrie_ids_with_active_fedora_ids([work1.id, work2.id, collection2.id, collection3.id])
end
end
context 'when active fedora objects requested' do
it 'returns ids of works only as fedora objects through pcdm_valkyrie_behavior' do
af_object_ids = parent_collection_resource.child_collections_and_works_ids(valkyrie: false)
expect(af_object_ids.to_a).to match_array [work1.id, work2.id, collection2.id, collection3.id]
end
end
context 'when return type is not specified' do
it 'returns ids of works only as fedora objects through pcdm_valkyrie_behavior' do
af_object_ids = parent_collection_resource.child_collections_and_works_ids
expect(af_object_ids.to_a).to match_array [work1.id, work2.id, collection2.id, collection3.id]
end
end
end
end

0 comments on commit 36cf01c

Please sign in to comment.