Skip to content

Commit

Permalink
obs of name - use lookup name
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Jan 28, 2025
1 parent ecdd13d commit 3e995d4
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions app/models/observation/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,10 @@ module Observation::Scopes # rubocop:disable Metrics/ModuleLength
# - include_all_name_proposals: boolean
# - of_look_alikes: boolean
#
scope :of_name, lambda { |name, **args|
# First, get a name record if string or id submitted
case name
when String
name = Name.find_by(text_name: name)
when Integer
name = Name.find_by(id: name)
end
return Observation.none unless name.is_a?(Name)

# Filter args may add to an array of names to collect Observations
names_array = [name]
# Maybe add synonyms (Name#synonyms includes original name)
names_array = name.synonyms if args[:include_synonyms]
# Keep names_array intact as is; maybe add more to its clone name_ids.
# (I'm thinking it's easier to pass name ids to the Observation query)
name_ids = names_array

# Add subtaxa to name_ids array. Subtaxa of synonyms too, if requested
# (don't modify the names_array we're iterating over)
if args[:include_subtaxa]
names_array.each do |n|
# |= don't add duplicates
name_ids |= Name.subtaxa_of(n)
end
end
scope :of_name, lambda { |names, **args|
# First, lookup names, plus synonyms and subtaxa if requested
lookup_args = args.slice(:include_synonyms, :include_subtaxa)
name_ids = Lookup::Names.new(names, **lookup_args)

# Query, with possible join to Naming. Mutually exclusive options:
if args[:include_all_name_proposals]
Expand Down

0 comments on commit 3e995d4

Please sign in to comment.