Skip to content

Commit

Permalink
active_fedora_converter etc. changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie Allinson committed Jan 25, 2020
1 parent 0b21d22 commit 7eb881f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/wings/active_fedora_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def active_fedora_class

return klass if klass <= ActiveFedora::Base

ModelRegistry.lookup(klass) || DefaultWork
ModelRegistry.lookup(klass) || Wings::ActiveFedoraConverter::DefaultWork(klass)
end

##
Expand All @@ -84,6 +84,16 @@ def id
resource.alternate_ids.first.to_s
end

def self.DefaultWork(valkyrie_class)
"Wings::#{dynamic_active_fedora_class_name(valkyrie_class)}".constantize
rescue NameError
Wings.const_set(dynamic_active_fedora_class_name(valkyrie_class), Class.new(DefaultWork))
end

def self.dynamic_active_fedora_class_name(valkyrie_class)
valkyrie_class.name.gsub('::', '__')
end

# A dummy work class for valkyrie resources that don't have corresponding
# hyrax ActiveFedora::Base models.
#
Expand Down
12 changes: 11 additions & 1 deletion lib/wings/orm_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ def self.relationship_keys_for(reflections:)
#
# @return [Class]
def self.base_for(klass:)
ModelRegistry.reverse_lookup(klass) || Hyrax::Resource
ModelRegistry.reverse_lookup(klass) || valkyrie_resource_class(klass)
end

def self.valkyrie_resource_class(klass)
if klass.name.starts_with?('Wings::')
klass.name.gsub('Wings::', '').gsub('__', '::').constantize
else
Hyrax::Resource
end
rescue NameError
Hyrax::Resource
end

##
Expand Down
2 changes: 1 addition & 1 deletion spec/wings/active_fedora_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ConverterDummyResource < Valkyrie::Resource; end

it 'gives a default work' do
expect(converter.convert)
.to be_a Wings::ActiveFedoraConverter::DefaultWork
.to be_a Wings::ConverterDummyResource
end

context 'and it is registered' do
Expand Down
23 changes: 23 additions & 0 deletions spec/wings/model_transformer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@
end

describe '#build' do
context 'when it was saved as a generated Valkyrie Resource' do
let(:work) { Wings::ActiveFedoraConverter.new(resource: Monograph.new(id: id, **attributes)).convert }

it 'returns a Monograph' do
expect(factory.build).to be_a Monograph
end
end

context 'when it was saved as a generated and Namespaced Valkyrie Resource' do
before do
module My
class SpecialMonograph < Valkyrie::Resource
end
end
end
let(:ns_class) { My::SpecialMonograph }
let(:work) { Wings::ActiveFedoraConverter.new(resource: ns_class.new(id: id, **attributes)).convert }

it 'returns a My::SpecialMonograph' do
expect(factory.build).to be_a ns_class
end
end

it 'returns a Valkyrie::Resource' do
expect(factory.build).to be_a Valkyrie::Resource
end
Expand Down
15 changes: 14 additions & 1 deletion spec/wings/valkyrie/persister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,25 @@ class Book < ActiveFedora::Base
end
end

context 'when passing a Valkyrie::Resource generated with hyrax:work_resource' do
subject(:persister) { described_class.new(adapter: adapter) }
let(:adapter) { Wings::Valkyrie::MetadataAdapter.new }
let(:query_service) { adapter.query_service }
let(:resource_class) { Monograph }
let(:resource) { resource_class.new }

it 'returns the correct Class after save' do
expect(persister.save(resource: resource)).to be_a(resource_class)
end

end

context "When passing a Valkyrie::Resource that was never an ActiveFedora::Base" do
subject(:persister) { described_class.new(adapter: adapter) }
let(:adapter) { Wings::Valkyrie::MetadataAdapter.new }
let(:query_service) { adapter.query_service }
before do
class CustomResource < Valkyrie::Resource
class CustomResource < Hyrax::Resource
attribute :title
attribute :author
attribute :member_ids
Expand Down

0 comments on commit 7eb881f

Please sign in to comment.