-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wings] Round trip registered valkyrie native classes #4012
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Wings | ||
## | ||
# This registry provides an off-ramp for legacy ActiveFedora models. | ||
# | ||
# New valkyrie models can be defined manually and registered as an analogue | ||
# of an existing `ActiveFedora::Base` implementer. This allows Wings to cast | ||
# the new model to the legacy model when saving, ensuring backwards compatible | ||
# data on save. | ||
# | ||
# Several models from Hyrax components have default mappings provided by Wings. | ||
# | ||
# @example | ||
# Wings::ModelRegistry.register(NewValkyrieModel, OldActiveFedoraModel) | ||
# | ||
# Wings::ModelRegistry.lookup(NewValkyrieModel) # => OldActiveFedoraModel | ||
# Wings::ModelRegistry.reverse_lookup(OldActiveFedoraModel) # => NewValkyrieModel | ||
# | ||
class ModelRegistry | ||
include Singleton | ||
|
||
def self.register(*args) | ||
instance.register(*args) | ||
end | ||
|
||
def self.lookup(*args) | ||
instance.lookup(*args) | ||
end | ||
|
||
def self.reverse_lookup(*args) | ||
instance.reverse_lookup(*args) | ||
end | ||
|
||
def initialize | ||
@map = {} | ||
end | ||
|
||
def register(valkyrie, active_fedora) | ||
@map[valkyrie] = active_fedora | ||
end | ||
|
||
def lookup(valkyrie) | ||
@map[valkyrie] | ||
end | ||
|
||
def reverse_lookup(active_fedora) | ||
@map.rassoc(active_fedora)&.first | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module Test | ||
class BookResource < Hyrax::Resource | ||
attribute :author, Valkyrie::Types::String | ||
attribute :created, Valkyrie::Types::Date | ||
attribute :isbn, Valkyrie::Types::String | ||
attribute :pubisher, Valkyrie::Types::String | ||
attribute :title, Valkyrie::Types::String | ||
end | ||
|
||
class Book < ActiveFedora::Base | ||
property :author, predicate: ::RDF::URI('http://example.com/ns/author') | ||
property :created, predicate: ::RDF::URI('http://example.com/ns/created') | ||
property :isbn, predicate: ::RDF::URI('http://example.com/ns/isbn') | ||
property :publisher, predicate: ::RDF::URI('http://example.com/ns/publisher') | ||
property :title, predicate: ::RDF::URI("http://example.com/ns/title") | ||
end | ||
end | ||
end | ||
|
||
Wings::ModelRegistry.register(Hyrax::Test::BookResource, Hyrax::Test::Book) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,31 +5,35 @@ | |
|
||
RSpec.describe Wings::Valkyrie::QueryService do | ||
before do | ||
class Book < ActiveFedora::Base | ||
include Hyrax::WorkBehavior | ||
include Hydra::AccessControls::Permissions | ||
property :title, predicate: ::RDF::Vocab::DC.title, multiple: true | ||
property :a_member_of, predicate: ::RDF::URI.new('http://www.example.com/a_member_of'), multiple: true do |index| | ||
index.as :symbol | ||
module Hyrax::Test | ||
module QueryService | ||
class Book < ActiveFedora::Base | ||
include Hyrax::WorkBehavior | ||
include Hydra::AccessControls::Permissions | ||
property :title, predicate: ::RDF::Vocab::DC.title, multiple: true | ||
property :a_member_of, predicate: ::RDF::URI.new('http://www.example.com/a_member_of'), multiple: true do |index| | ||
index.as :symbol | ||
end | ||
property :an_ordered_member_of, predicate: ::RDF::URI.new('http://www.example.com/an_ordered_member_of'), multiple: true | ||
end | ||
|
||
class Image < ActiveFedora::Base | ||
include Hydra::AccessControls::Permissions | ||
property :title, predicate: ::RDF::Vocab::DC.title, multiple: true | ||
end | ||
end | ||
property :an_ordered_member_of, predicate: ::RDF::URI.new('http://www.example.com/an_ordered_member_of'), multiple: true | ||
end | ||
class Image < ActiveFedora::Base | ||
include Hydra::AccessControls::Permissions | ||
property :title, predicate: ::RDF::Vocab::DC.title, multiple: true | ||
end | ||
end | ||
|
||
after do | ||
Object.send(:remove_const, :Book) | ||
Object.send(:remove_const, :Image) | ||
Hyrax::Test.send(:remove_const, :QueryService) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this! Makes for easier removal of constants defined in tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, we should probably start using Hyrax::Test everywhere test constants are needed |
||
end | ||
|
||
subject(:query_service) { described_class.new(adapter: adapter) } | ||
let(:adapter) { Wings::Valkyrie::MetadataAdapter.new } | ||
let(:persister) { Wings::Valkyrie::Persister.new(adapter: adapter) } | ||
let(:af_resource_class) { Book } | ||
let(:af_image_resource_class) { Image } | ||
let(:af_resource_class) { Hyrax::Test::QueryService::Book } | ||
let(:af_image_resource_class) { Hyrax::Test::QueryService::Image } | ||
let(:resource_class) { Wings::OrmConverter.to_valkyrie_resource_class(klass: af_resource_class) } | ||
let(:image_resource_class) { Wings::OrmConverter.to_valkyrie_resource_class(klass: af_image_resource_class) } | ||
let(:resource) { resource_class.new(title: ['Foo']) } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i chose to use
Wings::Valkyrie::MetadataAdapter.new
here to show this working explicitly with the wings adapter. i guess we could change either this or theHyrax.persister
on L25, but honestly I'm not thinking it matters. It might be good to show the ways of selecting persisters, and just get better inline documentation forHyrax.persister
in at the Hyrax level.