Skip to content

Commit

Permalink
Merge pull request #5810 from samvera/valkyrie-embargo-controller
Browse files Browse the repository at this point in the history
get a minimal passing EmbargoController for Valkyrie
  • Loading branch information
dlpierce authored Aug 30, 2022
2 parents be83762 + e307538 commit 0b506e7
Show file tree
Hide file tree
Showing 19 changed files with 347 additions and 91 deletions.
15 changes: 11 additions & 4 deletions app/actors/hyrax/actors/embargo_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ def initialize(work)
# Update the visibility of the work to match the correct state of the embargo, then clear the embargo date, etc.
# Saves the embargo and the work
def destroy
work.embargo_visibility! # If the embargo has lapsed, update the current visibility.
work.deactivate_embargo!
work.embargo.save!
work.save!
case work
when Valkyrie::Resource
embargo_manager = Hyrax::EmbargoManager.new(resource: work)
embargo_manager.release && Hyrax::AccessControlList(work).save
embargo_manager.nullify
else
work.embargo_visibility! # If the embargo has lapsed, update the current visibility.
work.deactivate_embargo!
work.embargo.save!
work.save!
end
end
end
end
Expand Down
15 changes: 11 additions & 4 deletions app/actors/hyrax/actors/lease_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ def initialize(work)
# Update the visibility of the work to match the correct state of the lease, then clear the lease date, etc.
# Saves the lease and the work
def destroy
work.lease_visibility! # If the lease has lapsed, update the current visibility.
work.deactivate_lease!
work.lease.save!
work.save!
case work
when Valkyrie::Resource
lease_manager = Hyrax::LeaseManager.new(resource: work)
lease_manager.release && Hyrax::AccessControlList(work).save
lease_manager.nullify
else
work.lease_visibility! # If the lease has lapsed, update the current visibility.
work.deactivate_lease!
work.lease.save!
work.save!
end
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/concerns/hyrax/embargoes_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def index
# Removes a single embargo
def destroy
Hyrax::Actors::EmbargoActor.new(curation_concern).destroy
flash[:notice] = curation_concern.embargo_history.last
if curation_concern.work? && curation_concern.file_sets.present?
flash[:notice] = embargo_history(curation_concern)
if curation_concern.work? && work_has_file_set_members?(curation_concern)
redirect_to confirm_permission_path
else
redirect_to edit_embargo_path
Expand Down Expand Up @@ -60,10 +60,19 @@ def self.local_prefixes
end

def edit
@curation_concern = Hyrax::Forms::WorkEmbargoForm.new(curation_concern).prepopulate! if
Hyrax.config.use_valkyrie?
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
add_breadcrumb t(:'hyrax.embargoes.index.manage_embargoes'), hyrax.embargoes_path
add_breadcrumb t(:'hyrax.embargoes.edit.embargo_update'), '#'
end

private

def embargo_history(concern)
concern.try(:embargo_history) ||
concern.try(:embargo)&.embargo_history
end
end
end
13 changes: 11 additions & 2 deletions app/controllers/concerns/hyrax/leases_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def index
# Removes a single lease
def destroy
Hyrax::Actors::LeaseActor.new(curation_concern).destroy
flash[:notice] = curation_concern.lease_history.last
if curation_concern.work? && curation_concern.file_sets.present?
flash[:notice] = lease_history(curation_concern)&.last
if curation_concern.work? && work_has_file_set_members?(curation_concern)
redirect_to confirm_permission_path
else
redirect_to edit_lease_path
Expand Down Expand Up @@ -51,10 +51,19 @@ def self.local_prefixes
end

def edit
@curation_concern = Hyrax::Forms::WorkLeaseForm.new(curation_concern).prepopulate! if
Hyrax.config.use_valkyrie?
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
add_breadcrumb t(:'hyrax.leases.index.manage_leases'), hyrax.leases_path
add_breadcrumb t(:'hyrax.leases.edit.lease_update'), '#'
end

private

def lease_history(concern)
concern.try(:lease_history) ||
concern.try(:lease)&.lease_history
end
end
end
14 changes: 13 additions & 1 deletion app/controllers/concerns/hyrax/manages_embargoes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module ManagesEmbargoes
included do
attr_accessor :curation_concern
helper_method :curation_concern
load_and_authorize_resource class: ActiveFedora::Base, instance_name: :curation_concern, except: [:index]
base_class = Hyrax.config.use_valkyrie? ? Hyrax::Resource : ActiveFedora::Base
load_and_authorize_resource class: base_class, instance_name: :curation_concern, except: [:index]
end

# This is an override of Hyrax::ApplicationController
Expand All @@ -15,5 +16,16 @@ def deny_access(exception)
end

def edit; end

private

def work_has_file_set_members?(work)
case work
when Valkyrie::Resource
Hyrax.custom_queries.find_child_file_set_ids(resource: work).any?
else
work.file_sets.present?
end
end
end
end
35 changes: 35 additions & 0 deletions app/forms/hyrax/forms/work_embargo_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Hyrax
module Forms
##
# Represents an embargo for edit through a work. That is, this form can
# be used to wrap a Work in order to capture state changes related only to
# its embargo, ignoring the work's other fields.
#
# @note this supports the edit functionality of
# +EmbargoesControllerBehavior+.
class WorkEmbargoForm < Hyrax::ChangeSet
property :embargo, form: Hyrax::Forms::Embargo, populator: :embargo_populator, prepopulator: :embargo_populator
property :embargo_release_date, virtual: true, prepopulator: ->(_opts) { self.embargo_release_date = model.embargo&.embargo_release_date }
property :visibility_after_embargo, virtual: true, prepopulator: ->(_opts) { self.visibility_after_embargo = model.embargo&.visibility_after_embargo }
property :visibility_during_embargo, virtual: true, prepopulator: ->(_opts) { self.visibility_during_embargo = model.embargo&.visibility_during_embargo }

