Skip to content

Commit

Permalink
Includes based_near_form_fields_behavior
Browse files Browse the repository at this point in the history
When basic_metadata isn't part of the schema, the based_near
prepopulators don't get included via form_fields.rb.

This is including them explicitly to avoidbroken based_near
behaviors in the forms.
  • Loading branch information
laritakr committed Oct 17, 2024
1 parent f498700 commit ed65676
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
41 changes: 41 additions & 0 deletions app/forms/based_near_form_fields_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# A duplication of Hyrax's BasicMetadataFormFieldsBehavior which gets dynamically included via lib/hyrax/form_fields.rb
# When :basic_metadata is not part of a resource's schema, and the resource includes basec_near, this module
# must be explicitly included in the resources form.
module BasedNearFormFieldsBehavior
# Provides compatibility with the behavior of the based_near (location) controlled vocabulary form field.
# The form expects a ControlledVocabularies::Location object as input and produces a hash like those
# used with accepts_nested_attributes_for.
def self.included(descendant)
descendant.property :based_near_attributes, virtual: true, populator: :based_near_populator, prepopulator: :based_near_prepopulator
end

private

def based_near_populator(fragment:, **_options)
adds = []
deletes = []
fragment.each do |_, h|
uri = RDF::URI.parse(h["id"]).to_s
if h["_destroy"] == "true"
deletes << uri
else
adds << uri
end
end
self.based_near = ((model.based_near + adds) - deletes).uniq
end

def based_near_prepopulator
self.based_near = based_near.map do |loc|
uri = RDF::URI.parse(loc)
if uri
Hyrax::ControlledVocabularies::Location.new(uri)
else
loc
end
end
based_near << Hyrax::ControlledVocabularies::Location.new if based_near.empty?
end
end
6 changes: 5 additions & 1 deletion app/forms/etd_resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
# @see https://github.com/samvera/hyrax/wiki/Hyrax-Valkyrie-Usage-Guide#forms
# @see https://github.com/samvera/valkyrie/wiki/ChangeSets-and-Dirty-Tracking
class EtdResourceForm < Hyrax::Forms::ResourceForm(EtdResource)
# Commented out basic_metadata because these terms were added to etd_resource so we can customize it.
# Commented out basic_metadata because the terms were added to the resource's yaml
# so we can customize it
# include Hyrax::FormFields(:basic_metadata)
include Hyrax::FormFields(:bulkrax_metadata)
include Hyrax::FormFields(:etd_resource)
include Hyrax::FormFields(:with_pdf_viewer)
include Hyrax::FormFields(:with_video_embed)
include VideoEmbedBehavior::Validation
# this duplicates Hyrax::BasicMetadataFormFieldsBehavior behavior which previously
# came in dynamically via lib/hyrax/form_fields.rb
include BasedNearFormFieldsBehavior
# Define custom form fields using the Valkyrie::ChangeSet interface
#
# property :my_custom_form_field
Expand Down
6 changes: 5 additions & 1 deletion app/forms/oer_resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
# @see https://github.com/samvera/hyrax/wiki/Hyrax-Valkyrie-Usage-Guide#forms
# @see https://github.com/samvera/valkyrie/wiki/ChangeSets-and-Dirty-Tracking
class OerResourceForm < Hyrax::Forms::ResourceForm(OerResource)
# Commented out basic_metadata because these terms were added to etd_resource so we can customize it.
# Commented out basic_metadata because the terms were added to the resource's yaml
# so we can customize it
# include Hyrax::FormFields(:basic_metadata)
include Hyrax::FormFields(:bulkrax_metadata)
include Hyrax::FormFields(:oer_resource)
include Hyrax::FormFields(:with_pdf_viewer)
include Hyrax::FormFields(:with_video_embed)
include VideoEmbedBehavior::Validation
# this duplicates Hyrax::BasicMetadataFormFieldsBehavior behavior which previously
# came in dynamically via lib/hyrax/form_fields.rb
include BasedNearFormFieldsBehavior
# Define custom form fields using the Valkyrie::ChangeSet interface
#
# property :my_custom_form_field
Expand Down
2 changes: 2 additions & 0 deletions config/metadata/oer_resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ attributes:
based_near:
type: string
multiple: true
form:
primary: false
index_keys:
- "based_near_sim"
- "based_near_tesim"
Expand Down

0 comments on commit ed65676

Please sign in to comment.