Skip to content

Commit

Permalink
Move PublishedRelationshipsFilter from dor-services
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 24, 2019
1 parent 04d84bc commit 2215a58
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/services/public_xml_service.rb
Expand Up @@ -16,8 +16,7 @@ def to_xml
pub.add_child(public_identity_metadata.root) # add in modified identityMetadata datastream
pub.add_child(public_content_metadata.root) if public_content_metadata.xpath('//resource').any?
pub.add_child(public_rights_metadata.root)

pub.add_child(public_relationships.root) unless public_relationships.nil? # TODO: Should never be nil in practice; working around an ActiveFedora quirk for testing
pub.add_child(public_relationships.root)
pub.add_child(DublinCoreService.new(object).ng_xml.root)
pub.add_child(Dor::PublicDescMetadataService.new(object).ng_xml.root)
pub.add_child(release_xml.root) unless release_xml.xpath('//release').children.empty? # If there are no release_tags, this prevents an empty <releaseData/> from being added
Expand Down Expand Up @@ -48,7 +47,7 @@ def release_xml
end

def public_relationships
@public_relationships ||= object.public_relationships.clone
PublishedRelationshipsFilter.new(object).xml
end

def public_rights_metadata
Expand Down
47 changes: 47 additions & 0 deletions app/services/published_relationships_filter.rb
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# Show the relationships that are publically available
# removes things like hydra:isGovernedBy and fedora-model:hasModel
class PublishedRelationshipsFilter
INCLUDE_ELEMENTS = ['fedora:isMemberOf', 'fedora:isMemberOfCollection', 'fedora:isConstituentOf'].freeze
NAMESPACE = { 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' }.freeze

# @param [Dor::Abstract] object
def initialize(object)
@obj = object
end

def xml
relationships_ng_xml do |rels_doc|
statements(rels_doc).each do |rel|
next if keep?(rel.namespace.prefix, rel.name)

rel.next_sibling.remove if rel.next_sibling.content.strip.empty?
rel.remove
end
end
end

private

attr_reader :obj

def statements(rels_doc)
rels_doc.xpath('/rdf:RDF/rdf:Description/*', NAMESPACE)
end

def keep?(prefix, name)
INCLUDE_ELEMENTS.include?([prefix, name].join(':'))
end

# This creates a duplicate of RELS-EXT and yields it to the block
def relationships_ng_xml
duplicate_rels_ext.tap do |ng_xml|
yield(ng_xml)
end
end

def duplicate_rels_ext
Nokogiri::XML(obj.datastreams['RELS-EXT'].content)
end
end
53 changes: 53 additions & 0 deletions spec/services/published_relationships_filter_spec.rb
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PublishedRelationshipsFilter do
subject(:service) { described_class.new(obj) }

let(:obj) { instantiate_fixture('druid:ab123cd4567', Dor::Item) }

describe '#xml' do
subject(:doc) { service.xml }

context 'with isMemberOfCollection and isConstituentOf relationships' do
let(:relationships) do
<<~EOXML
<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<hydra:isGovernedBy rdf:resource="info:fedora/druid:789012"></hydra:isGovernedBy>
<fedora-model:hasModel rdf:resource="info:fedora/hydra:commonMetadata"></fedora-model:hasModel>
<fedora:isMemberOf rdf:resource="info:fedora/druid:xh235dd9059"></fedora:isMemberOf>
<fedora:isMemberOfCollection rdf:resource="info:fedora/druid:xh235dd9059"></fedora:isMemberOfCollection>
<fedora:isConstituentOf rdf:resource="info:fedora/druid:hj097bm8879"></fedora:isConstituentOf>
</rdf:Description>
</rdf:RDF>
EOXML
end

let(:expected) do
<<~XML
<?xml version="1.0"?>
<rdf:RDF xmlns:fedora-model="info:fedora/fedora-system:def/model#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:fedora="info:fedora/fedora-system:def/relations-external#" xmlns:hydra="http://projecthydra.org/ns/relations#">
<rdf:Description rdf:about="info:fedora/druid:ab123cd4567">
<fedora:isMemberOf rdf:resource="info:fedora/druid:xh235dd9059"/>
<fedora:isMemberOfCollection rdf:resource="info:fedora/druid:xh235dd9059"/>
<fedora:isConstituentOf rdf:resource="info:fedora/druid:hj097bm8879"/>
</rdf:Description>
</rdf:RDF>
XML
end

before do
ActiveFedora::RelsExtDatastream.from_xml(relationships, obj.rels_ext)
# Needed to generate the Datastream#content
obj.object_relations.dirty = true
obj.rels_ext.serialize!
end

it 'discards the non-allowed relations' do
expect(doc).to be_equivalent_to expected
end
end
end
end

0 comments on commit 2215a58

Please sign in to comment.