Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor :with_observation and :with_description query parameters #2589

Merged
merged 9 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions app/classes/query/image_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,25 @@
module Query
# Query's for Images where Observation meets specified conditions
class ImageWithObservations < Query::ImageBase
include Query::Initializers::ContentFilters
include Query::Initializers::Names
include Query::Initializers::Observations
include Query::Initializers::Locations
include Query::Initializers::ContentFilters
include Query::Initializers::ObservationQueryDescriptions

def parameter_declarations
super.merge(
old_title?: :string,
old_by?: :string,
ids?: [Observation],
herbaria?: [:string],
location?: Location,
user_where?: :string,
project?: Project,
species_list?: SpeciesList,
is_collection_location?: :boolean,
with_public_lat_lng?: :boolean,
with_name?: :boolean,
with_comments?: { boolean: [true] },
with_sequences?: { boolean: [true] },
with_notes_fields?: [:string],
comments_has?: :string,
north?: :float,
south?: :float,
east?: :float,
west?: :float
).merge(content_filter_parameter_declarations(Observation)).
merge(consensus_parameter_declarations)
obs_ids?: [Observation]
).merge(observations_parameter_declarations).
merge(observations_coercion_parameter_declarations).
merge(bounding_box_parameter_declarations).
merge(content_filter_parameter_declarations(Observation)).
merge(naming_consensus_parameter_declarations)
end

def initialize_flavor
add_join(:observation_images, :observations)
add_ids_condition("observations")
add_ids_condition("observations", :obs_ids)
add_owner_and_time_stamp_conditions("observations")
add_by_user_condition("observations")
add_date_condition("observations.when", params[:date])
Expand Down Expand Up @@ -150,7 +137,7 @@ def default_order
end

def coerce_into_observation_query
Query.lookup(:Observation, :all, params_with_old_by_restored)
Query.lookup(:Observation, :all, params_back_to_observation_params)
end

def title
Expand Down
24 changes: 24 additions & 0 deletions app/classes/query/initializers/descriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ def descriptions_parameter_declarations
}
end

def descriptions_coercion_parameter_declarations
{
old_title?: :string,
old_by?: :string,
by_author?: User
}
end

def params_out_to_with_descriptions_params
pargs = params_plus_old_by
return pargs if pargs[:ids].blank?

pargs[:desc_ids] = pargs.delete(:ids)
pargs
end

def params_back_to_description_params
pargs = params_with_old_by_restored
return pargs if pargs[:desc_ids].blank?

pargs[:ids] = pargs.delete(:desc_ids)
pargs
end

def initialize_description_parameters(type = :name)
initialize_with_default_desc_parameter(type)
initialize_join_desc_parameter(type)
Expand Down
17 changes: 17 additions & 0 deletions app/classes/query/initializers/locations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Query
module Initializers
# initializing methods inherited by all Query's for Locations
module Locations
def bounding_box_parameter_declarations
{
north?: :float,
south?: :float,
east?: :float,
west?: :float
}
end
end
end
end
26 changes: 10 additions & 16 deletions app/classes/query/initializers/names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,17 @@ def names_parameter_declarations
}
end

def consensus_parameter_declarations
def naming_consensus_parameter_declarations
{
include_all_name_proposals?: :boolean,
exclude_consensus?: :boolean
}
end

def names_parameters
{
names: params[:names],
include_synonyms: params[:include_synonyms],
include_subtaxa: params[:include_subtaxa],
include_immediate_subtaxa: params[:include_immediate_subtaxa],
exclude_original_names: params[:exclude_original_names]
}
end

def initialize_name_parameters(*joins)
return force_empty_results if irreconcilable_name_parameters?

table = if params[:include_all_name_proposals]
"namings"
else
"observations"
end
table = params[:include_all_name_proposals] ? "namings" : "observations"
column = "#{table}.name_id"
ids = lookup_names_by_name(names_parameters)
add_id_condition(column, ids, *joins)
Expand All @@ -55,6 +41,14 @@ def initialize_name_parameters_for_name_queries
add_id_condition("names.id", lookup_names_by_name(names_parameters))
end

# Copy only the names_parameters into a name_params hash we use here.
def names_parameters
name_params = names_parameter_declarations.dup
name_params.transform_keys! { |k| k.to_s.chomp("?").to_sym }
name_params.each_key { |k| name_params[k] = params[k] }
name_params
end

# ------------------------------------------------------------------------

private
Expand Down
59 changes: 59 additions & 0 deletions app/classes/query/initializers/observations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module Query
module Initializers
# initializing methods inherited by all Query's for Observations
module Observations
def observations_parameter_declarations
{
notes_has?: :string,
with_notes_fields?: [:string],
comments_has?: :string,
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]
}
end

def observations_coercion_parameter_declarations
{
old_title?: :string,
old_by?: :string,
date?: [:date]
}
end

def params_out_to_with_observations_params(pargs)
return pargs if pargs[:ids].blank?

pargs[:obs_ids] = pargs.delete(:ids)
pargs
end

def params_back_to_observation_params
pargs = params_with_old_by_restored
return pargs if pargs[:obs_ids].blank?

