Skip to content

Commit

Permalink
Merge pull request #5493 from samvera/geonames_labels
Browse files Browse the repository at this point in the history
Geonames labels
  • Loading branch information
elrayle authored Mar 22, 2022
2 parents 4366714 + b9ab5a7 commit 96bfa02
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .dassie/config/initializers/hyrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
config.browse_everything = nil
end

# config.geonames_username = ''

##
# Set the system-wide virus scanner
config.virus_scanner = Hyrax::VirusScanner
Expand Down
4 changes: 1 addition & 3 deletions app/assets/javascripts/hyrax/autocomplete/linked_data.es6
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ export default class LinkedData {
// Called when Select2 is created to allow the user to initialize the
// selection based on the value of the element select2 is attached to.
// Essentially this is an id->object mapping function.

// TODO: Presently we're just showing a URI, but we should show the label.
var data = {
id: element.val(),
label: element.val()
label: element[0].dataset.label || element.val()
};
callback(data);
},
Expand Down
2 changes: 2 additions & 0 deletions app/inputs/controlled_vocabulary_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def build_options(value, index, options)

def build_options_for_new_row(_attribute_name, _index, options)
options[:value] = ''
options[:data][:label] = ''
end

def build_options_for_existing_row(_attribute_name, _index, value, options)
options[:value] = value.rdf_label.first || "Unable to fetch label for #{value.rdf_subject}"
options[:data][:label] = value.full_label || value.rdf_label
options[:readonly] = true
end

Expand Down
33 changes: 33 additions & 0 deletions app/services/hyrax/location_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true
module Hyrax
class LocationService < ::Qa::Authorities::Geonames
CACHE_KEY_PREFIX = 'hyrax_geonames_label-v1-'
CACHE_EXPIRATION = 1.week

def full_label(uri)
return if uri.blank?
id = extract_id uri
Rails.cache.fetch(cache_key(id), expires_in: CACHE_EXPIRATION) do
label.call(find(id))
end
end

private

def extract_id(obj)
uri = case obj
when String
URI(obj)
when URI
obj
else
raise ArgumentError, "#{obj} is not a valid type"
end
uri.path.split('/').last
end

def cache_key(id)
"#{CACHE_KEY_PREFIX}#{id}"
end
end
end
9 changes: 7 additions & 2 deletions lib/hyrax/controlled_vocabularies/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ class Location < ActiveTriples::Resource

# Return a tuple of url & label
def solrize
return [rdf_subject.to_s] if rdf_label.first.to_s.blank? || rdf_label.first.to_s == rdf_subject.to_s
[rdf_subject.to_s, { label: "#{rdf_label.first}$#{rdf_subject}" }]
label = full_label || rdf_label.first.to_s
return [rdf_subject.to_s] if label.blank? || label == rdf_subject.to_s
[rdf_subject.to_s, { label: "#{label}$#{rdf_subject}" }]
end

def full_label
Hyrax::LocationService.new.full_label(rdf_subject.to_s)
end
end
end
Expand Down
Loading

0 comments on commit 96bfa02

Please sign in to comment.