-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in new ROR search to funding body
- Loading branch information
Showing
6 changed files
with
183 additions
and
1 deletion.
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
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,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
59
app/authorities/qa/authorities/research_organization_registry.rb
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,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 |
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
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) %> |
44 changes: 44 additions & 0 deletions
44
lib/scholars_archive/controlled_vocabularies/research_organization_registry.rb
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,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 |