Skip to content

Commit

Permalink
converting a valkyrie resource lease into an active fedora lease is r…
Browse files Browse the repository at this point in the history
…idiculously tricky and I wish we did not have to.
  • Loading branch information
alishaevn committed Aug 4, 2023
1 parent f992e10 commit 96f5719
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/wings/active_fedora_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def normal_attributes

##
# apply attributes to the ActiveFedora model
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
def apply_attributes_to_model(af_object)
case af_object
when Hydra::AccessControl
Expand All @@ -141,11 +142,20 @@ def apply_attributes_to_model(af_object)
members = Array.wrap(converted_attrs.delete(:members))
files = converted_attrs.delete(:files)
af_object.attributes = converted_attrs
if resource.try(:lease) && af_object.reflections.include?(:lease)
# TODO(#6134): af_object.lease.class has the same name as resource.lease.class; however, each class has a different object_id
# so a type mismatch happens. the code below coerces the one object into the other
unless af_object.lease&.id
resource_lease_dup = af_object.reflections.fetch(:lease).klass.new(resource.lease.attributes.except(:id, :internal_resource, :created_at, :updated_at, :new_record))
af_object.lease = resource_lease_dup
end
end
members.empty? ? af_object.try(:ordered_members)&.clear : af_object.try(:ordered_members=, members)
af_object.try(:members)&.replace(members)
af_object.files.build_or_set(files) if files
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# Add attributes from resource which aren't AF properties into af_object
def add_access_control_attributes(af_object)
Expand Down
9 changes: 8 additions & 1 deletion spec/wings/active_fedora_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@
expect(work.errors.full_messages).to eq(converted_work.errors.full_messages)
end
end

context 'with an invalid FileSet object', unless: Hyrax.config.use_valkyrie? do
# valkyrie backed resources do not respond to `valid?`.
let(:expired_lease) { Hydra::AccessControls::Lease.new(lease_expiration_date: (Time.zone.today - 2).to_datetime) }
let(:file_set) { ::FileSet.new }

before do
allow(file_set).to receive(:lease).and_return(expired_lease)
end

it 'round trip converts to an FileSet object this is also invalid' do
file_set = ::FileSet.new(lease_expiration_date: 1.day.ago)
expect(file_set).not_to be_valid
converted_file_set = described_class.new(resource: file_set.valkyrie_resource).convert
expect(converted_file_set).not_to be_valid
Expand Down

0 comments on commit 96f5719

Please sign in to comment.