pargs[:ids] = pargs.delete(:obs_ids)
pargs
end
end
end
end
9 changes: 4 additions & 5 deletions app/classes/query/location_base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Query::LocationBase < Query::Base
include Query::Initializers::Locations
include Query::Initializers::Descriptions
include Query::Initializers::ContentFilters
include Query::Initializers::AdvancedSearch

Expand All @@ -16,13 +18,10 @@ def parameter_declarations
by_user?: User,
by_editor?: User,
users?: [User],
north?: :float,
south?: :float,
east?: :float,
west?: :float,
pattern?: :string,
regexp?: :string
).merge(content_filter_parameter_declarations(Location)).
).merge(bounding_box_parameter_declarations).
merge(content_filter_parameter_declarations(Location)).
merge(advanced_search_parameter_declarations)
end

Expand Down
3 changes: 2 additions & 1 deletion app/classes/query/location_description_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize_flavor
end

def coerce_into_location_query
Query.lookup(:Location, :with_descriptions, params_plus_old_by)
pargs = params_out_to_with_descriptions_params
Query.lookup(:Location, :with_descriptions, pargs)
end
end
2 changes: 2 additions & 0 deletions app/classes/query/location_description_base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Query::LocationDescriptionBase < Query::Base
include Query::Initializers::Descriptions

def model
LocationDescription
end
Expand Down
16 changes: 8 additions & 8 deletions app/classes/query/location_with_descriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
class Query::LocationWithDescriptions < Query::LocationBase
def parameter_declarations
super.merge(
ids?: [LocationDescription],
old_title?: :string,
old_by?: :string,
by_author?: User
)
desc_ids?: [LocationDescription]
).merge(descriptions_coercion_parameter_declarations)
end

def initialize_flavor
Expand All @@ -20,12 +17,15 @@ def initialize_flavor
end

def add_ids_condition
return unless params[:ids]
return unless params[:desc_ids]

set = clean_id_set(params[:desc_ids])
@where << "location_descriptions.id IN (#{set})"
self.order = "FIND_IN_SET(location_descriptions.id,'#{set}') ASC"

@title_tag = :query_title_with_descriptions.t(type: :location)
@title_args[:descriptions] = params[:old_title] ||
:query_title_in_set.t(type: :description)
initialize_in_set_flavor("location_descriptions")
end

def add_by_user_condition
Expand Down Expand Up @@ -59,6 +59,6 @@ def add_by_editor_condition
end

def coerce_into_location_description_query
Query.lookup(:LocationDescription, :all, params_with_old_by_restored)
Query.lookup(:LocationDescription, :all, params_back_to_description_params)
end
end
45 changes: 15 additions & 30 deletions app/classes/query/location_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,24 @@
module Query
# Query's for Locations where Observation meets specified conditions
class LocationWithObservations < Query::LocationBase
include Query::Initializers::ContentFilters
include Query::Initializers::Names
include Query::Initializers::Observations
include Query::Initializers::ContentFilters
include Query::Initializers::ObservationQueryDescriptions

def parameter_declarations
super.merge(
old_by?: :string,
date?: [:date],
ids?: [Observation],
old_title?: :string,
locations?: [:string],
location?: Location,
user_where?: :string,
project?: Project,
projects?: [:string],
species_list?: SpeciesList,
species_lists?: [:string],
herbaria?: [:string],
confidence?: [:float],
is_collection_location?: :boolean,
with_public_lat_lng?: :boolean,
with_name?: :boolean,
with_comments?: { boolean: [true] },
with_sequences?: { boolean: [true] },
with_notes?: :boolean,
with_notes_fields?: [:string],
notes_has?: :string,
comments_has?: :string
).merge(content_filter_parameter_declarations(Observation)).
obs_ids?: [Observation]
).merge(observations_parameter_declarations).
merge(observations_coercion_parameter_declarations).
merge(content_filter_parameter_declarations(Observation)).
merge(names_parameter_declarations).
merge(consensus_parameter_declarations)
merge(naming_consensus_parameter_declarations)
end

def initialize_flavor
add_join(:observations)
add_ids_condition("observations")
add_ids_condition
add_owner_and_time_stamp_conditions("observations")
add_by_user_condition("observations")
add_date_condition("observations.when", params[:date])
Expand All @@ -52,13 +34,16 @@ def initialize_flavor
super
end

def add_ids_condition(table)
return unless params[:ids]
def add_ids_condition
return unless params[:obs_ids]

set = clean_id_set(params[:obs_ids])
@where << "observations.id IN (#{set})"
self.order = "FIND_IN_SET(observations.id,'#{set}') ASC"

@title_args[:observations] = params[:old_title] ||
:query_title_in_set.t(type: :observation)
where << "observations.is_collection_location IS TRUE"
initialize_in_set_flavor(table)
end

def add_where_conditions
Expand Down Expand Up @@ -167,7 +152,7 @@ def initialize_search_parameters
end

def coerce_into_observation_query
Query.lookup(:Observation, :all, params_with_old_by_restored)
Query.lookup(:Observation, :all, params_back_to_observation_params)
end

def title
Expand Down
Loading
Loading