Skip to content

Commit

Permalink
Merge pull request #2592 from MushroomObserver/standardize-controller…
Browse files Browse the repository at this point in the history
…-indexes

More Query refactoring
  • Loading branch information
nimmolo authored Dec 21, 2024
2 parents d3b8fe8 + 9bfbb47 commit aeb1765
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 749 deletions.
1 change: 1 addition & 0 deletions app/classes/query/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Base
include Query::Modules::BoundingBox
include Query::Modules::Coercion
include Query::Modules::Conditions
include Query::Modules::Associations
include Query::Modules::Datetime
include Query::Modules::GoogleSearch
include Query::Modules::HighLevelQueries
Expand Down
9 changes: 0 additions & 9 deletions app/classes/query/field_slip_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ def initialize_flavor
super
end

def add_for_project_condition
return if params[:project].blank?

project = find_cached_parameter_instance(Project, :project)
@title_tag = :query_title_for_project
@title_args[:project] = project.title
where << "field_slips.project_id = '#{project.id}'"
end

def self.default_order
"date"
end
Expand Down
14 changes: 1 addition & 13 deletions app/classes/query/image_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ def initialize_flavor
add_join(:observation_images) if params[:with_observation]
initialize_notes_parameters
end
# add_by_user_condition("images")
# add_ids_condition
add_pattern_condition
add_advanced_search_conditions
initialize_association_parameters
Expand Down Expand Up @@ -86,22 +84,12 @@ def initialize_association_parameters
end

def add_project_conditions
add_for_project_condition
add_for_project_condition(:project_images)
add_id_condition("project_images.project_id",
lookup_projects_by_name(params[:projects]),
:project_images)
end

def add_for_project_condition
return if params[:project].blank?

project = find_cached_parameter_instance(Project, :project)
@title_tag = :query_title_for_project
@title_args[:project] = project.title
where << "project_images.project_id = '#{project.id}'"
add_join(:project_images)
end

def initialize_image_parameters
add_search_condition("images.copyright_holder",
params[:copyright_holder_has])
Expand Down
93 changes: 7 additions & 86 deletions app/classes/query/image_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,118 +28,39 @@ def initialize_flavor
initialize_association_parameters
add_where_conditions
initialize_boolean_parameters
initialize_search_parameters
initialize_obs_search_parameters
add_bounding_box_conditions_for_observations
initialize_content_filters(Observation)
super
end

def initialize_association_parameters
add_id_condition(
"herbarium_records.herbarium_id",
lookup_herbaria_by_name(params[:herbaria]),
:observations, :observation_herbarium_records, :herbarium_records
)
add_id_condition(
"project_observations.project_id",
lookup_projects_by_name(params[:projects]),
:observation_images, :observations, :project_observations
)
add_for_project_condition
initialize_herbaria_parameter
initialize_projects_parameter(:project_observations)
add_for_project_condition(:project_observations,
[:observations, :project_observations])
add_in_species_list_condition
end

def add_for_project_condition
return if params[:project].blank?

project = find_cached_parameter_instance(Project, :project)
@title_tag = :query_title_for_project
@title_args[:project] = project.title
where << "project_observations.project_id = '#{params[:project]}'"
add_join(:observations, :project_observations)
end

def add_in_species_list_condition
return if params[:species_list].blank?

spl = find_cached_parameter_instance(SpeciesList, :species_list)
@title_tag = :query_title_in_species_list
@title_args[:species_list] = spl.format_name
add_join(:observation_images, :observations)
add_join(:observations, :species_list_observations)
where << "species_list_observations.species_list_id = '#{spl.id}'"
end

def add_where_conditions
add_at_location_parameter(:observations)
add_at_location_condition(:observations)
add_search_condition("observations.where", params[:user_where])
end

def initialize_boolean_parameters
initialize_is_collection_location_parameter
initialize_with_public_lat_lng_parameter
initialize_with_name_parameter
initialize_with_notes_parameter
initialize_with_obs_notes_parameter
add_join(:observations, :comments) if params[:with_comments]
add_join(:observations, :sequences) if params[:with_sequences]
add_with_notes_fields_condition(params[:with_notes_fields])
end

def initialize_is_collection_location_parameter
add_boolean_condition(
"observations.is_collection_location IS TRUE",
"observations.is_collection_location IS FALSE",
params[:is_collection_location]
)
end

def initialize_with_public_lat_lng_parameter
add_boolean_condition(
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:with_public_lat_lng]
)
end

def initialize_with_name_parameter
genus = Name.ranks[:Genus]
group = Name.ranks[:Group]
add_boolean_condition(
"names.`rank` <= #{genus} or names.`rank` = #{group}",
"names.`rank` > #{genus} and names.`rank` < #{group}",
params[:with_name],
:observations, :names
)
end

def initialize_with_notes_parameter
add_boolean_condition(
"observations.notes != #{escape(Observation.no_notes_persisted)}",
"observations.notes = #{escape(Observation.no_notes_persisted)}",
params[:with_notes]
)
end

def initialize_search_parameters
add_search_condition(
"observations.notes",
params[:notes_has]
)
add_search_condition(
"CONCAT(comments.summary,COALESCE(comments.comment,''))",
params[:comments_has],
:observations, :comments
)
end

def default_order
"name"
end

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

