Skip to content

Commit

Permalink
Build permitted params dynamically from the terms.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsitu committed Sep 3, 2023
1 parent 4cdfc2a commit b437faa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 53 deletions.
48 changes: 32 additions & 16 deletions app/forms/hyrax/forms/resource_batch_edit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@ def initialize(model, _current_ability, batch_document_ids)
end

def terms
self.class.terms
end

def self.terms
return Hyrax::Forms::BatchEditForm.terms if self.model_class < ActiveFedora::Base

primary_terms = definitions.select { |_, definition| definition[:primary] }
.keys.map(&:to_sym)
secondary_terms = definitions.select { |_, definition| definition[:display] && !definition[:primary] }
.keys.map(&:to_sym)

primary_terms + secondary_terms
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,
Expand All @@ -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.
Expand All @@ -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
Expand Down
74 changes: 37 additions & 37 deletions spec/forms/hyrax/forms/resource_batch_edit_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit b437faa

Please sign in to comment.