Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Dec 21, 2024
1 parent 9bfbb47 commit e38db46
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 60 deletions.
15 changes: 1 addition & 14 deletions app/classes/query/collection_number_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(" \
Expand Down
2 changes: 1 addition & 1 deletion app/classes/query/external_link_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
38 changes: 13 additions & 25 deletions app/classes/query/herbarium_record_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,'') = ''",
Expand All @@ -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
Expand Down
17 changes: 5 additions & 12 deletions app/classes/query/image_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
26 changes: 24 additions & 2 deletions app/classes/query/modules/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?

Expand Down
6 changes: 1 addition & 5 deletions app/classes/query/name_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/classes/query/sequence_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e38db46

Please sign in to comment.