diff --git a/app/classes/query/collection_number_base.rb b/app/classes/query/collection_number_base.rb index 969a637dfd..538b107f27 100644 --- a/app/classes/query/collection_number_base.rb +++ b/app/classes/query/collection_number_base.rb @@ -20,30 +20,17 @@ def parameter_declarations ) end - # rubocop:disable Metrics/AbcSize def initialize_flavor add_owner_and_time_stamp_conditions("collection_numbers") add_for_observation_condition + initialize_observations_parameter add_pattern_condition - add_id_condition("observation_collection_numbers.observation_id", - params[:observations], :observation_collection_numbers) add_exact_match_condition("collection_numbers.name", params[:name]) add_exact_match_condition("collection_numbers.number", params[:number]) add_search_condition("collection_numbers.name", params[:name_has]) add_search_condition("collection_numbers.number", params[:number_has]) super end - # rubocop:enable Metrics/AbcSize - - def add_for_observation_condition - return if params[:observation].blank? - - obs = find_cached_parameter_instance(Observation, :observation) - @title_tag = :query_title_for_observation - @title_args[:observation] = obs.unique_format_name - where << "observation_collection_numbers.observation_id = '#{obs.id}'" - add_join(:observation_collection_numbers) - end def search_fields "CONCAT(" \ diff --git a/app/classes/query/external_link_base.rb b/app/classes/query/external_link_base.rb index 4bc470e9f6..c79e9716bc 100644 --- a/app/classes/query/external_link_base.rb +++ b/app/classes/query/external_link_base.rb @@ -18,7 +18,7 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("external_links") - add_id_condition("external_links.observation_id", params[:observations]) + initialize_observations_parameter(:external_links) add_id_condition("external_links.external_site_id", lookup_external_sites_by_name(params[:external_sites])) add_search_condition("external_links.url", params[:url]) diff --git a/app/classes/query/herbarium_record_base.rb b/app/classes/query/herbarium_record_base.rb index 33c9311317..85e0ef0be5 100644 --- a/app/classes/query/herbarium_record_base.rb +++ b/app/classes/query/herbarium_record_base.rb @@ -26,8 +26,6 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("herbarium_records") - add_for_observation_condition - add_in_herbarium_condition add_pattern_condition initialize_association_parameters initialize_boolean_parameters @@ -36,14 +34,11 @@ def initialize_flavor super end - def add_for_observation_condition - return if params[:observation].blank? - - obs = find_cached_parameter_instance(Observation, :observation) - @title_tag = :query_title_for_observation - @title_args[:observation] = obs.unique_format_name - where << "observation_herbarium_records.observation_id = '#{obs.id}'" - add_join(:observation_herbarium_records) + def initialize_association_parameters + add_in_herbarium_condition + initialize_herbaria_parameter([]) + add_for_observation_condition + initialize_observations_parameter end def add_in_herbarium_condition @@ -55,21 +50,6 @@ def add_in_herbarium_condition where << "herbarium_records.herbarium_id = '#{herbarium.id}'" end - def search_fields - "CONCAT(" \ - "herbarium_records.initial_det," \ - "herbarium_records.accession_number," \ - "COALESCE(herbarium_records.notes,'')" \ - ")" - end - - def initialize_association_parameters - add_id_condition("herbarium_records.herbarium_id", - lookup_herbaria_by_name(params[:herbaria])) - add_id_condition("observation_herbarium_records.observation_id", - params[:observations], :observation_herbarium_records) - end - def initialize_boolean_parameters add_boolean_condition("COALESCE(herbarium_records.notes,'') != ''", "COALESCE(herbarium_records.notes,'') = ''", @@ -92,6 +72,14 @@ def initialize_search_parameters params[:accession_number_has]) end + def search_fields + "CONCAT(" \ + "herbarium_records.initial_det," \ + "herbarium_records.accession_number," \ + "COALESCE(herbarium_records.notes,'')" \ + ")" + end + def self.default_order "herbarium_label" end diff --git a/app/classes/query/image_base.rb b/app/classes/query/image_base.rb index 8839732d64..e939b741fc 100644 --- a/app/classes/query/image_base.rb +++ b/app/classes/query/image_base.rb @@ -74,22 +74,15 @@ def initialize_association_parameters params[:observations], :observation_images) add_where_condition("observations", params[:locations], :observation_images, :observations) - add_project_conditions - add_id_condition( - "species_list_observations.species_list_id", - lookup_species_lists_by_name(params[:species_lists]), - :observation_images, :observations, :species_list_observations + add_for_project_condition(:project_images) + initialize_projects_parameter(:project_images, [:project_images]) + initialize_species_lists_parameter( + :species_list_observations, + [:observation_images, :observations, :species_list_observations] ) add_id_condition("images.license_id", params[:license]) end - def add_project_conditions - add_for_project_condition(:project_images) - add_id_condition("project_images.project_id", - lookup_projects_by_name(params[:projects]), - :project_images) - end - def initialize_image_parameters add_search_condition("images.copyright_holder", params[:copyright_holder_has]) diff --git a/app/classes/query/modules/associations.rb b/app/classes/query/modules/associations.rb index 5646070cc4..c5bacc1724 100644 --- a/app/classes/query/modules/associations.rb +++ b/app/classes/query/modules/associations.rb @@ -4,11 +4,14 @@ module Query module Modules # Helper methods for turning Query parameters into SQL conditions. module Associations - def initialize_herbaria_parameter + def initialize_herbaria_parameter( + joins = [:observations, :observation_herbarium_records, + :herbarium_records] + ) add_id_condition( "herbarium_records.herbarium_id", lookup_herbaria_by_name(params[:herbaria]), - :observations, :observation_herbarium_records, :herbarium_records + *joins ) end @@ -51,6 +54,25 @@ def add_is_collection_location_condition_for_locations where << "observations.is_collection_location IS TRUE" end + def add_for_observation_condition( + table = :"observation_#{model.table_name}", joins = [table] + ) + return if params[:observation].blank? + + obs = find_cached_parameter_instance(Observation, :observation) + @title_tag = :query_title_for_observation + @title_args[:observation] = obs.unique_format_name + where << "#{table}.observation_id = '#{obs.id}'" + add_join(*joins) + end + + def initialize_observations_parameter( + table = :"observation_#{model.table_name}", joins = [table] + ) + add_id_condition("#{table}.observation_id", params[:observations], + *joins) + end + def add_for_project_condition(table = model.table_name, joins = [table]) return if params[:project].blank? diff --git a/app/classes/query/name_base.rb b/app/classes/query/name_base.rb index cfba73b761..4eb963e247 100644 --- a/app/classes/query/name_base.rb +++ b/app/classes/query/name_base.rb @@ -125,11 +125,7 @@ def initialize_ok_for_export_parameter def initialize_association_parameters add_id_condition("observations.id", params[:observations], :observations) add_where_condition("observations", params[:locations], :observations) - 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 end def initialize_boolean_parameters diff --git a/app/classes/query/sequence_base.rb b/app/classes/query/sequence_base.rb index 7a2ea90496..74c51cefa8 100644 --- a/app/classes/query/sequence_base.rb +++ b/app/classes/query/sequence_base.rb @@ -88,7 +88,7 @@ def search_fields end def initialize_association_parameters - add_id_condition("sequences.observation_id", params[:observations]) + initialize_observations_parameter(:sequences) initialize_observers_parameter add_where_condition("observations", params[:locations], :observations) initialize_herbaria_parameter