-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into query-scopes-folder
- Loading branch information
Showing
129 changed files
with
5,728 additions
and
4,757 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/by_string.rb → app/classes/autocomplete/by_string.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
4 changes: 2 additions & 2 deletions
4
app/classes/auto_complete/by_word.rb → app/classes/autocomplete/by_word.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_clade.rb → app/classes/autocomplete/for_clade.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_herbarium.rb → app/classes/autocomplete/for_herbarium.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
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
2 changes: 1 addition & 1 deletion
2
.../auto_complete/for_location_containing.rb → ...s/autocomplete/for_location_containing.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_name.rb → app/classes/autocomplete/for_name.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_project.rb → app/classes/autocomplete/for_project.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_region.rb → app/classes/autocomplete/for_region.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
2 changes: 1 addition & 1 deletion
2
...classes/auto_complete/for_species_list.rb → app/classes/autocomplete/for_species_list.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
2 changes: 1 addition & 1 deletion
2
app/classes/auto_complete/for_user.rb → app/classes/autocomplete/for_user.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
# Lookup | ||
# | ||
# A flexible looker-upper of records. It can handle any identifiers we're likely | ||
# to throw at it: a string, ID, instance, or a mixed array of any of those. The | ||
# `lookup_method` has to be configured in the Lookup child class, because the | ||
# lookup column names are different for each model. | ||
# | ||
# Primarily used to get a clean set of ids for ActiveRecord query params. | ||
# For example, indexes like "Observations for (given) Projects" can be filtered | ||
# for more than one project at a time: "NEMF 2023" and "NEMF 2024". | ||
# The observation query needs the project IDs, and Lookup just allows callers | ||
# to send whatever param type is available. This is handy in the API and | ||
# in searches. | ||
# | ||
# Create an instance of a child class with a string, instance or id, or a mixed | ||
# array of any of these. Returns an array of ids, instances or strings (names) | ||
# via instance methods `ids`, `instances` and `titles`. | ||
# | ||
# Use: | ||
# project_ids = Lookup::Projects.new(["NEMF 2023", "NEMF 2024"]).ids | ||
# Observation.where(project: project_ids) | ||
# | ||
# fred_ids = Lookup::Users.new(["Fred", "Freddie", "Freda", "Anni Frid"]).ids | ||
# Image.where(user: fred_ids) | ||
# | ||
# Instance methods: | ||
# (all return arrays) | ||
# | ||
# ids: Array of ids of records matching the values sent to the instance | ||
# instances: Array of instances of those records | ||
# titles: Array of names of those records, via @title_column set in subclass | ||
# (A `names` method seemed too confusing, because Lookup::Names...) | ||
# | ||
# Class constants: | ||
# (defined in subclass) | ||
# | ||
# MODEL: | ||
# TITLE_COLUMN: | ||
# | ||
class Lookup | ||
attr_reader :vals, :params | ||
|
||
def initialize(vals, params = {}) | ||
unless defined?(self.class::MODEL) | ||
raise("Lookup is only usable via the subclasses, like Lookup::Names.") | ||
end | ||
|
||
@model = self.class::MODEL | ||
@title_column = self.class::TITLE_COLUMN | ||
@vals = prepare_vals(vals) | ||
@params = params | ||
end | ||
|
||
def prepare_vals(vals) | ||
return [] if vals.blank? | ||
|
||
[vals].flatten | ||
end | ||
|
||
def ids | ||
@ids ||= lookup_ids | ||
end | ||
|
||
def instances | ||
@instances ||= lookup_instances | ||
end | ||
|
||
def titles | ||
@titles ||= lookup_titles | ||
end | ||
|
||
def lookup_ids | ||
return [] if @vals.blank? | ||
|
||
evaluate_values_as_ids | ||
end | ||
|
||
# Could just look them up from the ids, but vals may already have instances | ||
def lookup_instances | ||
return [] if @vals.blank? | ||
|
||
evaluate_values_as_instances | ||
end | ||
|
||
def lookup_titles | ||
return [] if @vals.blank? | ||
|
||
instances.map(&:"#{@title_column}") | ||
end | ||
|
||
def evaluate_values_as_ids | ||
@vals.map do |val| | ||
if val.is_a?(@model) | ||
val.id | ||
elsif val.is_a?(AbstractModel) | ||
raise("Passed a #{val.class} to LookupIDs for #{@model}.") | ||
elsif /^\d+$/.match?(val.to_s) | ||
val | ||
else | ||
lookup_method(val).map(&:id) # each lookup returns an array | ||
end | ||
end.flatten.uniq.compact | ||
end | ||
|
||
def evaluate_values_as_instances | ||
@vals.map do |val| | ||
if val.is_a?(@model) | ||
val | ||
elsif val.is_a?(AbstractModel) | ||
raise("Passed a #{val.class} to LookupIDs for #{@model}.") | ||
elsif /^\d+$/.match?(val.to_s) | ||
@model.find(val.to_i) | ||
else | ||
lookup_method(val) | ||
end | ||
end.flatten.uniq.compact | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Lookup::ExternalSites < Lookup | ||
MODEL = ExternalSite | ||
TITLE_COLUMN = :name | ||
|
||
def initialize(vals, params = {}) | ||
super | ||
end | ||
|
||
def lookup_method(name) | ||
ExternalSite.where(name: name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Lookup::Herbaria < Lookup | ||
MODEL = Herbarium | ||
TITLE_COLUMN = :name | ||
|
||
def initialize(vals, params = {}) | ||
super | ||
end | ||
|
||
def lookup_method(name) | ||
Herbarium.where(name: name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Lookup::HerbariumRecords < Lookup | ||
MODEL = HerbariumRecord | ||
TITLE_COLUMN = :id | ||
|
||
def initialize(vals, params = {}) | ||
super | ||
end | ||
|
||
def lookup_method(name) | ||
HerbariumRecord.where(id: name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class Lookup::Locations < Lookup | ||
MODEL = Location | ||
TITLE_COLUMN = :name | ||
|
||
def initialize(vals, params = {}) | ||
super | ||
end | ||
|
||
def lookup_method(name) | ||
# Downcases and removes all punctuation, so it's a multi-string search | ||
# e.g. "sonoma co california usa" | ||
pattern = Location.clean_name(name.to_s).clean_pattern | ||
Location.name_contains(pattern) | ||
end | ||
end |
Oops, something went wrong.