def embargo_populator(**)
self.embargo = Hyrax::EmbargoManager.embargo_for(resource: model)
end

##
# @return [String]
def human_readable_type
model.to_model.human_readable_type
end

##
# @return [ActiveModel::Name]
def model_name
model.to_model.model_name
end
end
end
end
35 changes: 35 additions & 0 deletions app/forms/hyrax/forms/work_lease_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Hyrax
module Forms
##
# Represents a lease for edit through a work. That is, this form can
# be used to wrap a Work in order to capture state changes related only to
# its lease, ignoring the work's other fields.
#
# @note this supports the edit functionality of
# +LeasesControllerBehavior+.
class WorkLeaseForm < Hyrax::ChangeSet
property :lease, form: Hyrax::Forms::Lease, populator: :lease_populator, prepopulator: :lease_populator
property :lease_expiration_date, virtual: true, prepopulator: ->(_opts) { self.lease_expiration_date = model.lease&.lease_expiration_date }
property :visibility_after_lease, virtual: true, prepopulator: ->(_opts) { self.visibility_after_lease = model.lease&.visibility_after_lease }
property :visibility_during_lease, virtual: true, prepopulator: ->(_opts) { self.visibility_during_lease = model.lease&.visibility_during_lease }

def lease_populator(**)
self.lease = Hyrax::LeaseManager.lease_for(resource: model)
end

##
# @return [String]
def human_readable_type
model.to_model.human_readable_type
end

##
# @return [ActiveModel::Name]
def model_name
model.to_model.model_name
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/hyrax/embargo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Embargo < Valkyrie::Resource
attribute :embargo_history, Valkyrie::Types::Array

def active?
(embargo_release_date.present? && Time.zone.today < embargo_release_date)
(embargo_release_date.present? && Hyrax::TimeService.time_in_utc < embargo_release_date)
end
end
end
2 changes: 1 addition & 1 deletion app/models/hyrax/lease.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Lease < Valkyrie::Resource
attribute :lease_history, Valkyrie::Types::Array

def active?
(lease_expiration_date.present? && Time.zone.today < lease_expiration_date)
(lease_expiration_date.present? && Hyrax::TimeService.time_in_utc < lease_expiration_date)
end
end
end
9 changes: 9 additions & 0 deletions app/services/hyrax/embargo_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ def embargo
resource.embargo || Embargo.new
end

##
# Drop the embargo by setting its release date to `nil`.
#
# @return [void]
def nullify
return unless under_embargo?
embargo.embargo_release_date = nil
end

##
# Sets the visibility of the resource to the embargo's visibility condition.
# no-op if the embargo period is current.
Expand Down
9 changes: 9 additions & 0 deletions app/services/hyrax/lease_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ def lease
resource.lease || Lease.new
end

##
# Drop the lease by setting its release date to `nil`.
#
# @return [void]
def nullify
return unless under_lease?
lease.lease_expiration_date = nil
end

##
# @return [Boolean]
def release
Expand Down
6 changes: 3 additions & 3 deletions app/views/hyrax/leases/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<fieldset class="set-access-controls">
<section class="form-text">
<p>
<% if curation_concern.lease_expiration_date %>
<% if lease_enforced?(curation_concern) %>
<%= t('.lease_true_html', cc: cc_type) %>
<% else %>
<%= t('.lease_false_html', cc: cc_type) %>
Expand All @@ -29,7 +29,7 @@

<div class="row">
<div class="col-md-12 form-actions">
<% if curation_concern.lease_expiration_date %>
<% if lease_enforced?(curation_concern) %>
<%= f.submit t('.lease_update'), class: 'btn btn-primary' %>
<%= link_to t('.lease_deactivate'), lease_path(curation_concern), method: :delete, class: 'btn btn-danger' %>
<% else %>
Expand All @@ -48,7 +48,7 @@
<h2 class="card-title"><%= t('.header.past') %></h2>
</div>
<div class="card-body">
<% if curation_concern.lease_history.empty? %>
<% if lease_history(curation_concern).empty? %>
<%= t('.history_empty', cc: cc_type) %>
<% else %>
<%= render partial: 'lease_history', object: curation_concern.lease_history %>
Expand Down
13 changes: 13 additions & 0 deletions lib/hyrax/active_fedora_dummy_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,25 @@ def to_param
@id
end

##
# @api public
def to_key
[@id]
end

##
# @api public
def model_name
@model.model_name
end

##
# @api public
# @return [String]
def human_readable_type
@model.human_readable_type
end

##
# @api public
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# @example
# let(:resource) { FactoryBot.valkyrie_create(:hyrax_work) }
class ValkyrieCreateStrategy
def initialize
@strategy = FactoryBot.strategy_by_name(:create).new
end

delegate :association, to: :@strategy

def result(evaluation)
evaluation.notify(:after_build, evaluation.object)
evaluation.notify(:before_create, evaluation.object)
Expand Down
Loading

0 comments on commit 0b506e7

Please sign in to comment.