Skip to content

Commit

Permalink
Consolidate observation param handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Dec 20, 2024
1 parent 817eb80 commit 4cc128c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 357 deletions.
84 changes: 2 additions & 82 deletions app/classes/query/image_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand Down
122 changes: 122 additions & 0 deletions app/classes/query/initializers/observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -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
91 changes: 3 additions & 88 deletions app/classes/query/location_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 4cc128c

Please sign in to comment.