Skip to content
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

Improve the Hyrax form API #6419

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/hyrax/admin/admin_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def form
@form ||=
case @admin_set
when Valkyrie::Resource
Hyrax::Forms::ResourceForm.for(@admin_set)
Hyrax::Forms::ResourceForm.for(resource: @admin_set)
else
form_class.new(@admin_set, current_ability, repository)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/hyrax/dashboard/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def form
@form ||=
case @collection
when Valkyrie::Resource
form = Hyrax::Forms::ResourceForm.for(@collection)
form = Hyrax::Forms::ResourceForm.for(resource: @collection)
form.prepopulate!
form
else
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/hyrax/file_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def update_metadata
end

def valkyrie_update_metadata
change_set = Hyrax::Forms::ResourceForm.for(file_set)
change_set = Hyrax::Forms::ResourceForm.for(resource: file_set)

result =
change_set.validate(attributes) &&
Expand Down Expand Up @@ -233,7 +233,7 @@ def initialize_edit_form

case file_set
when Hyrax::Resource
@form = Hyrax::Forms::ResourceForm.for(file_set)
@form = Hyrax::Forms::ResourceForm.for(resource: file_set)
@form.prepopulate!
else
@form = form_class.new(file_set)
Expand Down
24 changes: 24 additions & 0 deletions app/forms/concerns/hyrax/contained_in_works_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
module Hyrax
##
# A module of form behaviours for resources which can be contained in works.
module ContainedInWorksBehavior
no-reply marked this conversation as resolved.
Show resolved Hide resolved
##
# @api private
InWorksPrepopulator = proc do |_options|
self.in_works_ids =
if persisted?
Hyrax.query_service
.find_inverse_references_by(resource: model, property: :member_ids)
.select(&:work?)
.map(&:id)
else
[]
end
end

def self.included(descendant)
descendant.property :in_works_ids, virtual: true, prepopulator: InWorksPrepopulator
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
6 changes: 1 addition & 5 deletions app/forms/hyrax/forms/administrative_set_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Forms
##
# @api public
# @see https://github.com/samvera/valkyrie/wiki/ChangeSets-and-Dirty-Tracking
class AdministrativeSetForm < Valkyrie::ChangeSet
class AdministrativeSetForm < Hyrax::Forms::ResourceForm
##
# @api private
AdminSetMembersPopulator = lambda do |**_options|
Expand All @@ -23,10 +23,6 @@ class AdministrativeSetForm < Valkyrie::ChangeSet
property :title, required: true, primary: true
property :description, primary: true

property :human_readable_type, writable: false
property :date_modified, readable: false
property :date_uploaded, readable: false

property :creator

validates :title, presence: true
Expand Down
5 changes: 5 additions & 0 deletions app/forms/hyrax/forms/file_set_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ 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
end
Expand Down
6 changes: 1 addition & 5 deletions app/forms/hyrax/forms/pcdm_collection_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Forms
##
# @api public
# @see https://github.com/samvera/valkyrie/wiki/ChangeSets-and-Dirty-Tracking
class PcdmCollectionForm < Valkyrie::ChangeSet # rubocop:disable Metrics/ClassLength
class PcdmCollectionForm < Hyrax::Forms::ResourceForm # rubocop:disable Metrics/ClassLength
include Hyrax::FormFields(:core_metadata)

BannerInfoPrepopulator = lambda do |**_options|
Expand Down Expand Up @@ -33,10 +33,6 @@ class PcdmCollectionForm < Valkyrie::ChangeSet # rubocop:disable Metrics/ClassLe
end
end

property :human_readable_type, writable: false
property :date_modified, readable: false
property :date_uploaded, readable: false

property :depositor, required: true
property :collection_type_gid, required: true
property :visibility, default: VisibilityIntention::PRIVATE
Expand Down
5 changes: 5 additions & 0 deletions app/forms/hyrax/forms/pcdm_object_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def self.inspect
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
9 changes: 7 additions & 2 deletions app/forms/hyrax/forms/resource_batch_edit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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 All @@ -22,9 +27,9 @@ def initialize(model, _current_ability, batch_document_ids)
@batch_document_ids = batch_document_ids
if @batch_document_ids.present?
combined_fields = model_attributes(model, initialize_combined_fields)
super(model.class.new(combined_fields))
super(resource: model.class.new(combined_fields))
else
super(model)
super(resource: model)
end
end

Expand Down
Loading