From 0dfbdcf6b8d3d8d77e149108de7b30f68a760ee2 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Wed, 7 Oct 2020 21:20:00 -0400 Subject: [PATCH] Ensuring embargo/lease changes are persisted Prior to this commit, when a user changed the Embargo or Lease of an already persisted object, that change was not persisted. With this commit, those changes are persisted. Related to samvera/hyrax#4537 Related to samvera/hyrax#4534 --- .../hydra/access_controls/embargoable.rb | 4 +- .../spec/unit/embargoable_spec.rb | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/hydra-access-controls/app/models/concerns/hydra/access_controls/embargoable.rb b/hydra-access-controls/app/models/concerns/hydra/access_controls/embargoable.rb index c176223f3..0afe07144 100644 --- a/hydra-access-controls/app/models/concerns/hydra/access_controls/embargoable.rb +++ b/hydra-access-controls/app/models/concerns/hydra/access_controls/embargoable.rb @@ -8,8 +8,8 @@ module Embargoable validates :lease_expiration_date, :'hydra/future_date' => true, if: :enforce_future_date_for_lease? validates :embargo_release_date, :'hydra/future_date' => true, if: :enforce_future_date_for_embargo? - belongs_to :embargo, predicate: Hydra::ACL.hasEmbargo, class_name: 'Hydra::AccessControls::Embargo' - belongs_to :lease, predicate: Hydra::ACL.hasLease, class_name: 'Hydra::AccessControls::Lease' + belongs_to :embargo, predicate: Hydra::ACL.hasEmbargo, class_name: 'Hydra::AccessControls::Embargo', autosave: true + belongs_to :lease, predicate: Hydra::ACL.hasLease, class_name: 'Hydra::AccessControls::Lease', autosave: true delegate :visibility_during_embargo, :visibility_during_embargo=, :visibility_after_embargo, :visibility_after_embargo=, :embargo_release_date, :embargo_release_date=, :embargo_history, :embargo_history=, to: :existing_or_new_embargo delegate :visibility_during_lease, :visibility_during_lease=, :visibility_after_lease, :visibility_after_lease=, :lease_expiration_date, :lease_expiration_date=, :lease_history, :lease_history=, to: :existing_or_new_lease diff --git a/hydra-access-controls/spec/unit/embargoable_spec.rb b/hydra-access-controls/spec/unit/embargoable_spec.rb index eabe40067..14d44e087 100644 --- a/hydra-access-controls/spec/unit/embargoable_spec.rb +++ b/hydra-access-controls/spec/unit/embargoable_spec.rb @@ -19,6 +19,59 @@ def save(returning_value = true) let(:model) { TestModel.new } subject { model } + describe 'an object under embargo/lease' do + before do + class ModelWithPersistence < ActiveFedora::Base + include Hydra::AccessControls::Embargoable + end + end + after { Object.send(:remove_const, :ModelWithPersistence) } + let(:original_date) { 7.days.from_now } + let(:updated_date) { 14.days.from_now } + subject { ModelWithPersistence.new } + context 'saved with a new embargo release date' do + it 'will persist the new date' do + subject.visibility_during_embargo = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE + subject.visibility_after_embargo = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC + subject.embargo_release_date = original_date + subject.save + + # These next three lines are to verify the round-trip for saves. + persisted_object = subject.class.find(subject.id) + expect(persisted_object).to be_under_embargo + expect(persisted_object.embargo_release_date).to eq(original_date) + + expect do + persisted_object.embargo_release_date = updated_date + persisted_object.save + end.to change { persisted_object.class.find(persisted_object.id).embargo_release_date } + .from(original_date) + .to(updated_date) + end + end + + context 'saved with a new lease expiration date' do + it 'will persist the new date' do + subject.visibility_during_lease = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC + subject.visibility_after_lease = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE + subject.lease_expiration_date = original_date + subject.save + + # These next three lines are to verify the round-trip for saves. + persisted_object = subject.class.find(subject.id) + expect(persisted_object).to be_active_lease + expect(persisted_object.lease_expiration_date).to eq(original_date) + + expect do + persisted_object.lease_expiration_date = updated_date + persisted_object.save + end.to change { persisted_object.class.find(persisted_object.id).lease_expiration_date } + .from(original_date) + .to(updated_date) + end + end + end + describe '#embargo_indexer_class' do subject { model.embargo_indexer_class } it { is_expected.to eq Hydra::AccessControls::EmbargoIndexer }