-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6126 deactivated leases #6127
6126 deactivated leases #6127
Changes from all commits
6cc1544
7b19cca
b8789d1
42d504b
ed8f13a
d26467d
16cdbfa
8061f33
bf1b1eb
d413323
fac77c3
0362456
c886e64
0c86b0f
1f979dc
4b5b054
6314351
33ebb3d
9975ac8
3154b19
6a59d45
5a8398f
9b64c54
530750c
cfc5d91
cef578a
10215f0
6b76967
753d216
82bb390
cee247a
5fdc19f
ff52b95
d315ec2
8ea729a
b3bd3cc
ad4f247
5c4486d
ef3f8e2
6c071cb
3c503bf
45f586b
77f7ac0
720b140
796a1f6
9896d7a
04c9c2e
9d54048
a1ea69c
b2a9f23
3e28096
8970a44
580d817
04fac65
b16bace
f992e10
96f5719
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,22 @@ module Hyrax | |
## | ||
# Provides utilities for managing the lifecycle of an `Hyrax::Lease` on a | ||
# `Hyrax::Resource`. | ||
class LeaseManager | ||
# | ||
# The lease terminology used here is as follows: | ||
# | ||
# - "Expiration Date" is the day a lease is scheduled to expire. | ||
# - "Under Lease" means the lease is "active"; i.e. that its expiration | ||
# date is today or later. | ||
# - "Applied" means the lease's pre-expiration visibility has been set on | ||
# the resource. | ||
# - "Released" means the lease's post-expiration visibility has been set on | ||
# the resource. | ||
# - "Enforced" means the object's visibility matches the pre-expiration | ||
# visibility of the lease; i.e. the lease has been applied, | ||
# but not released. | ||
# - "Deactivate" means that the existing lease will be removed | ||
# | ||
class LeaseManager # rubocop:disable Metrics/ClassLength | ||
## | ||
# @!attribute [rw] resource | ||
# @return [Hyrax::Resource] | ||
|
@@ -47,6 +62,51 @@ def release_lease_for!(resource:, query_service: Hyrax.query_service) | |
new(resource: resource, query_service: query_service) | ||
.release! | ||
end | ||
|
||
# Creates or updates an existing lease on a member to match the lease on the parent work | ||
# @param [Array<Valkyrie::Resource>] members | ||
# @param [Hyrax::Work] work | ||
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength | ||
def create_or_update_lease_on_members(members, work) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. putting this method in the LeaseManger feels like a better place for it to be than in "transactions/save.rb". it also makes it easier to use when #6131 gets worked on. |
||
# TODO: account for all members and levels, not just file sets. ref: #6131 | ||
|
||
members.each do |member| | ||
member_lease_needs_updating = work.lease.updated_at > member.lease&.updated_at if member.lease | ||
|
||
if member.lease && member_lease_needs_updating | ||
member.lease.lease_expiration_date = work.lease['lease_expiration_date'] | ||
member.lease.visibility_during_lease = work.lease['visibility_during_lease'] | ||
member.lease.visibility_after_lease = work.lease['visibility_after_lease'] | ||
member.lease = Hyrax.persister.save(resource: member.lease) | ||
else | ||
work_lease_manager = Hyrax::LeaseManager.new(resource: work) | ||
work_lease_manager.copy_lease_to(target: member) | ||
member = Hyrax.persister.save(resource: member) | ||
end | ||
|
||
user ||= ::User.find_by_user_key(member.depositor) | ||
# the line below works in that it indexes the file set with the necessary lease properties | ||
# I do not know however if this is the best event_id to pass | ||
Hyrax.publisher.publish('object.metadata.updated', object: member, user: user) | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength | ||
end | ||
|
||
# Deactivates the lease and logs a message to the lease_history property | ||
def deactivate! | ||
lease_state = lease.active? ? 'active' : 'expired' | ||
lease_record = lease_history_message( | ||
lease_state, | ||
Time.zone.today, | ||
lease.lease_expiration_date, | ||
lease.visibility_during_lease, | ||
lease.visibility_after_lease | ||
) | ||
|
||
release(force: true) | ||
nullify(force: true) | ||
lease.lease_history += [lease_record] | ||
end | ||
|
||
## | ||
|
@@ -58,7 +118,7 @@ def release_lease_for!(resource:, query_service: Hyrax.query_service) | |
def copy_lease_to(target:) | ||
return false unless under_lease? | ||
|
||
target.lease = Lease.new(clone_attributes) | ||
target.lease = Hyrax.persister.save(resource: Lease.new(clone_attributes)) | ||
self.class.apply_lease_for(resource: target) | ||
end | ||
|
||
|
@@ -80,7 +140,8 @@ def apply! | |
## | ||
# @return [Boolean] | ||
def enforced? | ||
lease.visibility_during_lease.to_s == resource.visibility | ||
lease.lease_expiration_date.present? && | ||
lease.visibility_during_lease.to_s == resource.visibility | ||
end | ||
|
||
## | ||
|
@@ -90,18 +151,27 @@ def lease | |
end | ||
|
||
## | ||
# Drop the lease by setting its release date to `nil`. | ||
# Drop the lease by setting its release date and visibility settings to `nil`. | ||
# | ||
# @param force [boolean] force the nullify even when the lease period is current | ||
# @return [void] | ||
def nullify | ||
return unless under_lease? | ||
def nullify(force: false) | ||
return false if !force && under_lease? | ||
|
||
lease.lease_expiration_date = nil | ||
lease.visibility_during_lease = nil | ||
lease.visibility_after_lease = nil | ||
end | ||
|
||
## | ||
# Sets the visibility of the resource to the lease's after lease visibility. | ||
# no-op if the lease period is current and the force flag is false. | ||
# | ||
# @param force [boolean] force the release even when the lease period is current | ||
# | ||
# @return [Boolean] | ||
def release | ||
return false if under_lease? | ||
def release(force: false) | ||
return false if !force && under_lease? | ||
return true if lease.visibility_after_lease.nil? | ||
|
||
resource.visibility = lease.visibility_after_lease | ||
|
@@ -131,5 +201,15 @@ def clone_attributes | |
def core_attribute_keys | ||
[:visibility_after_lease, :visibility_during_lease, :lease_expiration_date] | ||
end | ||
|
||
# Create the log message used when deactivating a lease | ||
def lease_history_message(state, deactivate_date, expiration_date, visibility_during, visibility_after) | ||
I18n.t 'hydra.lease.history_message', | ||
state: state, | ||
deactivate_date: deactivate_date, | ||
expiration_date: expiration_date, | ||
visibility_during: visibility_during, | ||
visibility_after: visibility_after | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<div class="form-inline"> | ||
<%= visibility_badge(Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_LEASE) %> | ||
<%= f.input :visibility_during_lease, wrapper: :inline, collection: visibility_options(:loosen), include_blank: false %> | ||
<%= f.input :lease_expiration_date, wrapper: :inline, input_html: { value: f.object.lease_expiration_date || Date.tomorrow, class: 'datepicker' } %> | ||
<%= f.input :lease_expiration_date, wrapper: :inline, input_html: { value: f.object.lease_expiration_date&.to_date || Date.tomorrow, class: 'datepicker' } %> | ||
<%= f.input :visibility_after_lease, wrapper: :inline, collection: visibility_options(:restrict), include_blank: false %> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,14 @@ def initialize(handler: Hyrax::WorkUploadsHandler) | |
# @return [Dry::Monads::Result] | ||
def call(obj, uploaded_files: [], file_set_params: []) | ||
if @handler.new(work: obj).add(files: uploaded_files, file_set_params: file_set_params).attach | ||
if obj.lease | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a new file set gets added to the work in this |
||
file_sets = obj.member_ids.map do |member| | ||
Hyrax.query_service.find_by(id: member) if Hyrax.query_service.find_by(id: member).is_a? Hyrax::FileSet | ||
end | ||
|
||
Hyrax::LeaseManager.create_or_update_lease_on_members(file_sets, obj) | ||
end | ||
|
||
Success(obj) | ||
else | ||
Failure[:failed_to_attach_file_sets, uploaded_files] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted this error handling so that
generic_works_controller_json_spec.rb:58
passes. this is also how the #create method in this same file handles the params