From ffb9b4387fd112d1373ba8d557ef866a80c344a0 Mon Sep 17 00:00:00 2001 From: Longshou Situ Date: Fri, 1 Sep 2023 10:15:54 -0700 Subject: [PATCH 1/3] Use dynamic terms for ResourceBatchEditForm. --- .../hyrax/forms/resource_batch_edit_form.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/forms/hyrax/forms/resource_batch_edit_form.rb b/app/forms/hyrax/forms/resource_batch_edit_form.rb index c466e1f80d..8465aa370c 100644 --- a/app/forms/hyrax/forms/resource_batch_edit_form.rb +++ b/app/forms/hyrax/forms/resource_batch_edit_form.rb @@ -17,17 +17,15 @@ def initialize(model, _current_ability, batch_document_ids) @names = [] @batch_document_ids = batch_document_ids if @batch_document_ids.present? - super(model.class.new(initialize_combined_fields)) + combined_fields = model_attributes(model, initialize_combined_fields) + super(model.class.new(combined_fields)) else super(model) end end def terms - [:creator, :contributor, :description, - :keyword, :resource_type, :license, :publisher, :date_created, - :subject, :language, :identifier, :based_near, - :related_url] + primary_terms + secondary_terms end attr_reader :batch_document_ids @@ -85,6 +83,16 @@ def initialize_combined_fields names << work.to_s end end + + # Model attributes for ActiveFedora compatibility + def model_attributes(model, attrs) + return attrs unless model.is_a? ActiveFedora::Base + + attrs.keys.each do |k| + attrs[k] = Array.wrap(attrs[k]).first unless model.class.properties[k.to_s]&.multiple? + end + attrs + end end end end From dc3a99a15b68f22e6cf9785056954fdb875988ff Mon Sep 17 00:00:00 2001 From: Longshou Situ Date: Fri, 1 Sep 2023 13:27:45 -0700 Subject: [PATCH 2/3] Build permitted params dynamically from the configured terms. --- .../hyrax/forms/resource_batch_edit_form.rb | 50 ++++++++----- .../forms/resource_batch_edit_form_spec.rb | 74 +++++++++---------- 2 files changed, 70 insertions(+), 54 deletions(-) diff --git a/app/forms/hyrax/forms/resource_batch_edit_form.rb b/app/forms/hyrax/forms/resource_batch_edit_form.rb index 8465aa370c..dd283b780c 100644 --- a/app/forms/hyrax/forms/resource_batch_edit_form.rb +++ b/app/forms/hyrax/forms/resource_batch_edit_form.rb @@ -25,28 +25,31 @@ def initialize(model, _current_ability, batch_document_ids) end def terms - primary_terms + secondary_terms + self.class.terms + end + + def self.terms + return Hyrax::Forms::BatchEditForm.terms if model_class < ActiveFedora::Base + + terms_primary = definitions.select { |_, definition| definition[:primary] } + .keys.map(&:to_sym) + terms_secondary = definitions.select { |_, definition| definition[:display] && !definition[:primary] } + .keys.map(&:to_sym) + + terms_primary + terms_secondary end attr_reader :batch_document_ids # Returns a list of parameters we accept from the form - # rubocop:disable Metrics/MethodLength def self.build_permitted_params - [{ creator: [] }, - { contributor: [] }, - { description: [] }, - { keyword: [] }, - { resource_type: [] }, - { license: [] }, - { publisher: [] }, - { date_created: [] }, - { subject: [] }, - { language: [] }, - { identifier: [] }, - { based_near: [] }, - { related_url: [] }, - { permissions_attributes: [:type, :name, :access, :id, :_destroy] }, + terms_permitted_params + additional_permitted_params + end + + # Returns a list of parameters other than those terms for the form + # rubocop:disable Metrics/MethodLength + def self.additional_permitted_params + [{ permissions_attributes: [:type, :name, :access, :id, :_destroy] }, :on_behalf_of, :version, :add_works_to_collection, @@ -61,6 +64,19 @@ def self.build_permitted_params end # rubocop:enable Metrics/MethodLength + # Returns a list of permitted parameters for the terms + # @param terms Array[Symbol] + # @return Array[Hash] + def self.terms_permitted_params + [].tap do |params| + terms.each do |term| + h = {} + h[term] = [] + params << h + end + end + end + # @param name [Symbol] # @return [Symbol] # @note Added for ActiveModel compatibility. @@ -78,7 +94,7 @@ def initialize_combined_fields work = Hyrax.query_service.find_by(id: doc_id) terms.each do |field| combined_attributes[field] ||= [] - combined_attributes[field] = (combined_attributes[field] + work[field].to_a).uniq + combined_attributes[field] = (combined_attributes[field] + Array.wrap(work[field])).uniq end names << work.to_s end diff --git a/spec/forms/hyrax/forms/resource_batch_edit_form_spec.rb b/spec/forms/hyrax/forms/resource_batch_edit_form_spec.rb index 6f582dea75..2fad0f7b61 100644 --- a/spec/forms/hyrax/forms/resource_batch_edit_form_spec.rb +++ b/spec/forms/hyrax/forms/resource_batch_edit_form_spec.rb @@ -43,18 +43,18 @@ it do is_expected.to include(:creator, - :contributor, - :description, - :keyword, - :resource_type, - :license, - :publisher, - :date_created, - :subject, - :language, - :identifier, - :based_near, - :related_url) + :contributor, + :description, + :keyword, + :resource_type, + :license, + :publisher, + :date_created, + :subject, + :language, + :identifier, + :based_near, + :related_url) end end @@ -79,31 +79,31 @@ subject { described_class.build_permitted_params } it do - is_expected.to eq [{ creator: [] }, - { contributor: [] }, - { description: [] }, - { keyword: [] }, - { resource_type: [] }, - { license: [] }, - { publisher: [] }, - { date_created: [] }, - { subject: [] }, - { language: [] }, - { identifier: [] }, - { based_near: [] }, - { related_url: [] }, - { permissions_attributes: [:type, :name, :access, :id, :_destroy] }, - :on_behalf_of, - :version, - :add_works_to_collection, - :visibility_during_embargo, - :embargo_release_date, - :visibility_after_embargo, - :visibility_during_lease, - :lease_expiration_date, - :visibility_after_lease, - :visibility, - { based_near_attributes: [:id, :_destroy] }] + is_expected.to include({ creator: [] }, + { contributor: [] }, + { description: [] }, + { keyword: [] }, + { resource_type: [] }, + { license: [] }, + { publisher: [] }, + { date_created: [] }, + { subject: [] }, + { language: [] }, + { identifier: [] }, + { based_near: [] }, + { related_url: [] }, + { permissions_attributes: [:type, :name, :access, :id, :_destroy] }, + :on_behalf_of, + :version, + :add_works_to_collection, + :visibility_during_embargo, + :embargo_release_date, + :visibility_after_embargo, + :visibility_during_lease, + :lease_expiration_date, + :visibility_after_lease, + :visibility, + { based_near_attributes: [:id, :_destroy] }) end end end From 332acab469aa6a4f08fa6b2f51508027573bd933 Mon Sep 17 00:00:00 2001 From: Longshou Situ Date: Tue, 5 Sep 2023 09:43:17 -0700 Subject: [PATCH 3/3] Exclude terms from valkyrie batch edit. --- app/forms/hyrax/forms/resource_batch_edit_form.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/forms/hyrax/forms/resource_batch_edit_form.rb b/app/forms/hyrax/forms/resource_batch_edit_form.rb index dd283b780c..217a7a5b83 100644 --- a/app/forms/hyrax/forms/resource_batch_edit_form.rb +++ b/app/forms/hyrax/forms/resource_batch_edit_form.rb @@ -7,6 +7,10 @@ class ResourceBatchEditForm < Hyrax::Forms::ResourceForm self.required_fields = [] self.model_class = Hyrax.primary_work_type + # Terms that need to exclude from batch edit + class_attribute :terms_excluded + self.terms_excluded = [:abstract, :label, :source] + # Contains a list of titles of all the works in the batch attr_accessor :names @@ -36,7 +40,7 @@ def self.terms terms_secondary = definitions.select { |_, definition| definition[:display] && !definition[:primary] } .keys.map(&:to_sym) - terms_primary + terms_secondary + (terms_primary + terms_secondary) - terms_excluded end attr_reader :batch_document_ids