Skip to content

Commit

Permalink
Split up Hyrax::ContainedInWorksBehavior
Browse files Browse the repository at this point in the history
In discussion, it was decided that it was better to break this out into
separate modules.
  • Loading branch information
marrus-sh committed Nov 13, 2023
1 parent 98280e5 commit f698651
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 58 deletions.
60 changes: 2 additions & 58 deletions app/forms/concerns/hyrax/contained_in_works_behavior.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
module Hyrax
##
# A module of form behaviours common to PCDM Objects and FileSets.
# A module of form behaviours for resources which can be contained in works.
module ContainedInWorksBehavior
##
# @api private
Expand All @@ -17,64 +17,8 @@ module ContainedInWorksBehavior
end
end

def self.included(descendant) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
descendant.property :depositor

descendant.property :visibility, default: VisibilityIntention::PRIVATE, populator: :visibility_populator

descendant.property :agreement_accepted, virtual: true, default: false, prepopulator: proc { |_opts| self.agreement_accepted = !model.new_record }

descendant.collection(:permissions,
virtual: true,
default: [],
form: Hyrax::Forms::Permission,
populator: :permission_populator,
prepopulator: proc { |_opts| self.permissions = Hyrax::AccessControl.for(resource: model).permissions })

descendant.property :embargo, form: Hyrax::Forms::Embargo, populator: :embargo_populator
descendant.property :lease, form: Hyrax::Forms::Lease, populator: :lease_populator

# virtual properties for embargo/lease;
descendant.property :embargo_release_date, virtual: true, prepopulator: proc { |_opts| self.embargo_release_date = model.embargo&.embargo_release_date }
descendant.property :visibility_after_embargo, virtual: true, prepopulator: proc { |_opts| self.visibility_after_embargo = model.embargo&.visibility_after_embargo }
descendant.property :visibility_during_embargo, virtual: true, prepopulator: proc { |_opts| self.visibility_during_embargo = model.embargo&.visibility_during_embargo }

descendant.property :lease_expiration_date, virtual: true, prepopulator: proc { |_opts| self.lease_expiration_date = model.lease&.lease_expiration_date }
descendant.property :visibility_after_lease, virtual: true, prepopulator: proc { |_opts| self.visibility_after_lease = model.lease&.visibility_after_lease }
descendant.property :visibility_during_lease, virtual: true, prepopulator: proc { |_opts| self.visibility_during_lease = model.lease&.visibility_during_lease }

def self.included(descendant)
descendant.property :in_works_ids, virtual: true, prepopulator: InWorksPrepopulator
end

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

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

# https://trailblazer.to/2.1/docs/reform.html#reform-populators-populator-collections
def permission_populator(collection:, index:, **)
Hyrax::Forms::Permission.new(collection[index])
end

def visibility_populator(fragment:, doc:, **)
case fragment
when "embargo"
self.visibility = doc['visibility_during_embargo']

doc['embargo'] = doc.slice('visibility_after_embargo',
'visibility_during_embargo',
'embargo_release_date')
when "lease"
self.visibility = doc['visibility_during_lease']
doc['lease'] = doc.slice('visibility_after_lease',
'visibility_during_lease',
'lease_expiration_date')
else
self.visibility = fragment
end
end
end
end
12 changes: 12 additions & 0 deletions app/forms/concerns/hyrax/deposit_agreement_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
module Hyrax
##
# A module of form behaviours for depositors and depositor agreements.
module DepositAgreementBehavior
def self.included(descendant)
descendant.property :depositor

descendant.property :agreement_accepted, virtual: true, default: false, prepopulator: proc { |_opts| self.agreement_accepted = !model.new_record }
end
end
end
49 changes: 49 additions & 0 deletions app/forms/concerns/hyrax/leaseability_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true
module Hyrax
##
# A module of form behaviours for embargoes, leases, and resulting
# visibilities.
module LeaseabilityBehavior
def self.included(descendant) # rubocop:disable Metrics/AbcSize
descendant.property :visibility, default: VisibilityIntention::PRIVATE, populator: :visibility_populator

