Skip to content

Commit

Permalink
obs scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Jan 28, 2025
1 parent 2510d12 commit ecdd13d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 65 deletions.
40 changes: 18 additions & 22 deletions app/classes/query/params/observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,42 @@ def observations_per_se_parameter_declarations
updated_at?: [:time],

ids?: [Observation],
herbarium_records?: [:string],
project_lists?: [:string],
users?: [User],
by_user?: User,
by_editor?: User, # for coercions from name/location
users?: [User],
field_slips?: [:string],
pattern?: :string,
regexp?: :string, # for coercions from location
herbarium_records?: [:string],
project_lists?: [:string],
needs_naming?: :boolean,
in_clade?: :string,
in_region?: :string
in_region?: :string,
pattern?: :string,
regexp?: :string # for coercions from location
}
end

# for observations, or coercions to observations.
def observations_parameter_declarations
{
with_name?: :boolean,
confidence?: [:float],
location?: Location,
locations?: [:string],
user_where?: :string,
is_collection_location?: :boolean,
with_public_lat_lng?: :boolean,
by_user?: User,
with_notes?: :boolean,
notes_has?: :string,
with_notes_fields?: [:string],
with_comments?: { boolean: [true] },
comments_has?: :string,
with_sequences?: { boolean: [true] },
herbaria?: [:string],
user_where?: :string,
by_user?: User,
location?: Location,
locations?: [:string],
project?: Project,
projects?: [:string],
species_list?: SpeciesList,
species_lists?: [:string],

# boolean
with_comments?: { boolean: [true] },
with_public_lat_lng?: :boolean,
with_name?: :boolean,
with_notes?: :boolean,
with_sequences?: { boolean: [true] },
is_collection_location?: :boolean,

# numeric
confidence?: [:float]
species_lists?: [:string]
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
# in_herbarium(herbarium)
# herbarium_record_notes_contain(notes)
# on_species_list(species_list)
# on_species_list_of_project(project)
# on_project_species_lists(project)
#
# == Instance methods
#
Expand Down
29 changes: 16 additions & 13 deletions app/models/observation/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module Observation::Scopes # rubocop:disable Metrics/ModuleLength
-> { where(name_id: Name.with_rank_above_genus) }
scope :without_confident_name,
-> { where(vote_cache: ..0) }
# confidence between min & max, in percentages
scope :confidence, lambda { |min, max = min|
where(vote_cache: (min.to_f / (100 / 3))..(max.to_f / (100 / 3)))
}
# Use this definition when running script to populate the column:
# scope :needs_naming, lambda {
# with_name_above_genus.or(without_confident_name)
Expand Down Expand Up @@ -395,29 +399,28 @@ def self.name_search_name_observation_ids(phrase)
# much faster than `missing(:sequences)` which uses left outer join.
scope :without_sequences,
-> { where.not(id: with_sequences) }
# confidence between min & max, in percentages
scope :confidence, lambda { |min, max = min|
where(vote_cache: (min.to_f / (100 / 3))..(max.to_f / (100 / 3)))
}
scope :for_project, lambda { |project|
scope :for_project, lambda { |projects|
project_ids = Lookup::Projects.new(projects).ids
joins(:project_observations).
where(ProjectObservation[:project_id].eq(project.id)).distinct
where(project_observations: { project: project_ids }).distinct
}
scope :in_herbarium, lambda { |herbarium|
scope :in_herbarium, lambda { |herbaria|
herbaria_ids = Lookup::Herbaria.new(herbaria).ids
joins(:herbarium_records).
where(HerbariumRecord[:herbarium_id].eq(herbarium.id)).distinct
where(herbarium_records: { herbarium: herbaria_ids }).distinct
}
scope :herbarium_record_notes_contain, lambda { |phrase|
joins(:herbarium_records).search_columns(HerbariumRecord[:notes], phrase)
}
scope :on_species_list, lambda { |species_list|
scope :on_species_list, lambda { |species_lists|
spl_ids = Lookup::SpeciesLists.new(species_lists).ids
joins(:species_list_observations).
where(SpeciesListObservation[:species_list_id].eq(species_list.id)).
distinct
where(species_list_observation: { species_list: spl_ids }).distinct
}
scope :on_species_list_of_project, lambda { |project|
scope :project_species_lists, lambda { |projects|
project_ids = Lookup::Projects.new(projects).ids
joins(species_lists: :project_species_lists).
where(ProjectSpeciesList[:project_id].eq(project.id)).distinct
where(project_species_lists: { project: project_ids }).distinct
}

scope :show_includes, lambda {
Expand Down
2 changes: 1 addition & 1 deletion test/classes/pattern_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def test_observation_search_project
end

def test_observation_search_project_lists
expect = Observation.on_species_list_of_project(projects(:bolete_project))
expect = Observation.on_project_species_lists(projects(:bolete_project))
assert(expect.count.positive?)
x = PatternSearch::Observation.new('project_lists:"Bolete Project"')
assert_obj_arrays_equal(expect, x.query.results, :sort)
Expand Down
67 changes: 39 additions & 28 deletions test/classes/query/observations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,36 @@ def test_observation_all
assert_query(expects, :Observation)
end

def test_observation_in_project_list
project = projects(:bolete_project)
# expects = project.species_lists.map(&:observations).flatten.to_a
expects = Observation.index_order.
joins(species_lists: :project_species_lists).
where(project_species_lists: { project: project }).distinct
assert_query(expects, :Observation, project_lists: project.title)
end

def test_observation_at_location
expects = Observation.index_order.
where(location: locations(:burbank)).distinct
assert_query(expects, :Observation, location: locations(:burbank))
end

# Overwrites scope `order_by_rss_log` in abstract_model
def test_observation_by_rss_log
expects = Observation.order_by_rss_log
assert_query(expects, :Observation, by: :rss_log)
end

def observations_set
[
observations(:unknown_with_no_naming),
observations(:minimal_unknown_obs),
observations(:strobilurus_diminutivus_obs),
observations(:detailed_unknown_obs),
observations(:agaricus_campestros_obs),
observations(:coprinus_comatus_obs),
observations(:agaricus_campestras_obs),
observations(:agaricus_campestris_obs),
observations(:agaricus_campestrus_obs)
]
end

def test_observation_ids_ids
assert_query(observations_set.map(&:id),
:Observation, ids: observations_set.map(&:id))
end

def test_observation_ids_instances
assert_query(observations_set.map(&:id),
:Observation, ids: observations_set)
end

def test_observation_by_user
expects = Observation.reorder(id: :asc).where(user: rolf.id).to_a
assert_query(expects, :Observation, by_user: rolf, by: :id)
Expand All @@ -40,6 +49,21 @@ def test_observation_by_user
assert_query([], :Observation, by_user: junk, by: :id)
end

def test_observation_in_project_list
project = projects(:bolete_project)
# expects = project.species_lists.map(&:observations).flatten.to_a
expects = Observation.index_order.
joins(species_lists: :project_species_lists).
where(project_species_lists: { project: project }).distinct
assert_query(expects, :Observation, project_lists: project.title)
end

def test_observation_at_location
expects = Observation.index_order.
where(location: locations(:burbank)).distinct
assert_query(expects, :Observation, location: locations(:burbank))
end

def test_observation_for_project
assert_query([],
:Observation, project: projects(:empty_project))
Expand All @@ -55,19 +79,6 @@ def test_observation_for_project_projects_equivalence
assert_equal(qu1.results, qu2.results)
end

def test_observation_in_set
obs_set_ids = [observations(:unknown_with_no_naming).id,
observations(:minimal_unknown_obs).id,
observations(:strobilurus_diminutivus_obs).id,
observations(:detailed_unknown_obs).id,
observations(:agaricus_campestros_obs).id,
observations(:coprinus_comatus_obs).id,
observations(:agaricus_campestras_obs).id,
observations(:agaricus_campestris_obs).id,
observations(:agaricus_campestrus_obs).id]
assert_query(obs_set_ids, :Observation, ids: obs_set_ids)
end

def test_observation_in_species_list
# These two are identical, so should be disambiguated by reverse_id.
assert_query([observations(:detailed_unknown_obs).id,
Expand Down

0 comments on commit ecdd13d

Please sign in to comment.