Skip to content

Commit

Permalink
limit embargo/lease date validation to create
Browse files Browse the repository at this point in the history
introduce a wrapper for `Hydra::AccessControls::Embargoable` with less agressive
validation behavior.
  • Loading branch information
tamsin johnson committed May 7, 2021
1 parent 6adbe21 commit 0a62037
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/models/concerns/hyrax/embargoable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
module Hyrax
module Embargoable
extend ActiveSupport::Concern
include Hydra::AccessControls::Embargoable

included do
validates :lease_expiration_date, 'hydra/future_date': true, on: :create
validates :embargo_release_date, 'hydra/future_date': true, on: :create
end

##
# Override aggressive Hydra::AccessControls validation
def enforce_future_date_for_embargo?
false
end

##
# Override aggressive Hydra::AccessControls validation
def enforce_future_date_for_lease?
false
end
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/work_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module WorkBehavior
include Naming
include CoreMetadata
include InAdminSet
include Hydra::AccessControls::Embargoable
include Hyrax::Embargoable
include GlobalID::Identification
include NestedWorks
include Suppressible
Expand Down
15 changes: 15 additions & 0 deletions lib/wings/active_fedora_converter/default_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class DefaultWork < ActiveFedora::Base
include Hydra::AccessControls::Embargoable
property :nested_resource, predicate: ::RDF::URI("http://example.com/nested_resource"), class_name: "Wings::ActiveFedoraConverter::NestedResource"

validates :lease_expiration_date, 'hydra/future_date': true, on: :create
validates :embargo_release_date, 'hydra/future_date': true, on: :create

class_attribute :valkyrie_class
self.valkyrie_class = Hyrax::Resource

Expand All @@ -121,6 +124,18 @@ def to_rdf_representation
alias to_s inspect
end

##
# Override aggressive Hydra::AccessControls validation
def enforce_future_date_for_embargo?
false
end

##
# Override aggressive Hydra::AccessControls validation
def enforce_future_date_for_lease?
false
end

def indexing_service
Hyrax::ValkyrieIndexer.for(resource: valkyrie_resource)
end
Expand Down
33 changes: 33 additions & 0 deletions spec/models/generic_work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@
end
end

describe "embargo" do
subject(:work) { described_class.new(title: ['a title'], embargo_release_date: embargo_release_date) }
let(:embargo_release_date) { Time.zone.today + 10 }

it { is_expected.to be_valid }

context 'with a past date' do
let(:embargo_release_date) { Time.zone.today - 10 }

it { is_expected.not_to be_valid }

it 'has errors related to the date' do
expect { work.valid? }
.to change { work.errors.to_a }
.from(be_empty)
.to include("Embargo release date Must be a future date")
end
end

context 'with a saved embargo' do
let(:past) { Time.zone.today - 10 }

before { work.save! }

it 'can update the embargo with any date' do
work.embargo_release_date = past

expect(work).to be_valid
expect{ work.save! }.not_to raise_error
end
end
end

describe "delegations" do
let(:work) { described_class.new { |gw| gw.apply_depositor_metadata("user") } }
let(:proxy_depositor) { create(:user) }
Expand Down

0 comments on commit 0a62037

Please sign in to comment.