-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes based_near_form_fields_behavior
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
Showing
4 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters