Skip to content

Commit

Permalink
Added in new ROR search to funding body
Browse files Browse the repository at this point in the history
  • Loading branch information
lamtu1 committed Nov 12, 2024
1 parent 359e6c0 commit 30a1ab1
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rubocop_fix_me.yml
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ Style/MethodDefParentheses:
Lint/UriEscapeUnescape:
Exclude:
- "lib/qa/authorities/geonames.rb"
- "app/authorities/qa/authorities/research_organization_registry.rb"

Capybara/VisibilityMatcher:
Exclude:
Expand Down
64 changes: 64 additions & 0 deletions app/assets/javascripts/hyrax/autocomplete.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Default from './autocomplete/default'
import Resource from './autocomplete/resource'
import LinkedData from './autocomplete/linked_data'

// OVERRIDE: Add in new field to the autocomplete form
export default class Autocomplete {
/**
* Setup for the autocomplete field.
* @param {jQuery} element - The input field to add autocompete to
* @param {string} fieldName - The name of the field (e.g. 'based_near')
* @param {string} url - The url for the autocompete search endpoint
*/
setup (element, fieldName, url) {
if(element.data('autocomplete-type') && element.data('autocomplete-type').length > 0) {
this.byDataAttribute(element, url)
} else {
this.byFieldName(element, fieldName, url)
}
}

byDataAttribute(element, url) {
let type = element.data('autocomplete-type')
let exlude = element.data('exclude-work')
if(type === 'resource' && exclude.length > 0) {
new Resource(
element,
url,
{ excluding: exclude }
)
} else if(type === 'resource' ) {
new Resource(
element,
url)
} else if(type === 'linked') {
new LinkedData(element, url)
} else {
new Default(element, url)
}
}

byFieldName(element, fieldName, url) {
switch (fieldName) {
case 'work':
new Resource(
element,
url,
{ excluding: element.data('exclude-work') }
)
break
case 'collection':
new Resource(
element,
url)
break
// ADD: Add case for 'funding_body' on autocomplete
case 'funding_body':
case 'based_near':
new LinkedData(element, url)
default:
new Default(element, url)
break
}
}
}
59 changes: 59 additions & 0 deletions app/authorities/qa/authorities/research_organization_registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module Qa::Authorities
# CLASS: Research Organization Registry
class ResearchOrganizationRegistry < Qa::Authorities::Base
include WebServiceBase

# ATTRIBUTE: Add a :label class attribute
class_attribute :label

# SELF METHOD: Format the label to present on typeahead
self.label = lambda do |item|
[item['names'].map { |v| v['value'] if v['types'].include?('ror_display') }, "(#{item['id']})"].compact.join(', ')
end

# METHOD: Create a search function based on user typing
def search(query)
# CONDITION: Check if user search by 'id' or 'word'
if query[0] == '0' && query.length == 9
parse_authority_response_with_id(json(build_id_url(query)))
else
parse_authority_response(json(build_query_url(query)))
end
end

# METHOD: To double check the 'q' string for special char
def untaint(query)
query.gsub(/[^\w\s-]/, '')
end

# METHOD: Build search using 'word'
def build_query_url(query)
query_str = URI.escape(untaint(query))
"https://api.ror.org/v2/organizations?query=#{query_str}"
end

# METHOD: Create a search using id number instead
def build_id_url(query)
query_str = URI.escape(untaint(query))
"https://api.ror.org/v2/organizations/#{query_str}"
end

private

# PARSE: Reformats the data received from the service
def parse_authority_response(response)
response['items'].map do |result|
{ 'id' => result['id'].to_s,
'label' => label.call(result) }
end
end

# REFORMAT: Format data with the id given
def parse_authority_response_with_id(response)
[{ 'id' => response['id'].to_s,
'label' => label.call(response) }]
end
end
end
3 changes: 2 additions & 1 deletion app/models/concerns/scholars_archive/default_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def set_default_visibility
index.as :stored_searchable, :facetable
end

property :funding_body, predicate: ::RDF::Vocab::MARCRelators.fnd do |index|
# UPDATE: Add in the class name for 'controlled_vocab' to the funding_body
property :funding_body, predicate: ::RDF::Vocab::MARCRelators.fnd, class_name: ScholarsArchive::ControlledVocabularies::ResearchOrganizationRegistry do |index|
index.as :stored_searchable, :facetable
end

Expand Down
13 changes: 13 additions & 0 deletions app/views/records/edit_fields/_funding_body.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%# OVERRIDE: Override the edit field for 'funding_body' to have autocomplete field %>
<%= f.input key,
as: :controlled_vocabulary,
placeholder: 'Search for a funding body',
input_html: {
class: 'form-control',
data: { 'autocomplete-url' => "/authorities/search/research_organization_registry",
'autocomplete' => key }
},
### Required for the ControlledVocabulary javascript:
wrapper_html: { data: { 'autocomplete-url' => "/authorities/search/research_organization_registry",
'field-name' => key }},
required: f.object.required?(key) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module ScholarsArchive
module ControlledVocabularies
# CLASS: Research Organization Registry (Controlled Vocab)
class ResearchOrganizationRegistry < ActiveTriples::Resource
include Hyrax::ControlledVocabularies::ResourceLabelCaching

# METHOD: Setup custom rdf_label
def rdf_label
labels = Array.wrap(self.class.rdf_label)
labels += default_labels
# OVERRIDE: From rdf_triples to select only and all english labels
values = []
labels.each do |label|
values += get_values(label).to_a
end
eng_values = values.select { |val| val.language.in? %i[en en-us] if val.is_a?(RDF::Literal) }

# We want English first
return eng_values unless eng_values.blank?

# But we'll take non-english if that's all there is
return values unless values.blank?

node? ? [] : [rdf_subject.to_s]
end

# METHOD: To solrize and return a tuple of url & label
def solrize
return [rdf_subject.to_s] if rdf_label.first.to_s.blank? || rdf_label_uri_same?

[rdf_subject.to_s, { label: "#{rdf_label}$#{rdf_subject}" }]
end

private

# METHOD: Double check if rdf_label & subject are the same
def rdf_label_uri_same?
rdf_label.first.to_s == rdf_subject.to_s
end
end
end
end

0 comments on commit 30a1ab1

Please sign in to comment.