diff --git a/app/helpers/hyrax/embargo_helper.rb b/app/helpers/hyrax/embargo_helper.rb index f12099fac0..c2fc5dc5ac 100644 --- a/app/helpers/hyrax/embargo_helper.rb +++ b/app/helpers/hyrax/embargo_helper.rb @@ -11,5 +11,16 @@ def assets_under_embargo def assets_with_deactivated_embargoes @assets_with_deactivated_embargoes ||= EmbargoService.assets_with_deactivated_embargoes end + + ## + # @since 3.0.0 + # + # @param [Valkyrie::Resource, ActiveFedora::Base] resource + # + # @return [Boolean] whether the resource has an embargo that is currently + # enforced (regardless of whether it has expired) + def embargo_enforced?(resource) + !!resource.lease_expiration_date + end end end diff --git a/app/helpers/hyrax/lease_helper.rb b/app/helpers/hyrax/lease_helper.rb index 99e56b65f1..7eed0ab18b 100644 --- a/app/helpers/hyrax/lease_helper.rb +++ b/app/helpers/hyrax/lease_helper.rb @@ -11,5 +11,16 @@ def assets_under_lease def assets_with_deactivated_leases @assets_with_deactivated_leases ||= LeaseService.assets_with_deactivated_leases end + + ## + # @since 3.0.0 + # + # @param [Valkyrie::Resource, ActiveFedora::Base] resource + # + # @return [Boolean] whether the resource has an lease that is currently + # enforced (regardless of whether it has expired) + def lease_enforced?(resource) + !!resource.lease_expiration_date + end end end diff --git a/app/views/hyrax/base/_form_permission.html.erb b/app/views/hyrax/base/_form_permission.html.erb index 374196fa44..05e986cab8 100644 --- a/app/views/hyrax/base/_form_permission.html.erb +++ b/app/views/hyrax/base/_form_permission.html.erb @@ -1,7 +1,7 @@ <% # This is used by works and by FileSet and the layout (col-6 vs col-12) is different for both %> -<% if f.object.embargo_release_date %> +<% if embargo_enforced?(f.object) %> <%= render 'form_permission_under_embargo', f: f %> -<% elsif f.object.lease_expiration_date %> +<% elsif lease_enforced?(f.object) %> <%= render 'form_permission_under_lease', f: f %> <% else %>
diff --git a/app/views/hyrax/base/_form_visibility_component.html.erb b/app/views/hyrax/base/_form_visibility_component.html.erb index 444be2d973..2ba9859ed1 100644 --- a/app/views/hyrax/base/_form_visibility_component.html.erb +++ b/app/views/hyrax/base/_form_visibility_component.html.erb @@ -1,6 +1,6 @@ -<% if f.object.embargo_release_date %> +<% if embargo_enforced?(f.object) %> <%= render 'form_permission_under_embargo', f: f %> -<% elsif f.object.lease_expiration_date %> +<% elsif lease_enforced?(f.object) %> <%= render 'form_permission_under_lease', f: f %> <% else %>
diff --git a/spec/helpers/hyrax/embargo_helper_spec.rb b/spec/helpers/hyrax/embargo_helper_spec.rb new file mode 100644 index 0000000000..b32ece3c38 --- /dev/null +++ b/spec/helpers/hyrax/embargo_helper_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +RSpec.describe Hyrax::EmbargoHelper do + let(:resource) { build(:monograph) } + + describe 'embargo_enforced?' do + context 'with an ActiveFedora resource' do + let(:resource) { build(:work) } + + it 'returns false' do + expect(embargo_enforced?(resource)).to be false + end + + context 'when the resource is under embargo' do + let(:resource) { build(:embargoed_work) } + + it 'returns true' do + expect(embargo_enforced?(resource)).to be true + end + + it 'and the embargo is expired returns true' do + resource.embargo.embargo_release_date = Time.zone.today - 1 + + expect(embargo_enforced?(resource)).to be true + end + + it 'and the embargo is deactivated returns false' do + resource.embargo.embargo_release_date = Time.zone.today - 1 + resource.embargo.deactivate! + + expect(embargo_enforced?(resource)).to be false + end + end + end + end +end diff --git a/spec/helpers/hyrax/lease_helper_spec.rb b/spec/helpers/hyrax/lease_helper_spec.rb new file mode 100644 index 0000000000..53fdb25bd0 --- /dev/null +++ b/spec/helpers/hyrax/lease_helper_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +RSpec.describe Hyrax::LeaseHelper do + let(:resource) { build(:monograph) } + + describe 'lease_enforced?' do + context 'with an ActiveFedora resource' do + let(:resource) { build(:work) } + + it 'returns false' do + expect(lease_enforced?(resource)).to be false + end + + context 'when the resource is under lease' do + let(:resource) { build(:leased_work) } + + it 'returns true' do + expect(lease_enforced?(resource)).to be true + end + + it 'and the lease is expired returns true' do + resource.lease.lease_expiration_date = Time.zone.today - 1 + + expect(lease_enforced?(resource)).to be true + end + + it 'and the lease is deactivated returns false' do + resource.lease.lease_expiration_date = Time.zone.today - 1 + resource.lease.deactivate! + + expect(lease_enforced?(resource)).to be false + end + end + end + end +end