descendant.property :embargo, form: Hyrax::Forms::Embargo, populator: :embargo_populator
descendant.property :lease, form: Hyrax::Forms::Lease, populator: :lease_populator

# virtual properties for embargo/lease;
descendant.property :embargo_release_date, virtual: true, prepopulator: proc { |_opts| self.embargo_release_date = model.embargo&.embargo_release_date }
descendant.property :visibility_after_embargo, virtual: true, prepopulator: proc { |_opts| self.visibility_after_embargo = model.embargo&.visibility_after_embargo }
descendant.property :visibility_during_embargo, virtual: true, prepopulator: proc { |_opts| self.visibility_during_embargo = model.embargo&.visibility_during_embargo }

descendant.property :lease_expiration_date, virtual: true, prepopulator: proc { |_opts| self.lease_expiration_date = model.lease&.lease_expiration_date }
descendant.property :visibility_after_lease, virtual: true, prepopulator: proc { |_opts| self.visibility_after_lease = model.lease&.visibility_after_lease }
descendant.property :visibility_during_lease, virtual: true, prepopulator: proc { |_opts| self.visibility_during_lease = model.lease&.visibility_during_lease }
end

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

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

def visibility_populator(fragment:, doc:, **)
case fragment
when "embargo"
self.visibility = doc['visibility_during_embargo']

doc['embargo'] = doc.slice('visibility_after_embargo',
'visibility_during_embargo',
'embargo_release_date')
when "lease"
self.visibility = doc['visibility_during_lease']
doc['lease'] = doc.slice('visibility_after_lease',
'visibility_during_lease',
'lease_expiration_date')
else
self.visibility = fragment
end
end
end
end
20 changes: 20 additions & 0 deletions app/forms/concerns/hyrax/permission_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Hyrax
##
# A module of form behaviours for populating permissions.
module PermissionBehavior
def self.included(descendant)
descendant.collection(:permissions,
virtual: true,
default: [],
form: Hyrax::Forms::Permission,
populator: :permission_populator,
prepopulator: proc { |_opts| self.permissions = Hyrax::AccessControl.for(resource: model).permissions })
end

# https://trailblazer.to/2.1/docs/reform.html#reform-populators-populator-collections
def permission_populator(collection:, index:, **)
Hyrax::Forms::Permission.new(collection[index])
end
end
end
3 changes: 3 additions & 0 deletions app/forms/hyrax/forms/file_set_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class FileSetForm < Hyrax::Forms::ResourceForm
# be configurable.
include Hyrax::FormFields(:file_set_metadata)

include Hyrax::DepositAgreementBehavior
include Hyrax::ContainedInWorksBehavior
include Hyrax::LeaseabilityBehavior
include Hyrax::PermissionBehavior

property :representative_id, type: Valkyrie::Types::String, writeable: false
property :thumbnail_id, type: Valkyrie::Types::String, writeable: false
Expand Down
3 changes: 3 additions & 0 deletions app/forms/hyrax/forms/pcdm_object_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class PcdmObjectForm < Hyrax::Forms::ResourceForm
include Hyrax::FormFields(:core_metadata)

include Hyrax::ContainedInWorksBehavior
include Hyrax::DepositAgreementBehavior
include Hyrax::LeaseabilityBehavior
include Hyrax::PermissionBehavior

property :on_behalf_of
property :proxy_depositor
Expand Down
4 changes: 4 additions & 0 deletions app/forms/hyrax/forms/resource_batch_edit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module Hyrax
module Forms
class ResourceBatchEditForm < Hyrax::Forms::ResourceForm
include Hyrax::FormFields(:basic_metadata)

include Hyrax::ContainedInWorksBehavior
include Hyrax::DepositAgreementBehavior
include Hyrax::LeaseabilityBehavior
include Hyrax::PermissionBehavior

self.required_fields = []
self.model_class = Hyrax.primary_work_type
Expand Down

0 comments on commit f698651

Please sign in to comment.