Skip to content

Commit

Permalink
Provide tooling for reversing nested collection relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar committed Aug 30, 2017
1 parent 3436320 commit a27527c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ AllCops:
Metrics/LineLength:
Enabled: false

Metrics/AbcSize:
Max: 17

Style/CollectionMethods:
PreferredMethods:
collect: 'map'
Expand Down Expand Up @@ -43,7 +46,7 @@ RSpec/NamedSubject:
RSpec/MultipleExpectations:
Exclude:
- spec/hydra/pcdm/collection_indexer_spec.rb
- spec/hydra/pcdm/object_indexer_spec.rb
- spec/hydra/pcdm/pcdm_indexer_spec.rb
- spec/hydra/pcdm/models/*

RSpec/ExampleLength:
Expand Down
18 changes: 0 additions & 18 deletions lib/hydra/pcdm/models/concerns/object_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@ module ObjectBehavior
# @return [ActiveFedora::Associations::ContainerProxy]
directly_contains :files, has_member_relation: Vocab::PCDMTerms.hasFile,
class_name: 'Hydra::PCDM::File'

##
# @macro [new] indirectly_contains
# @!method $1
# @return [ActiveFedora::Associations::ContainerProxy]
indirectly_contains :member_of_collections,
has_member_relation: Vocab::PCDMTerms.memberOf,
inserted_content_relation: RDF::Vocab::ORE.proxyFor,
class_name: 'ActiveFedora::Base',
through: 'ActiveFedora::Aggregation::Proxy',
foreign_key: :target,
type_validator: Validators::PCDMCollectionValidator
end

##
Expand Down Expand Up @@ -97,12 +85,6 @@ def in_objects
member_of.select(&:pcdm_object?).to_a
end

##
# @return [Enumerable<String>]
def member_of_collection_ids
member_of_collections.map(&:id)
end

##
# Gives directly contained files that have the requested RDF Type
#
Expand Down
18 changes: 18 additions & 0 deletions lib/hydra/pcdm/models/concerns/pcdm_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ module PcdmBehavior
inserted_content_relation: RDF::Vocab::ORE.proxyFor, class_name: 'ActiveFedora::Base',
through: 'ActiveFedora::Aggregation::Proxy', foreign_key: :target,
type_validator: Validators::PCDMObjectValidator

##
# @macro [new] indirectly_contains
# @!method $1
# @return [ActiveFedora::Associations::ContainerProxy]
indirectly_contains :member_of_collections,
has_member_relation: Vocab::PCDMTerms.memberOf,
inserted_content_relation: RDF::Vocab::ORE.proxyFor,
class_name: 'ActiveFedora::Base',
through: 'ActiveFedora::Aggregation::Proxy',
foreign_key: :target,
type_validator: Validators::PCDMCollectionValidator
end

##
Expand Down Expand Up @@ -113,6 +125,12 @@ def in_collection_ids
in_collections.map(&:id)
end

##
# @return [Enumerable<String>]
def member_of_collection_ids
member_of_collections.map(&:id)
end

##
# @param [ActiveFedora::Base] potential_ancestor the resource to check for
# ancestorship
Expand Down
5 changes: 0 additions & 5 deletions lib/hydra/pcdm/object_indexer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
module Hydra::PCDM
class ObjectIndexer < PCDMIndexer
def generate_solr_document
super.tap do |solr_doc|
solr_doc[Config.indexing_member_of_collection_ids_key] = object.member_of_collection_ids
end
end
end
end
1 change: 1 addition & 0 deletions lib/hydra/pcdm/pcdm_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def generate_solr_document
solr_doc[Config.indexing_member_ids_key] += object.member_ids
solr_doc[Config.indexing_member_ids_key].uniq!
solr_doc[Config.indexing_object_ids_key] = object.ordered_object_ids
solr_doc[Config.indexing_member_of_collection_ids_key] = object.member_of_collection_ids
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/hydra/pcdm/models/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,29 @@ class Awbject < Hydra::PCDM::Object
end
end

describe 'membership in collections' do
subject do
collection = described_class.new
collection.member_of_collections = [collection1, collection2]
collection
end

let(:collection1) { described_class.create }
let(:collection2) { described_class.create }

describe '#member_of_collections' do
it 'contains collections the object is a member of' do
expect(subject.member_of_collections).to match_array [collection1, collection2]
end
end

describe '#member_of_collection_ids' do
it 'contains the ids of collections the object is a member of' do
expect(subject.member_of_collection_ids).to match_array [collection1.id, collection2.id]
end
end
end

describe '.indexer' do
after do
Object.send(:remove_const, :Foo)
Expand Down
4 changes: 0 additions & 4 deletions spec/hydra/pcdm/object_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
let(:object) { Hydra::PCDM::Object.new }
let(:child_object1) { Hydra::PCDM::Object.new(id: '123') }
let(:child_object2) { Hydra::PCDM::Object.new(id: '456') }
let(:collection1) { Hydra::PCDM::Collection.new(id: 'abc') }
let(:collection2) { Hydra::PCDM::Collection.new(id: 'def') }
let(:indexer) { described_class.new(object) }

before do
allow(object).to receive(:ordered_object_ids).and_return([child_object1.id, child_object2.id])
allow(object).to receive(:member_of_collection_ids).and_return([collection1.id, collection2.id])
end

describe '#generate_solr_document' do
subject { indexer.generate_solr_document }

it 'has fields' do
expect(subject[Hydra::PCDM::Config.indexing_object_ids_key]).to eq %w(123 456)
expect(subject[Hydra::PCDM::Config.indexing_member_of_collection_ids_key]).to eq %w(abc def)
end
end
end
4 changes: 4 additions & 0 deletions spec/hydra/pcdm/pcdm_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
let(:collection) { Hydra::PCDM::Collection.new }
let(:member_ids) { %w(123 456 789) }
let(:indexer) { described_class.new(collection) }
let(:collection1) { Hydra::PCDM::Collection.new(id: 'abc') }
let(:collection2) { Hydra::PCDM::Collection.new(id: 'def') }

before do
allow(collection).to receive(:member_ids).and_return(member_ids)
allow(collection).to receive(:member_of_collection_ids).and_return([collection1.id, collection2.id])
end

describe '#generate_solr_document' do
subject { indexer.generate_solr_document }

it 'has fields' do
expect(subject[Hydra::PCDM::Config.indexing_member_ids_key]).to eq %w(123 456 789)
expect(subject[Hydra::PCDM::Config.indexing_member_of_collection_ids_key]).to eq %w(abc def)
end
end
end

0 comments on commit a27527c

Please sign in to comment.