def title
default = super
with_observations_query_description || default
Expand Down
70 changes: 60 additions & 10 deletions app/classes/query/initializers/descriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,54 @@ def descriptions_coercion_parameter_declarations
}
end

def params_out_to_with_descriptions_params
pargs = params_plus_old_by
return pargs if pargs[:ids].blank?
def add_desc_ids_condition(type)
return unless params[:desc_ids]

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

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

def params_back_to_description_params
pargs = params_with_old_by_restored
return pargs if pargs[:desc_ids].blank?
def add_desc_by_user_condition(type)
return unless params[:by_user]

pargs[:ids] = pargs.delete(:desc_ids)
pargs
user = find_cached_parameter_instance(User, :by_user)
@title_tag = :query_title_with_descriptions_by_user.t(type: type)
@title_args[:user] = user.legal_name
add_join(:"#{type}_descriptions")
where << "#{type}_descriptions.user_id = '#{user.id}'"
end

def add_desc_by_author_condition(type)
return unless params[:by_author]

# Change this conditional to check for :with_descriptions param
with_desc = [Name, Location].include?(model) ? "_with_descriptions" : ""
user = find_cached_parameter_instance(User, :by_author)
@title_tag = :"query_title#{with_desc}_by_author".t(
type: :"#{type}_description", user: user.legal_name
)
@title_args[:user] = user.legal_name
add_join(:"#{type}_descriptions", :"#{type}_description_authors")
where << "#{type}_description_authors.user_id = '#{user.id}'"
end

def add_desc_by_editor_condition(type)
return unless params[:by_editor]

# Change this conditional to check for :with_descriptions param
with_desc = [Name, Location].include?(model) ? "_with_descriptions" : ""
user = find_cached_parameter_instance(User, :by_editor)
@title_tag = :"query_title#{with_desc}_by_editor".t(
type: :"#{type}_description", user: user.legal_name
)
@title_args[:user] = user.legal_name
add_join(:"#{type}_descriptions", :"#{type}_description_editors")
where << "#{type}_description_editors.user_id = '#{user.id}'"
end

def initialize_description_parameters(type = :name)
Expand Down Expand Up @@ -95,6 +129,22 @@ def initialize_desc_content_parameter(type)
fields = "CONCAT(#{fields.join(",")})"
add_search_condition(fields, params[:desc_content])
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
end
end
end
115 changes: 115 additions & 0 deletions app/classes/query/initializers/observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,117 @@ def observations_coercion_parameter_declarations
}
end

# This is just to allow the additional location conditions
def add_ids_condition(table = model.table_name, ids = :ids)
return if params[ids].nil? # [] is valid

super
add_is_collection_location_condition_for_locations
end

def initialize_obs_date_parameter(param_name = :date)
add_date_condition(
"observations.when",
params[param_name],
:observations
)
end

def initialize_confidence_parameter
add_range_condition(
"observations.vote_cache",
params[:confidence],
:observations
)
end

def initialize_project_lists_parameter
add_id_condition(
"species_list_observations.species_list_id",
lookup_lists_for_projects_by_name(params[:project_lists]),
:observations, :species_list_observations
)
end

def initialize_field_slips_parameter
return unless params[:field_slips]

add_join(:field_slips)
add_exact_match_condition(
"field_slips.code",
params[:field_slips],
:observations
)
end

def initialize_is_collection_location_parameter
add_boolean_condition(
"observations.is_collection_location IS TRUE",
"observations.is_collection_location IS FALSE",
params[:is_collection_location],
:observations
)
end

def initialize_with_public_lat_lng_parameter
add_boolean_condition(
"observations.lat IS NOT NULL AND observations.gps_hidden IS FALSE",
"observations.lat IS NULL OR observations.gps_hidden IS TRUE",
params[:with_public_lat_lng],
:observations
)
end

def initialize_with_images_parameter
add_boolean_condition(
"observations.thumb_image_id IS NOT NULL",
"observations.thumb_image_id IS NULL",
params[:with_images],
:observations
)
end

def initialize_with_specimen_parameter
add_boolean_condition(
"observations.specimen IS TRUE",
"observations.specimen IS FALSE",
params[:with_specimen],
:observations
)
end

def initialize_with_name_parameter
genus = Name.ranks[:Genus]
group = Name.ranks[:Group]
add_boolean_condition(
"names.`rank` <= #{genus} or names.`rank` = #{group}",
"names.`rank` > #{genus} and names.`rank` < #{group}",
params[:with_name],
:observations, :names
)
end

def initialize_with_obs_notes_parameter(param_name = :with_notes)
add_boolean_condition(
"observations.notes != #{escape(Observation.no_notes_persisted)}",
"observations.notes = #{escape(Observation.no_notes_persisted)}",
params[param_name],
:observations
)
end

def initialize_obs_search_parameters
add_search_condition(
"observations.notes",
params[:notes_has]
)
add_search_condition(
"CONCAT(comments.summary,COALESCE(comments.comment,''))",
params[:comments_has],
:observations, :comments
)
end

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

Expand All @@ -54,6 +165,10 @@ def params_back_to_observation_params
pargs[:ids] = pargs.delete(:obs_ids)
pargs
end

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

0 comments on commit aeb1765

Please sign in to comment.