From 4cc128ca5e3538bdda7b8bf3b7828b78ca7b970d Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Fri, 20 Dec 2024 02:29:04 -0800 Subject: [PATCH] Consolidate observation param handlers --- app/classes/query/image_with_observations.rb | 84 +----------- .../query/initializers/observations.rb | 122 ++++++++++++++++++ .../query/location_with_observations.rb | 91 +------------ app/classes/query/name_with_observations.rb | 82 +----------- app/classes/query/observation_base.rb | 107 --------------- 5 files changed, 129 insertions(+), 357 deletions(-) diff --git a/app/classes/query/image_with_observations.rb b/app/classes/query/image_with_observations.rb index dc42f2db22..bc78df425d 100644 --- a/app/classes/query/image_with_observations.rb +++ b/app/classes/query/image_with_observations.rb @@ -35,41 +35,12 @@ def initialize_flavor 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 - ) + initialize_herbaria_parameter + initialize_projects_parameter add_for_project_condition 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_search_condition("observations.where", params[:user_where]) @@ -85,61 +56,10 @@ def initialize_boolean_parameters 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 diff --git a/app/classes/query/initializers/observations.rb b/app/classes/query/initializers/observations.rb index 974051c4b0..5a5e80fca3 100644 --- a/app/classes/query/initializers/observations.rb +++ b/app/classes/query/initializers/observations.rb @@ -40,6 +40,124 @@ def observations_coercion_parameter_declarations } end + def initialize_herbaria_parameter + add_id_condition( + "herbarium_records.herbarium_id", + lookup_herbaria_by_name(params[:herbaria]), + :observations, :observation_herbarium_records, :herbarium_records + ) + end + + def initialize_herbarium_records_parameter + add_id_condition( + "observation_herbarium_records.herbarium_record_id", + lookup_herbarium_records_by_name(params[:herbarium_records]), + :observations, :observation_herbarium_records + ) + end + + def initialize_projects_parameter + project_joins = [:observations, :project_observations] + project_joins << :observation_images if model == Image + + add_id_condition( + "project_observations.project_id", + lookup_projects_by_name(params[:projects]), + *project_joins + ) + 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_collection_location_condition_for_locations + add_join(:observations, :project_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_species_lists_parameter + add_id_condition( + "species_list_observations.species_list_id", + lookup_species_lists_by_name(params[:species_lists]), + :observations, :species_list_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 + where << "species_list_observations.species_list_id = '#{spl.id}'" + add_collection_location_condition_for_locations + add_join(:observations, :species_list_observations) + end + + def add_collection_location_condition_for_locations + return unless model == Location + + where << "observations.is_collection_location IS TRUE" + 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 params_out_to_with_observations_params(pargs) return pargs if pargs[:ids].blank? @@ -54,6 +172,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 diff --git a/app/classes/query/location_with_observations.rb b/app/classes/query/location_with_observations.rb index cab7122f39..ad7e7abd36 100644 --- a/app/classes/query/location_with_observations.rb +++ b/app/classes/query/location_with_observations.rb @@ -53,45 +53,11 @@ def add_where_conditions end def initialize_association_parameters - add_id_condition( - "project_observations.project_id", - lookup_projects_by_name(params[:projects]), - :observations, :project_observations - ) + initialize_projects_parameter add_for_project_condition - add_id_condition( - "species_list_observations.species_list_id", - lookup_species_lists_by_name(params[:species_lists]), - :observations, :species_list_observations - ) + initialize_species_lists_parameter add_in_species_list_condition - add_id_condition( - "herbarium_records.herbarium_id", - lookup_herbaria_by_name(params[:herbaria]), - :observations, :observation_herbarium_records, :herbarium_records - ) - 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]}'" - where << "observations.is_collection_location IS TRUE" - 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(:observations, :species_list_observations) - where << "species_list_observations.species_list_id = '#{spl.id}'" - where << "observations.is_collection_location IS TRUE" + initialize_herbaria_parameter end def initialize_boolean_parameters @@ -104,57 +70,6 @@ def initialize_boolean_parameters add_join(:observations, :sequences) if params[:with_sequences] 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 coerce_into_observation_query - Query.lookup(:Observation, :all, params_back_to_observation_params) - end - def title default = super with_observations_query_description || default diff --git a/app/classes/query/name_with_observations.rb b/app/classes/query/name_with_observations.rb index b9084e3c3e..f7f915342d 100644 --- a/app/classes/query/name_with_observations.rb +++ b/app/classes/query/name_with_observations.rb @@ -37,38 +37,10 @@ def initialize_flavor end def initialize_association_parameters - add_id_condition( - "project_observations.project_id", - lookup_projects_by_name(params[:projects]), - :observations, :project_observations - ) + initialize_projects_parameter add_for_project_condition add_in_species_list_condition - add_id_condition( - "herbarium_records.herbarium_id", - lookup_herbaria_by_name(params[:herbaria]), - :observations, :observation_herbarium_records, :herbarium_records - ) - 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 - where << "species_list_observations.species_list_id = '#{spl.id}'" - add_join(:observations, :species_list_observations) + initialize_herbaria_parameter end def add_where_conditions @@ -86,56 +58,6 @@ def initialize_boolean_parameters add_join(:observations, :sequences) if params[:with_sequences] 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] - ) - 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 coerce_into_observation_query - Query.lookup(:Observation, :all, params_back_to_observation_params) - end - def title default = super with_observations_query_description || default diff --git a/app/classes/query/observation_base.rb b/app/classes/query/observation_base.rb index dabb651c0f..7f83c1e61a 100644 --- a/app/classes/query/observation_base.rb +++ b/app/classes/query/observation_base.rb @@ -91,66 +91,6 @@ def initialize_association_parameters initialize_field_slips_parameter end - def initialize_herbaria_parameter - add_id_condition( - "herbarium_records.herbarium_id", - lookup_herbaria_by_name(params[:herbaria]), - :observation_herbarium_records, :herbarium_records - ) - end - - def initialize_herbarium_records_parameter - add_id_condition( - "observation_herbarium_records.herbarium_record_id", - lookup_herbarium_records_by_name(params[:herbarium_records]), - :observation_herbarium_records - ) - 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("project_observations") - end - - def initialize_projects_parameter - add_id_condition( - "project_observations.project_id", - lookup_projects_by_name(params[:projects]), - :project_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]), - :species_list_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 - where << "species_list_observations.species_list_id = '#{spl.id}'" - add_join(:species_list_observations) - end - - def initialize_species_lists_parameter - add_id_condition( - "species_list_observations.species_list_id", - lookup_species_lists_by_name(params[:species_lists]), - :species_list_observations - ) - end - def initialize_field_slips_parameter return unless params[:field_slips] @@ -243,53 +183,6 @@ def initialize_boolean_parameters add_join(:sequences) if params[:with_sequences] 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], - :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], - :comments - ) - end - def add_join_to_names add_join(:names) end