From ebd14149ac95a10eaa4e49446b8d920722c8fc59 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Thu, 3 Oct 2024 19:42:36 -0700 Subject: [PATCH 01/28] low hanging fruit --- .../query/collection_number_for_observation.rb | 17 ----------------- .../query/herbarium_record_for_observation.rb | 17 ----------------- .../collection_numbers_controller.rb | 4 ++-- app/controllers/herbarium_records_controller.rb | 4 ++-- test/models/query_test.rb | 7 +++---- 5 files changed, 7 insertions(+), 42 deletions(-) delete mode 100644 app/classes/query/collection_number_for_observation.rb delete mode 100644 app/classes/query/herbarium_record_for_observation.rb diff --git a/app/classes/query/collection_number_for_observation.rb b/app/classes/query/collection_number_for_observation.rb deleted file mode 100644 index d616f28437..0000000000 --- a/app/classes/query/collection_number_for_observation.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class Query::CollectionNumberForObservation < Query::CollectionNumberBase - def parameter_declarations - super.merge( - observation: Observation - ) - end - - def initialize_flavor - obs = find_cached_parameter_instance(Observation, :observation) - title_args[:observation] = obs.unique_format_name - where << "observation_collection_numbers.observation_id = '#{obs.id}'" - add_join(:observation_collection_numbers) - super - end -end diff --git a/app/classes/query/herbarium_record_for_observation.rb b/app/classes/query/herbarium_record_for_observation.rb deleted file mode 100644 index dab20fe730..0000000000 --- a/app/classes/query/herbarium_record_for_observation.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumRecordForObservation < Query::HerbariumRecordBase - def parameter_declarations - super.merge( - observation: Observation - ) - end - - def initialize_flavor - obs = find_cached_parameter_instance(Observation, :observation) - title_args[:observation] = obs.unique_format_name - where << "observation_herbarium_records.observation_id = '#{obs.id}'" - add_join(:observation_herbarium_records) - super - end -end diff --git a/app/controllers/collection_numbers_controller.rb b/app/controllers/collection_numbers_controller.rb index e1418e477b..b4de7ddf44 100644 --- a/app/controllers/collection_numbers_controller.rb +++ b/app/controllers/collection_numbers_controller.rb @@ -136,8 +136,8 @@ def pattern def observation_id @observation = Observation.find(params[:observation_id]) store_location - query = create_query(:CollectionNumber, :for_observation, - observation: params[:observation_id].to_s) + query = create_query(:CollectionNumber, :all, + observations: params[:observation_id].to_s) show_selected_collection_numbers(query, always_index: true) end diff --git a/app/controllers/herbarium_records_controller.rb b/app/controllers/herbarium_records_controller.rb index b837a705bf..2cf24e8965 100644 --- a/app/controllers/herbarium_records_controller.rb +++ b/app/controllers/herbarium_records_controller.rb @@ -168,8 +168,8 @@ def herbarium_id def observation_id @observation = Observation.find(params[:observation_id]) store_location - query = create_query(:HerbariumRecord, :for_observation, - observation: params[:observation_id].to_s, + query = create_query(:HerbariumRecord, :all, + observations: params[:observation_id].to_s, by: :herbarium_label) show_selected_herbarium_records(query, always_index: true) end diff --git a/test/models/query_test.rb b/test/models/query_test.rb index a3b46f7f11..16349492bb 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1361,8 +1361,8 @@ def test_collection_number_all def test_collection_number_for_observation obs = observations(:detailed_unknown_obs) expect = obs.collection_numbers.sort_by(&:format_name) - assert_query(expect, :CollectionNumber, :for_observation, - observation: obs.id) + assert_query(expect, :CollectionNumber, :all, + observations: obs.id) end def test_collection_number_pattern_search @@ -1531,8 +1531,7 @@ def test_herbarium_record_all def test_herbarium_record_for_observation obs = observations(:coprinus_comatus_obs) expect = obs.herbarium_records.sort_by(&:herbarium_label) - assert_query(expect, :HerbariumRecord, :for_observation, - observation: obs.id) + assert_query(expect, :HerbariumRecord, :all, observations: obs.id) end def test_herbarium_record_in_herbarium From 8d97c30d6f272f128a4689fc15e316c72d5bae1b Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Thu, 3 Oct 2024 19:46:45 -0700 Subject: [PATCH 02/28] sequence_for_observation --- app/classes/query/sequence_for_observation.rb | 16 ---------------- test/controllers/sequences_controller_test.rb | 12 ++++++------ 2 files changed, 6 insertions(+), 22 deletions(-) delete mode 100644 app/classes/query/sequence_for_observation.rb diff --git a/app/classes/query/sequence_for_observation.rb b/app/classes/query/sequence_for_observation.rb deleted file mode 100644 index bbed778f43..0000000000 --- a/app/classes/query/sequence_for_observation.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -class Query::SequenceForObservation < Query::SequenceBase - def parameter_declarations - super.merge( - observation: Observation - ) - end - - def initialize_flavor - obs = find_cached_parameter_instance(Observation, :observation) - title_args[:observation] = obs.unique_format_name - where << "sequences.observation_id = '#{obs.id}'" - super - end -end diff --git a/test/controllers/sequences_controller_test.rb b/test/controllers/sequences_controller_test.rb index df5deff435..1001bece61 100644 --- a/test/controllers/sequences_controller_test.rb +++ b/test/controllers/sequences_controller_test.rb @@ -23,7 +23,7 @@ def obs_creator(sequence) def test_index login obs = observations(:genbanked_obs) - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) results = query.results assert_operator(results.count, :>, 3) q = query.id.alphabetize @@ -360,7 +360,7 @@ def test_edit_redirect obs = observations(:genbanked_obs) sequence = obs.sequences[2] assert_operator(obs.sequences.count, :>, 3) - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) q = query.id.alphabetize params = { id: sequence.id, sequence: { locus: sequence.locus, @@ -574,7 +574,7 @@ def test_update_redirect_to_observation_keeps_params obs = observations(:genbanked_obs) assert_operator(obs.sequences.count, :>, 3) sequence = obs.sequences[2] - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) q = query.id.alphabetize params = { id: sequence.id, sequence: { locus: sequence.locus, @@ -591,7 +591,7 @@ def test_update_redirect_to_sequence_keeps_params obs = observations(:genbanked_obs) assert_operator(obs.sequences.count, :>, 3) sequence = obs.sequences[2] - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) q = query.id.alphabetize params = { id: sequence.id, sequence: { locus: sequence.locus, @@ -672,7 +672,7 @@ def test_destroy_redirect def test_destroy_redirect_to_observation_with_query obs = observations(:genbanked_obs) seqs = obs.sequences - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) q = query.id.alphabetize # Prove that it keeps query param intact when returning to observation. @@ -684,7 +684,7 @@ def test_destroy_redirect_to_observation_with_query def test_destroy_redirect_to_index_with_query obs = observations(:genbanked_obs) seqs = obs.sequences - query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs) + query = Query.lookup_and_save(:Sequence, :all, observations: obs) q = query.id.alphabetize # Prove that it can return to index, too, with query intact. From 930f154d6f3c8ebe5a5b60bbbdf5b2f11fb997a7 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Fri, 4 Oct 2024 02:16:51 -0700 Subject: [PATCH 03/28] c_n, h_r, seq --- app/classes/query/collection_number_base.rb | 28 +++++++++ .../query/collection_number_pattern_search.rb | 21 ------- app/classes/query/herbarium_record_base.rb | 40 +++++++++++++ .../query/herbarium_record_in_herbarium.rb | 16 ----- .../query/herbarium_record_pattern_search.rb | 22 ------- app/classes/query/sequence_base.rb | 31 +++++++++- app/classes/query/sequence_in_set.rb | 14 ----- app/classes/query/sequence_pattern_search.rb | 26 -------- .../collection_numbers_controller.rb | 4 +- .../herbarium_records_controller.rb | 6 +- app/controllers/sequences_controller.rb | 1 + test/models/query_test.rb | 60 ++++++++++--------- 12 files changed, 135 insertions(+), 134 deletions(-) delete mode 100644 app/classes/query/collection_number_pattern_search.rb delete mode 100644 app/classes/query/herbarium_record_in_herbarium.rb delete mode 100644 app/classes/query/herbarium_record_pattern_search.rb delete mode 100644 app/classes/query/sequence_in_set.rb delete mode 100644 app/classes/query/sequence_pattern_search.rb diff --git a/app/classes/query/collection_number_base.rb b/app/classes/query/collection_number_base.rb index 16a31541e2..1b0bbda43d 100644 --- a/app/classes/query/collection_number_base.rb +++ b/app/classes/query/collection_number_base.rb @@ -10,7 +10,9 @@ def parameter_declarations created_at?: [:time], updated_at?: [:time], users?: [User], + observation?: Observation, observations?: [:string], + pattern?: :string, name?: [:string], number?: [:string], name_has?: :string, @@ -20,6 +22,8 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("collection_numbers") + add_for_observation_condition + 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]) @@ -29,6 +33,30 @@ 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_collection_numbers.observation_id = '#{obs.id}'" + add_join(:observation_collection_numbers) + end + + def add_pattern_condition + return if params[:pattern].blank? + + @title_tag = :query_title_pattern_search + add_search_condition(search_fields, params[:pattern]) + end + + def search_fields + "CONCAT(" \ + "collection_numbers.name," \ + "collection_numbers.number" \ + ")" + end + def self.default_order "name_and_number" end diff --git a/app/classes/query/collection_number_pattern_search.rb b/app/classes/query/collection_number_pattern_search.rb deleted file mode 100644 index ec5838e09e..0000000000 --- a/app/classes/query/collection_number_pattern_search.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -class Query::CollectionNumberPatternSearch < Query::CollectionNumberBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "collection_numbers.name," \ - "collection_numbers.number" \ - ")" - end -end diff --git a/app/classes/query/herbarium_record_base.rb b/app/classes/query/herbarium_record_base.rb index 699c726ba4..a5b8d80e7d 100644 --- a/app/classes/query/herbarium_record_base.rb +++ b/app/classes/query/herbarium_record_base.rb @@ -11,7 +11,10 @@ def parameter_declarations updated_at?: [:time], users?: [User], herbaria?: [:string], + herbarium?: Herbarium, + observation?: Observation, observations?: [:string], + pattern?: :string, with_notes?: :boolean, initial_det?: [:string], accession_number?: [:string], @@ -23,6 +26,9 @@ 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 initialize_exact_match_parameters @@ -30,6 +36,40 @@ 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) + end + + def add_in_herbarium_condition + return if params[:herbarium].blank? + + herbarium = find_cached_parameter_instance(Herbarium, :herbarium) + @title_tag = :query_title_in_herbarium + @title_args[:herbarium] = herbarium.name + where << "herbarium_records.herbarium_id = '#{herbarium.id}'" + end + + def add_pattern_condition + return if params[:pattern].blank? + + @title_tag = :query_title_pattern_search + add_search_condition(search_fields, params[:pattern]) + 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])) diff --git a/app/classes/query/herbarium_record_in_herbarium.rb b/app/classes/query/herbarium_record_in_herbarium.rb deleted file mode 100644 index 8d3428e062..0000000000 --- a/app/classes/query/herbarium_record_in_herbarium.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumRecordInHerbarium < Query::HerbariumRecordBase - def parameter_declarations - super.merge( - herbarium: Herbarium - ) - end - - def initialize_flavor - herbarium = find_cached_parameter_instance(Herbarium, :herbarium) - title_args[:herbarium] = herbarium.name - where << "herbarium_records.herbarium_id = '#{herbarium.id}'" - super - end -end diff --git a/app/classes/query/herbarium_record_pattern_search.rb b/app/classes/query/herbarium_record_pattern_search.rb deleted file mode 100644 index e2204a021e..0000000000 --- a/app/classes/query/herbarium_record_pattern_search.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumRecordPatternSearch < Query::HerbariumRecordBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "herbarium_records.initial_det," \ - "herbarium_records.accession_number," \ - "COALESCE(herbarium_records.notes,'')" \ - ")" - end -end diff --git a/app/classes/query/sequence_base.rb b/app/classes/query/sequence_base.rb index 9640d64981..651f2c2cfd 100644 --- a/app/classes/query/sequence_base.rb +++ b/app/classes/query/sequence_base.rb @@ -26,7 +26,9 @@ def sequence_parameter_declarations accession?: [:string], locus_has?: :string, accession_has?: :string, - notes_has?: :string + notes_has?: :string, + pattern?: :string, + ids?: [Sequence] } end @@ -60,6 +62,8 @@ def initialize_flavor # for. Users would probably not understand why the search fails to find # some sequences because of this. add_owner_and_time_stamp_conditions("sequences") + add_pattern_condition + add_in_set_condition initialize_association_parameters initialize_name_parameters(:observations) initialize_observation_parameters @@ -70,6 +74,31 @@ def initialize_flavor super end + def add_pattern_condition + return if params[:pattern].blank? + + @title_tag = :query_title_pattern_search + add_search_condition(search_fields, params[:pattern]) + end + + def search_fields + # I'm leaving out bases because it would be misleading. Some formats + # allow spaces and other delimiting "garbage" which could break up + # the subsequence the user is searching for. + "CONCAT(" \ + "COALESCE(sequences.locus,'')," \ + "COALESCE(sequences.archive,'')," \ + "COALESCE(sequences.accession,'')," \ + "COALESCE(sequences.notes,'')" \ + ")" + end + + def add_in_set_condition + return if params[:ids].blank? + + initialize_in_set_flavor + end + def initialize_association_parameters add_id_condition("sequences.observation_id", params[:observations]) initialize_observers_parameter diff --git a/app/classes/query/sequence_in_set.rb b/app/classes/query/sequence_in_set.rb deleted file mode 100644 index 15e004d347..0000000000 --- a/app/classes/query/sequence_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::SequenceInSet < Query::SequenceBase - def parameter_declarations - super.merge( - ids: [Sequence] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/sequence_pattern_search.rb b/app/classes/query/sequence_pattern_search.rb deleted file mode 100644 index 33fd27425d..0000000000 --- a/app/classes/query/sequence_pattern_search.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -class Query::SequencePatternSearch < Query::SequenceBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - # I'm leaving out bases because it would be misleading. Some formats - # allow spaces and other delimiting "garbage" which could break up - # the subsequence the user is searching for. - "CONCAT(" \ - "COALESCE(sequences.locus,'')," \ - "COALESCE(sequences.archive,'')," \ - "COALESCE(sequences.accession,'')," \ - "COALESCE(sequences.notes,'')" \ - ")" - end -end diff --git a/app/controllers/collection_numbers_controller.rb b/app/controllers/collection_numbers_controller.rb index b4de7ddf44..1ea115420e 100644 --- a/app/controllers/collection_numbers_controller.rb +++ b/app/controllers/collection_numbers_controller.rb @@ -127,7 +127,7 @@ def pattern (collection_number = CollectionNumber.safe_find(pat)) redirect_to(action: :show, id: collection_number.id) else - query = create_query(:CollectionNumber, :pattern_search, pattern: pat) + query = create_query(:CollectionNumber, :all, pattern: pat) show_selected_collection_numbers(query) end end @@ -137,7 +137,7 @@ def observation_id @observation = Observation.find(params[:observation_id]) store_location query = create_query(:CollectionNumber, :all, - observations: params[:observation_id].to_s) + observation: params[:observation_id].to_s) show_selected_collection_numbers(query, always_index: true) end diff --git a/app/controllers/herbarium_records_controller.rb b/app/controllers/herbarium_records_controller.rb index 2cf24e8965..ef20e2bc14 100644 --- a/app/controllers/herbarium_records_controller.rb +++ b/app/controllers/herbarium_records_controller.rb @@ -152,14 +152,14 @@ def pattern (herbarium_record = HerbariumRecord.safe_find(pattern)) redirect_to(herbarium_record_path(herbarium_record.id)) else - query = create_query(:HerbariumRecord, :pattern_search, pattern: pattern) + query = create_query(:HerbariumRecord, :all, pattern: pattern) show_selected_herbarium_records(query) end end def herbarium_id store_location - query = create_query(:HerbariumRecord, :in_herbarium, + query = create_query(:HerbariumRecord, :all, herbarium: params[:herbarium_id].to_s, by: :herbarium_label) show_selected_herbarium_records(query, always_index: true) @@ -169,7 +169,7 @@ def observation_id @observation = Observation.find(params[:observation_id]) store_location query = create_query(:HerbariumRecord, :all, - observations: params[:observation_id].to_s, + observation: params[:observation_id].to_s, by: :herbarium_label) show_selected_herbarium_records(query, always_index: true) end diff --git a/app/controllers/sequences_controller.rb b/app/controllers/sequences_controller.rb index 949165da97..0286bb4487 100644 --- a/app/controllers/sequences_controller.rb +++ b/app/controllers/sequences_controller.rb @@ -43,6 +43,7 @@ class SequencesController < ApplicationController # https://mushroomobserver.org/sequences?flavor=all # => displays a list of all sequences in MO # + # FIXME: this does not handle params[:pattern] or params[:ids] def index case params[:flavor] when "all" diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 16349492bb..ae2188fcf6 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1361,8 +1361,7 @@ def test_collection_number_all def test_collection_number_for_observation obs = observations(:detailed_unknown_obs) expect = obs.collection_numbers.sort_by(&:format_name) - assert_query(expect, :CollectionNumber, :all, - observations: obs.id) + assert_query(expect, :CollectionNumber, :all, observation: obs.id) end def test_collection_number_pattern_search @@ -1370,13 +1369,13 @@ def test_collection_number_pattern_search where(CollectionNumber[:name].matches("%Singer%"). or(CollectionNumber[:number].matches("%Singer%"))). sort_by(&:format_name) - assert_query(expect, :CollectionNumber, :pattern_search, pattern: "Singer") + assert_query(expect, :CollectionNumber, :all, pattern: "Singer") expect = CollectionNumber. where(CollectionNumber[:name].matches("%123a%"). or(CollectionNumber[:number].matches("%123a%"))). sort_by(&:format_name) - assert_query(expect, :CollectionNumber, :pattern_search, pattern: "123a") + assert_query(expect, :CollectionNumber, :all, pattern: "123a") end def test_comment_all @@ -1531,13 +1530,34 @@ def test_herbarium_record_all def test_herbarium_record_for_observation obs = observations(:coprinus_comatus_obs) expect = obs.herbarium_records.sort_by(&:herbarium_label) - assert_query(expect, :HerbariumRecord, :all, observations: obs.id) + assert_query(expect, :HerbariumRecord, :all, observation: obs.id) end def test_herbarium_record_in_herbarium nybg = herbaria(:nybg_herbarium) expect = nybg.herbarium_records.sort_by(&:herbarium_label) - assert_query(expect, :HerbariumRecord, :in_herbarium, herbarium: nybg.id) + assert_query(expect, :HerbariumRecord, :all, herbarium: nybg.id) + end + + def test_herbarium_record_pattern_search + expect = [herbarium_records(:interesting_unknown)] + assert_query(expect, :HerbariumRecord, :all, pattern: "dried") + + assert_query([], :HerbariumRecord, :all, + pattern: "no herbarium record has this") + assert_query( + HerbariumRecord.where( + HerbariumRecord[:initial_det].matches("%Agaricus%") + ), + :HerbariumRecord, :all, pattern: "Agaricus" + ) + assert_query( + HerbariumRecord.where(HerbariumRecord[:notes].matches("%rare%")), + :HerbariumRecord, :all, pattern: "rare" + ) + assert_query( + HerbariumRecord.all, :HerbariumRecord, :all, pattern: "" + ) end def test_image_all @@ -3037,17 +3057,17 @@ def test_uses_join_hash def test_sequence_in_set list_set_ids = [sequences(:fasta_formatted_sequence).id, sequences(:bare_formatted_sequence).id] - assert_query(list_set_ids, :Sequence, :in_set, ids: list_set_ids) + assert_query(list_set_ids, :Sequence, :all, ids: list_set_ids) end def test_sequence_pattern_search - assert_query([], :Sequence, :pattern_search, pattern: "nonexistent") + assert_query([], :Sequence, :all, pattern: "nonexistent") assert_query(Sequence.where(Sequence[:locus].matches("ITS%")), - :Sequence, :pattern_search, pattern: "ITS") + :Sequence, :all, pattern: "ITS") assert_query([sequences(:alternate_archive)], - :Sequence, :pattern_search, pattern: "UNITE") + :Sequence, :all, pattern: "UNITE") assert_query([sequences(:deposited_sequence)], - :Sequence, :pattern_search, pattern: "deposited_sequence") + :Sequence, :all, pattern: "deposited_sequence") end def test_species_list_all @@ -3128,24 +3148,6 @@ def test_species_list_pattern_search :SpeciesList, :pattern_search, pattern: "") end - def test_herbarium_record_pattern_search - assert_query([], :HerbariumRecord, :pattern_search, - pattern: "no herbarium record has this") - assert_query( - HerbariumRecord.where( - HerbariumRecord[:initial_det].matches("%Agaricus%") - ), - :HerbariumRecord, :pattern_search, pattern: "Agaricus" - ) - assert_query( - HerbariumRecord.where(HerbariumRecord[:notes].matches("%rare%")), - :HerbariumRecord, :pattern_search, pattern: "rare" - ) - assert_query( - HerbariumRecord.all, :HerbariumRecord, :pattern_search, pattern: "" - ) - end - def test_user_all expect = User.order(:name).to_a assert_query(expect, :User, :all) From 3ee185071b1b9c3612dfdc5148363d7251bc6a69 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Fri, 4 Oct 2024 17:49:52 -0700 Subject: [PATCH 04/28] Rubocop --- app/classes/query/collection_number_base.rb | 2 ++ app/controllers/sequences_controller.rb | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/classes/query/collection_number_base.rb b/app/classes/query/collection_number_base.rb index 1b0bbda43d..fb42cd0877 100644 --- a/app/classes/query/collection_number_base.rb +++ b/app/classes/query/collection_number_base.rb @@ -20,6 +20,7 @@ def parameter_declarations ) end + # rubocop:disable Metrics/AbcSize def initialize_flavor add_owner_and_time_stamp_conditions("collection_numbers") add_for_observation_condition @@ -32,6 +33,7 @@ def initialize_flavor 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? diff --git a/app/controllers/sequences_controller.rb b/app/controllers/sequences_controller.rb index 0286bb4487..8863eca9fe 100644 --- a/app/controllers/sequences_controller.rb +++ b/app/controllers/sequences_controller.rb @@ -43,7 +43,9 @@ class SequencesController < ApplicationController # https://mushroomobserver.org/sequences?flavor=all # => displays a list of all sequences in MO # - # FIXME: this does not handle params[:pattern] or params[:ids] + # NOTE: #index does not handle params[:pattern] or params[:ids] because + # we don't offer sequence pattern search. However, the Query::SequenceBase + # class can handle a pattern param. def index case params[:flavor] when "all" From a7a0cbae4845c69e393d3c57f0a92bab4bb6fa39 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 01:59:39 -0700 Subject: [PATCH 05/28] Comments --- app/classes/query/comment_base.rb | 55 ++++++++++++++++++++- app/classes/query/comment_by_user.rb | 16 ------ app/classes/query/comment_for_target.rb | 26 ---------- app/classes/query/comment_for_user.rb | 17 ------- app/classes/query/comment_in_set.rb | 14 ------ app/classes/query/comment_pattern_search.rb | 21 -------- app/classes/query/modules/conditions.rb | 16 ++++++ app/classes/query/sequence_base.rb | 8 +-- app/controllers/comments_controller.rb | 10 ++-- test/models/query_test.rb | 13 +++-- 10 files changed, 82 insertions(+), 114 deletions(-) delete mode 100644 app/classes/query/comment_by_user.rb delete mode 100644 app/classes/query/comment_for_target.rb delete mode 100644 app/classes/query/comment_for_user.rb delete mode 100644 app/classes/query/comment_in_set.rb delete mode 100644 app/classes/query/comment_pattern_search.rb diff --git a/app/classes/query/comment_base.rb b/app/classes/query/comment_base.rb index aa27aaed39..083113da5e 100644 --- a/app/classes/query/comment_base.rb +++ b/app/classes/query/comment_base.rb @@ -11,15 +11,26 @@ def parameter_declarations super.merge( created_at?: [:time], updated_at?: [:time], + ids?: [Comment], + by_user?: User, + for_user?: User, users?: [User], types?: [{ string: Comment.all_type_tags }], summary_has?: :string, - content_has?: :string + content_has?: :string, + pattern?: :string, + target?: AbstractModel, + type?: :string ) end def initialize_flavor add_owner_and_time_stamp_conditions("comments") + add_by_user_condition("comments") + initialize_ids_parameter + add_for_user_condition + add_for_target_condition + add_pattern_parameter add_string_enum_condition("comments.target_type", params[:types], Comment.all_type_tags) add_search_condition("comments.summary", params[:summary_has]) @@ -27,6 +38,48 @@ def initialize_flavor super end + def add_for_user_condition + return if params[:for_user].blank? + + user = find_cached_parameter_instance(User, :for_user) + @title_tag = :query_title_for_user + @title_args[:user] = user.legal_name + add_join(:observations) + where << "observations.user_id = '#{params[:for_user]}'" + end + + def add_for_target_condition + return if params[:target].blank? || params[:type].blank? + + target = target_instance + @title_tag = :query_title_for_target + @title_args[:object] = target.unique_format_name + where << "comments.target_id = '#{target.id}'" + where << "comments.target_type = '#{target.class.name}'" + end + + def target_instance + unless (type = Comment.safe_model_from_name(params[:type])) + raise("The model #{params[:type].inspect} does not support comments!") + end + + find_cached_parameter_instance(type, :target) + end + + def add_pattern_parameter + return if params[:pattern].blank? + + @title_tag = :query_title_pattern_search + add_search_condition(search_fields, params[:pattern]) + end + + def search_fields + "CONCAT(" \ + "comments.summary," \ + "COALESCE(comments.comment,'')" \ + ")" + end + def self.default_order "created_at" end diff --git a/app/classes/query/comment_by_user.rb b/app/classes/query/comment_by_user.rb deleted file mode 100644 index 0441e2df7a..0000000000 --- a/app/classes/query/comment_by_user.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -class Query::CommentByUser < Query::CommentBase - def parameter_declarations - super.merge( - user: User - ) - end - - def initialize_flavor - user = find_cached_parameter_instance(User, :user) - title_args[:user] = user.legal_name - where << "comments.user_id = '#{user.id}'" - super - end -end diff --git a/app/classes/query/comment_for_target.rb b/app/classes/query/comment_for_target.rb deleted file mode 100644 index 2175025906..0000000000 --- a/app/classes/query/comment_for_target.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -class Query::CommentForTarget < Query::CommentBase - def parameter_declarations - super.merge( - target: AbstractModel, - type: :string - ) - end - - def initialize_flavor - target = target_instance - title_args[:object] = target.unique_format_name - where << "comments.target_id = '#{target.id}'" - where << "comments.target_type = '#{target.class.name}'" - super - end - - def target_instance - unless (type = Comment.safe_model_from_name(params[:type])) - raise("The model #{params[:type].inspect} does not support comments!") - end - - find_cached_parameter_instance(type, :target) - end -end diff --git a/app/classes/query/comment_for_user.rb b/app/classes/query/comment_for_user.rb deleted file mode 100644 index fde3c43c4d..0000000000 --- a/app/classes/query/comment_for_user.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class Query::CommentForUser < Query::CommentBase - def parameter_declarations - super.merge( - user: User - ) - end - - def initialize_flavor - user = find_cached_parameter_instance(User, :user) - title_args[:user] = user.legal_name - add_join(:observations) - where << "observations.user_id = '#{params[:user]}'" - super - end -end diff --git a/app/classes/query/comment_in_set.rb b/app/classes/query/comment_in_set.rb deleted file mode 100644 index 61ecf740dd..0000000000 --- a/app/classes/query/comment_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::CommentInSet < Query::CommentBase - def parameter_declarations - super.merge( - ids: [Comment] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/comment_pattern_search.rb b/app/classes/query/comment_pattern_search.rb deleted file mode 100644 index c507302d1a..0000000000 --- a/app/classes/query/comment_pattern_search.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -class Query::CommentPatternSearch < Query::CommentBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "comments.summary," \ - "COALESCE(comments.comment,'')" \ - ")" - end -end diff --git a/app/classes/query/modules/conditions.rb b/app/classes/query/modules/conditions.rb index 1dce83d007..eeb9cc6467 100644 --- a/app/classes/query/modules/conditions.rb +++ b/app/classes/query/modules/conditions.rb @@ -12,6 +12,15 @@ def add_owner_and_time_stamp_conditions(table) lookup_users_by_name(params[:users])) end + def add_by_user_condition(table) + return if params[:by_user].blank? + + user = find_cached_parameter_instance(User, :by_user) + @title_tag = :query_title_by_user + @title_args[:user] = user.legal_name + where << "#{table}.user_id = '#{user.id}'" + end + def add_boolean_condition(true_cond, false_cond, val, *) return if val.nil? @@ -70,6 +79,13 @@ def add_indexed_enum_condition(col, vals, allowed, *) add_joins(*) end + def initialize_ids_parameter + return if params[:ids].blank? + + initialize_in_set_flavor + end + + # move this above when all in_set flavors converted def initialize_in_set_flavor(table = model.table_name) set = clean_id_set(params[:ids]) @where << "#{table}.id IN (#{set})" diff --git a/app/classes/query/sequence_base.rb b/app/classes/query/sequence_base.rb index 651f2c2cfd..64c0a1716b 100644 --- a/app/classes/query/sequence_base.rb +++ b/app/classes/query/sequence_base.rb @@ -63,7 +63,7 @@ def initialize_flavor # some sequences because of this. add_owner_and_time_stamp_conditions("sequences") add_pattern_condition - add_in_set_condition + initialize_ids_parameter initialize_association_parameters initialize_name_parameters(:observations) initialize_observation_parameters @@ -93,12 +93,6 @@ def search_fields ")" end - def add_in_set_condition - return if params[:ids].blank? - - initialize_in_set_flavor - end - def initialize_association_parameters add_id_condition("sequences.observation_id", params[:observations]) initialize_observers_parameter diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 04049eb63b..38664ae763 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -66,7 +66,7 @@ def by_user ) return unless user - query = create_query(:Comment, :by_user, user: user) + query = create_query(:Comment, :all, by_user: user) show_selected_comments(query) end @@ -79,7 +79,7 @@ def for_user ) return unless user - query = create_query(:Comment, :for_user, user: user) + query = create_query(:Comment, :all, for_user: user) show_selected_comments(query) end @@ -89,8 +89,8 @@ def target return no_model unless (model = Comment.safe_model_from_name(params[:type])) return unless (target = find_or_goto_index(model, params[:target].to_s)) - query = create_query(:Comment, :for_target, target: target.id, - type: target.class.name) + query = create_query(:Comment, :all, target: target.id, + type: target.class.name) show_selected_comments(query) end @@ -105,7 +105,7 @@ def pattern if pattern.match?(/^\d+$/) && (comment = Comment.safe_find(pattern)) redirect_to(action: :show, id: comment.id) else - query = create_query(:Comment, :pattern_search, pattern: pattern) + query = create_query(:Comment, :all, pattern: pattern) show_selected_comments(query) end end diff --git a/test/models/query_test.rb b/test/models/query_test.rb index ae2188fcf6..f7080175d3 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1385,26 +1385,25 @@ def test_comment_all def test_comment_by_user expect = Comment.where(user_id: mary.id).reverse - assert_query(expect, :Comment, :by_user, user: mary) + assert_query(expect, :Comment, :all, by_user: mary) end def test_comment_for_target obs = observations(:minimal_unknown_obs) expect = Comment.where(target_id: obs.id) - assert_query(expect, :Comment, :for_target, target: obs, - type: "Observation") + assert_query(expect, :Comment, :all, target: obs, type: "Observation") end def test_comment_for_user expect = Comment.all.select { |c| c.target.user == mary } - assert_query(expect, :Comment, :for_user, user: mary) - assert_query([], :Comment, :for_user, user: rolf) + assert_query(expect, :Comment, :all, for_user: mary) + assert_query([], :Comment, :all, for_user: rolf) end def test_comment_in_set assert_query([comments(:detailed_unknown_obs_comment).id, comments(:minimal_unknown_obs_comment_1).id], - :Comment, :in_set, + :Comment, :all, ids: [comments(:detailed_unknown_obs_comment).id, comments(:minimal_unknown_obs_comment_1).id]) end @@ -1414,7 +1413,7 @@ def test_comment_pattern_search comments(:minimal_unknown_obs_comment_1), comments(:detailed_unknown_obs_comment) ] - assert_query(expect, :Comment, :pattern_search, pattern: "unknown") + assert_query(expect, :Comment, :all, pattern: "unknown") end def test_external_link_all From d58ab09d6224ce09f38131fd386a627a45d72f50 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 02:08:20 -0700 Subject: [PATCH 06/28] GlossaryTerm, refactor add_pattern_condition --- app/classes/query/collection_number_base.rb | 7 ------ app/classes/query/comment_base.rb | 9 +------- app/classes/query/glossary_term_base.rb | 11 ++++++++- .../query/glossary_term_pattern_search.rb | 23 ------------------- app/classes/query/herbarium_record_base.rb | 7 ------ app/classes/query/modules/conditions.rb | 7 ++++++ app/classes/query/sequence_base.rb | 7 ------ app/controllers/glossary_terms_controller.rb | 2 +- test/models/query_test.rb | 8 +++---- 9 files changed, 22 insertions(+), 59 deletions(-) delete mode 100644 app/classes/query/glossary_term_pattern_search.rb diff --git a/app/classes/query/collection_number_base.rb b/app/classes/query/collection_number_base.rb index fb42cd0877..969a637dfd 100644 --- a/app/classes/query/collection_number_base.rb +++ b/app/classes/query/collection_number_base.rb @@ -45,13 +45,6 @@ def add_for_observation_condition add_join(:observation_collection_numbers) end - def add_pattern_condition - return if params[:pattern].blank? - - @title_tag = :query_title_pattern_search - add_search_condition(search_fields, params[:pattern]) - end - def search_fields "CONCAT(" \ "collection_numbers.name," \ diff --git a/app/classes/query/comment_base.rb b/app/classes/query/comment_base.rb index 083113da5e..22fdaa9b53 100644 --- a/app/classes/query/comment_base.rb +++ b/app/classes/query/comment_base.rb @@ -30,7 +30,7 @@ def initialize_flavor initialize_ids_parameter add_for_user_condition add_for_target_condition - add_pattern_parameter + add_pattern_condition add_string_enum_condition("comments.target_type", params[:types], Comment.all_type_tags) add_search_condition("comments.summary", params[:summary_has]) @@ -66,13 +66,6 @@ def target_instance find_cached_parameter_instance(type, :target) end - def add_pattern_parameter - return if params[:pattern].blank? - - @title_tag = :query_title_pattern_search - add_search_condition(search_fields, params[:pattern]) - end - def search_fields "CONCAT(" \ "comments.summary," \ diff --git a/app/classes/query/glossary_term_base.rb b/app/classes/query/glossary_term_base.rb index 082485a225..c50ee5d01a 100644 --- a/app/classes/query/glossary_term_base.rb +++ b/app/classes/query/glossary_term_base.rb @@ -12,18 +12,27 @@ def parameter_declarations updated_at?: [:time], users?: [User], name_has?: :string, - description_has?: :string + description_has?: :string, + pattern?: :string ) end def initialize_flavor add_owner_and_time_stamp_conditions("glossary_terms") + add_pattern_condition add_search_condition("glossary_terms.name", params[:name_has]) add_search_condition("glossary_terms.description", params[:description_has]) super end + def search_fields + "CONCAT(" \ + "glossary_terms.name," \ + "COALESCE(glossary_terms.description,'')" \ + ")" + end + def self.default_order "name" end diff --git a/app/classes/query/glossary_term_pattern_search.rb b/app/classes/query/glossary_term_pattern_search.rb deleted file mode 100644 index 05b084c49d..0000000000 --- a/app/classes/query/glossary_term_pattern_search.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module Query - class GlossaryTermPatternSearch < Query::GlossaryTermBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "glossary_terms.name," \ - "COALESCE(glossary_terms.description,'')" \ - ")" - end - end -end diff --git a/app/classes/query/herbarium_record_base.rb b/app/classes/query/herbarium_record_base.rb index a5b8d80e7d..33c9311317 100644 --- a/app/classes/query/herbarium_record_base.rb +++ b/app/classes/query/herbarium_record_base.rb @@ -55,13 +55,6 @@ def add_in_herbarium_condition where << "herbarium_records.herbarium_id = '#{herbarium.id}'" end - def add_pattern_condition - return if params[:pattern].blank? - - @title_tag = :query_title_pattern_search - add_search_condition(search_fields, params[:pattern]) - end - def search_fields "CONCAT(" \ "herbarium_records.initial_det," \ diff --git a/app/classes/query/modules/conditions.rb b/app/classes/query/modules/conditions.rb index eeb9cc6467..7942bf506b 100644 --- a/app/classes/query/modules/conditions.rb +++ b/app/classes/query/modules/conditions.rb @@ -21,6 +21,13 @@ def add_by_user_condition(table) where << "#{table}.user_id = '#{user.id}'" end + def add_pattern_condition + return if params[:pattern].blank? + + @title_tag = :query_title_pattern_search + add_search_condition(search_fields, params[:pattern]) + end + def add_boolean_condition(true_cond, false_cond, val, *) return if val.nil? diff --git a/app/classes/query/sequence_base.rb b/app/classes/query/sequence_base.rb index 64c0a1716b..54e793f256 100644 --- a/app/classes/query/sequence_base.rb +++ b/app/classes/query/sequence_base.rb @@ -74,13 +74,6 @@ def initialize_flavor super end - def add_pattern_condition - return if params[:pattern].blank? - - @title_tag = :query_title_pattern_search - add_search_condition(search_fields, params[:pattern]) - end - def search_fields # I'm leaving out bases because it would be misleading. Some formats # allow spaces and other delimiting "garbage" which could break up diff --git a/app/controllers/glossary_terms_controller.rb b/app/controllers/glossary_terms_controller.rb index 428a31c142..f8ac900dd5 100644 --- a/app/controllers/glossary_terms_controller.rb +++ b/app/controllers/glossary_terms_controller.rb @@ -93,7 +93,7 @@ def patterned_index location: glossary_term_path(glossary_term.id)) and return else show_selected_glossary_terms( - create_query(:GlossaryTerm, :pattern_search, pattern: pattern) + create_query(:GlossaryTerm, :all, pattern: pattern) ) end end diff --git a/test/models/query_test.rb b/test/models/query_test.rb index f7080175d3..d896215cf2 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1449,7 +1449,7 @@ def test_glossary_term_pattern_search GlossaryTerm. where(GlossaryTerm[:name].matches("%conic_glossary_term%"). or(GlossaryTerm[:description].matches("%conic_glossary_term%"))), - :GlossaryTerm, :pattern_search, pattern: "conic_glossary_term" + :GlossaryTerm, :all, pattern: "conic_glossary_term" ) # description expect = @@ -1461,10 +1461,8 @@ def test_glossary_term_pattern_search where(GlossaryTerm[:description].matches("%of%")). where(GlossaryTerm[:description].matches("%Term%")) ) - assert_query(expect, - :GlossaryTerm, :pattern_search, pattern: "Description of Term") - assert_query(GlossaryTerm.all, - :GlossaryTerm, :pattern_search, pattern: "") + assert_query(expect, :GlossaryTerm, :all, pattern: "Description of Term") + assert_query(GlossaryTerm.all, :GlossaryTerm, :all, pattern: "") end def test_herbarium_all From cc2e23cb4f6f3a1d05166aa339a2af8b14a7bd9b Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 02:10:45 -0700 Subject: [PATCH 07/28] initialize_ids_parameter to add_ids_condition --- app/classes/query/comment_base.rb | 2 +- app/classes/query/modules/conditions.rb | 2 +- app/classes/query/sequence_base.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/classes/query/comment_base.rb b/app/classes/query/comment_base.rb index 22fdaa9b53..8685523d82 100644 --- a/app/classes/query/comment_base.rb +++ b/app/classes/query/comment_base.rb @@ -27,7 +27,7 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("comments") add_by_user_condition("comments") - initialize_ids_parameter + add_ids_condition add_for_user_condition add_for_target_condition add_pattern_condition diff --git a/app/classes/query/modules/conditions.rb b/app/classes/query/modules/conditions.rb index 7942bf506b..8c63bbdf36 100644 --- a/app/classes/query/modules/conditions.rb +++ b/app/classes/query/modules/conditions.rb @@ -86,7 +86,7 @@ def add_indexed_enum_condition(col, vals, allowed, *) add_joins(*) end - def initialize_ids_parameter + def add_ids_condition return if params[:ids].blank? initialize_in_set_flavor diff --git a/app/classes/query/sequence_base.rb b/app/classes/query/sequence_base.rb index 54e793f256..0981afb093 100644 --- a/app/classes/query/sequence_base.rb +++ b/app/classes/query/sequence_base.rb @@ -63,7 +63,7 @@ def initialize_flavor # some sequences because of this. add_owner_and_time_stamp_conditions("sequences") add_pattern_condition - initialize_ids_parameter + add_ids_condition initialize_association_parameters initialize_name_parameters(:observations) initialize_observation_parameters From b786e3e0e23ff270b7f1b5659facf6f03e26d802 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 02:20:13 -0700 Subject: [PATCH 08/28] article and herbaria --- app/classes/query/article_base.rb | 2 ++ app/classes/query/article_in_set.rb | 14 ----------- app/classes/query/herbarium_base.rb | 24 ++++++++++++++++++- app/classes/query/herbarium_in_set.rb | 14 ----------- app/classes/query/herbarium_nonpersonal.rb | 8 ------- app/classes/query/herbarium_pattern_search.rb | 23 ------------------ app/controllers/herbaria_controller.rb | 2 +- test/controllers/articles_controller_test.rb | 2 +- test/controllers/herbaria_controller_test.rb | 2 +- test/models/query_test.rb | 4 ++-- 10 files changed, 30 insertions(+), 65 deletions(-) delete mode 100644 app/classes/query/article_in_set.rb delete mode 100644 app/classes/query/herbarium_in_set.rb delete mode 100644 app/classes/query/herbarium_nonpersonal.rb delete mode 100644 app/classes/query/herbarium_pattern_search.rb diff --git a/app/classes/query/article_base.rb b/app/classes/query/article_base.rb index 285826d3b5..34b0e7e3e7 100644 --- a/app/classes/query/article_base.rb +++ b/app/classes/query/article_base.rb @@ -10,6 +10,7 @@ def parameter_declarations created_at?: [:time], updated_at?: [:time], users?: [User], + ids?: [Article], title_has?: :string, body_has?: :string ) @@ -17,6 +18,7 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("articles") + add_ids_condition add_search_condition("articles.title", params[:title_has]) add_search_condition("articles.body", params[:body_has]) super diff --git a/app/classes/query/article_in_set.rb b/app/classes/query/article_in_set.rb deleted file mode 100644 index 215cc50d2f..0000000000 --- a/app/classes/query/article_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::ArticleInSet < Query::ArticleBase - def parameter_declarations - super.merge( - ids: [Article] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/herbarium_base.rb b/app/classes/query/herbarium_base.rb index 0a5df5aed4..10eb05629a 100644 --- a/app/classes/query/herbarium_base.rb +++ b/app/classes/query/herbarium_base.rb @@ -11,8 +11,11 @@ def parameter_declarations updated_at?: [:time], code?: :string, name?: :string, + ids?: [Herbarium], description?: :string, - address?: :string + address?: :string, + pattern?: :string, + nonpersonal?: :boolean ) end @@ -23,9 +26,28 @@ def initialize_flavor add_search_condition("herbaria.name", params[:name]) add_search_condition("herbaria.description", params[:description]) add_search_condition("herbaria.mailing_address", params[:address]) + add_ids_condition + add_pattern_condition + add_nonpersonal_condition super end + def add_nonpersonal_condition + return if params[:nonpersonal].blank? + + @title_tag = :query_title_nonpersonal + where << "herbaria.personal_user_id IS NULL" + end + + def search_fields + "CONCAT(" \ + "herbaria.code," \ + "herbaria.name," \ + "COALESCE(herbaria.description,'')," \ + "COALESCE(herbaria.mailing_address,'')" \ + ")" + end + def self.default_order "name" end diff --git a/app/classes/query/herbarium_in_set.rb b/app/classes/query/herbarium_in_set.rb deleted file mode 100644 index 86798875b4..0000000000 --- a/app/classes/query/herbarium_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumInSet < Query::HerbariumBase - def parameter_declarations - super.merge( - ids: [Herbarium] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/herbarium_nonpersonal.rb b/app/classes/query/herbarium_nonpersonal.rb deleted file mode 100644 index bd6d9f82be..0000000000 --- a/app/classes/query/herbarium_nonpersonal.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumNonpersonal < Query::HerbariumBase - def initialize_flavor - where << "herbaria.personal_user_id IS NULL" - super - end -end diff --git a/app/classes/query/herbarium_pattern_search.rb b/app/classes/query/herbarium_pattern_search.rb deleted file mode 100644 index b4b2217637..0000000000 --- a/app/classes/query/herbarium_pattern_search.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -class Query::HerbariumPatternSearch < Query::HerbariumBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "herbaria.code," \ - "herbaria.name," \ - "COALESCE(herbaria.description,'')," \ - "COALESCE(herbaria.mailing_address,'')" \ - ")" - end -end diff --git a/app/controllers/herbaria_controller.rb b/app/controllers/herbaria_controller.rb index b45fee660b..13ddf92343 100644 --- a/app/controllers/herbaria_controller.rb +++ b/app/controllers/herbaria_controller.rb @@ -75,7 +75,7 @@ def index when "nonpersonal" # List institutional Herbaria store_location show_selected_herbaria( - create_query(:Herbarium, :nonpersonal, by: :code_then_name), + create_query(:Herbarium, :all, nonpersonal: true, by: :code_then_name), always_index: true ) else # default List herbaria resulting from query diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb index d3fcf6b685..1469eb51b2 100644 --- a/test/controllers/articles_controller_test.rb +++ b/test/controllers/articles_controller_test.rb @@ -19,7 +19,7 @@ def test_index def test_index_filtered article = Article.first - query = Query.lookup(:Article, :in_set, ids: [article.id]) + query = Query.lookup(:Article, :all, ids: [article.id]) params = @controller.query_params(query) get(:index, params: params) diff --git a/test/controllers/herbaria_controller_test.rb b/test/controllers/herbaria_controller_test.rb index 32f0a2a304..1d50eae8ac 100644 --- a/test/controllers/herbaria_controller_test.rb +++ b/test/controllers/herbaria_controller_test.rb @@ -108,7 +108,7 @@ def test_show_prev def test_index set = [nybg, herbaria(:rolf_herbarium)] - query = Query.lookup_and_save(:Herbarium, :in_set, by: :name, ids: set) + query = Query.lookup_and_save(:Herbarium, :all, by: :name, ids: set) login("zero") # Does not own any herbarium in set get(:index, params: { q: query.record.id.alphabetize }) diff --git a/test/models/query_test.rb b/test/models/query_test.rb index d896215cf2..a592126b1b 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1350,7 +1350,7 @@ def test_article_by_rss_log def test_article_in_set assert_query([articles(:premier_article).id], :Article, :in_set, ids: [articles(:premier_article).id]) - assert_query([], :Article, :in_set, ids: []) + assert_query([], :Article, :all, ids: []) end def test_collection_number_all @@ -1485,7 +1485,7 @@ def test_herbarium_in_set herbaria(:dick_herbarium), herbaria(:nybg_herbarium) ] - assert_query(expect, :Herbarium, :in_set, ids: expect) + assert_query(expect, :Herbarium, :all, ids: expect) end def test_herbarium_pattern_search From 4a6970857c51ccbdd6896d700d7c34dd69d1a97d Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 02:47:41 -0700 Subject: [PATCH 09/28] image pattern, fixes --- app/classes/query/image_base.rb | 28 +++++++++++++++++++++++++- app/controllers/herbaria_controller.rb | 2 +- app/controllers/images_controller.rb | 2 +- test/models/query_test.rb | 19 +++++++++-------- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/classes/query/image_base.rb b/app/classes/query/image_base.rb index ecfff764b1..f880d9ade6 100644 --- a/app/classes/query/image_base.rb +++ b/app/classes/query/image_base.rb @@ -14,7 +14,9 @@ def parameter_declarations created_at?: [:time], updated_at?: [:time], date?: [:date], + # by_user?: User, users?: [User], + # ids?: [Image], locations?: [:string], observations?: [Observation], projects?: [:string], @@ -29,7 +31,8 @@ def parameter_declarations with_votes?: :boolean, quality?: [:float], confidence?: [:float], - ok_for_export?: :boolean + ok_for_export?: :boolean, + pattern?: :string ).merge(names_parameter_declarations) end @@ -41,6 +44,9 @@ 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 initialize_association_parameters initialize_name_parameters(:observation_images, :observations) initialize_image_parameters @@ -91,6 +97,26 @@ def initialize_vote_parameters :observation_images, :observations) end + def add_pattern_condition + return if params[:pattern].blank? + + add_search_condition(search_fields, params[:pattern]) + add_join(:observation_images, :observations) + add_join(:observations, :locations!) + add_join(:observations, :names) + super + end + + def search_fields + "CONCAT(" \ + "names.search_name," \ + "COALESCE(images.original_name,'')," \ + "COALESCE(images.copyright_holder,'')," \ + "COALESCE(images.notes,'')," \ + "observations.where" \ + ")" + end + def self.default_order "created_at" end diff --git a/app/controllers/herbaria_controller.rb b/app/controllers/herbaria_controller.rb index 13ddf92343..399e7573a9 100644 --- a/app/controllers/herbaria_controller.rb +++ b/app/controllers/herbaria_controller.rb @@ -213,7 +213,7 @@ def patterned_index redirect_to(herbarium_path(herbarium.id)) else show_selected_herbaria( - create_query(:Herbarium, :pattern_search, pattern: pattern) + create_query(:Herbarium, :all, pattern: pattern) ) end end diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 3d9db51f94..91a4ff49a7 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -118,7 +118,7 @@ def pattern (image = Image.safe_find(pattern)) redirect_to(action: "show", id: image.id) else - query = create_query(:Image, :pattern_search, pattern: pattern) + query = create_query(:Image, :all, pattern: pattern) show_selected_images(query) end end diff --git a/test/models/query_test.rb b/test/models/query_test.rb index a592126b1b..facf08a5a9 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1349,7 +1349,7 @@ def test_article_by_rss_log def test_article_in_set assert_query([articles(:premier_article).id], :Article, - :in_set, ids: [articles(:premier_article).id]) + :all, ids: [articles(:premier_article).id]) assert_query([], :Article, :all, ids: []) end @@ -1442,8 +1442,7 @@ def test_glossary_term_all end def test_glossary_term_pattern_search - assert_query([], :GlossaryTerm, :pattern_search, - pattern: "no glossary term has this") + assert_query([], :GlossaryTerm, :all, pattern: "no glossary term has this") # name assert_query( GlossaryTerm. @@ -1490,7 +1489,7 @@ def test_herbarium_in_set def test_herbarium_pattern_search expect = [herbaria(:nybg_herbarium)] - assert_query(expect, :Herbarium, :pattern_search, pattern: "awesome") + assert_query(expect, :Herbarium, :all, pattern: "awesome") end def test_image_advanced_search @@ -1611,24 +1610,24 @@ def test_image_for_project def test_image_pattern_search assert_query([images(:agaricus_campestris_image).id], - :Image, :pattern_search, pattern: "agaricus") # name + :Image, :all, pattern: "agaricus") # name assert_query([images(:agaricus_campestris_image).id, images(:connected_coprinus_comatus_image).id, images(:turned_over_image).id, images(:in_situ_image).id], - :Image, :pattern_search, pattern: "bob dob") # copyright holder + :Image, :all, pattern: "bob dob") # copyright holder assert_query( [images(:in_situ_image).id], - :Image, :pattern_search, pattern: "looked gorilla OR original" # notes + :Image, :all, pattern: "looked gorilla OR original" # notes ) assert_query([images(:agaricus_campestris_image).id, images(:connected_coprinus_comatus_image).id], - :Image, :pattern_search, pattern: "notes some") # notes + :Image, :all, pattern: "notes some") # notes assert_query( [images(:turned_over_image).id, images(:in_situ_image).id], - :Image, :pattern_search, pattern: "dobbs -notes" # (c), not notes + :Image, :all, pattern: "dobbs -notes" # (c), not notes ) - assert_query([images(:in_situ_image).id], :Image, :pattern_search, + assert_query([images(:in_situ_image).id], :Image, :all, pattern: "DSCN8835") # original filename end From 295ed92ef0a35a912ece0b11235b5e819f49624a Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 02:52:49 -0700 Subject: [PATCH 10/28] articles in set --- app/classes/query/modules/conditions.rb | 4 ++-- test/models/query_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/classes/query/modules/conditions.rb b/app/classes/query/modules/conditions.rb index 8c63bbdf36..12ebba8394 100644 --- a/app/classes/query/modules/conditions.rb +++ b/app/classes/query/modules/conditions.rb @@ -86,10 +86,10 @@ def add_indexed_enum_condition(col, vals, allowed, *) add_joins(*) end - def add_ids_condition + def add_ids_condition(table = model.table_name) return if params[:ids].blank? - initialize_in_set_flavor + initialize_in_set_flavor(table) end # move this above when all in_set flavors converted diff --git a/test/models/query_test.rb b/test/models/query_test.rb index facf08a5a9..b3974b8b73 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1350,7 +1350,7 @@ def test_article_by_rss_log def test_article_in_set assert_query([articles(:premier_article).id], :Article, :all, ids: [articles(:premier_article).id]) - assert_query([], :Article, :all, ids: []) + # assert_query([], :Article, :all, ids: []) end def test_collection_number_all From bca7352c94ba9ac01a0903c09136b2c24af2f426 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 03:12:56 -0700 Subject: [PATCH 11/28] project, rss_log, user --- app/classes/query/project_base.rb | 14 +++++- app/classes/query/project_in_set.rb | 14 ------ app/classes/query/project_pattern_search.rb | 22 --------- app/classes/query/rss_log_base.rb | 3 +- app/classes/query/rss_log_in_set.rb | 14 ------ app/classes/query/species_list_base.rb | 2 + app/classes/query/species_list_in_set.rb | 14 ------ app/classes/query/user_base.rb | 22 ++++++++- app/classes/query/user_in_set.rb | 14 ------ app/classes/query/user_pattern_search.rb | 21 --------- app/classes/query/user_with_contribution.rb | 8 ---- app/controllers/contributors_controller.rb | 3 +- app/controllers/projects_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- test/models/query_test.rb | 52 ++++++++++----------- 15 files changed, 67 insertions(+), 140 deletions(-) delete mode 100644 app/classes/query/project_in_set.rb delete mode 100644 app/classes/query/project_pattern_search.rb delete mode 100644 app/classes/query/rss_log_in_set.rb delete mode 100644 app/classes/query/species_list_in_set.rb delete mode 100644 app/classes/query/user_in_set.rb delete mode 100644 app/classes/query/user_pattern_search.rb delete mode 100644 app/classes/query/user_with_contribution.rb diff --git a/app/classes/query/project_base.rb b/app/classes/query/project_base.rb index 06a13e3e10..e56de08785 100644 --- a/app/classes/query/project_base.rb +++ b/app/classes/query/project_base.rb @@ -10,6 +10,7 @@ def parameter_declarations created_at?: [:time], updated_at?: [:time], users?: [User], + ids?: [Project], with_images?: { boolean: [true] }, with_observations?: { boolean: [true] }, with_species_lists?: { boolean: [true] }, @@ -19,7 +20,8 @@ def parameter_declarations summary_has?: :string, field_slip_prefix_has?: :string, comments_has?: :string, - member?: User + member?: User, + pattern?: :string ) end @@ -28,6 +30,8 @@ def initialize_flavor initialize_association_parameters initialize_boolean_parameters initialize_search_parameters + add_ids_condition + add_pattern_condition super end @@ -71,6 +75,14 @@ def initialize_search_parameters ) end + def search_fields + "CONCAT(" \ + "projects.title," \ + "COALESCE(projects.summary,'')," \ + "COALESCE(projects.field_slip_prefix,'')" \ + ")" + end + def self.default_order "updated_at" end diff --git a/app/classes/query/project_in_set.rb b/app/classes/query/project_in_set.rb deleted file mode 100644 index 7240422f2b..0000000000 --- a/app/classes/query/project_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::ProjectInSet < Query::ProjectBase - def parameter_declarations - super.merge( - ids: [Project] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/project_pattern_search.rb b/app/classes/query/project_pattern_search.rb deleted file mode 100644 index b951128798..0000000000 --- a/app/classes/query/project_pattern_search.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -class Query::ProjectPatternSearch < Query::ProjectBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "projects.title," \ - "COALESCE(projects.summary,'')," \ - "COALESCE(projects.field_slip_prefix,'')" \ - ")" - end -end diff --git a/app/classes/query/rss_log_base.rb b/app/classes/query/rss_log_base.rb index 38ffaec642..a0b8c65278 100644 --- a/app/classes/query/rss_log_base.rb +++ b/app/classes/query/rss_log_base.rb @@ -10,7 +10,8 @@ def model def parameter_declarations super.merge( updated_at?: [:time], - type?: :string + type?: :string, + ids: [RssLog] ).merge(content_filter_parameter_declarations(Observation)). merge(content_filter_parameter_declarations(Location)) end diff --git a/app/classes/query/rss_log_in_set.rb b/app/classes/query/rss_log_in_set.rb deleted file mode 100644 index 154d805852..0000000000 --- a/app/classes/query/rss_log_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::RssLogInSet < Query::RssLogBase - def parameter_declarations - super.merge( - ids: [RssLog] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/species_list_base.rb b/app/classes/query/species_list_base.rb index 646151eb26..712f59d756 100644 --- a/app/classes/query/species_list_base.rb +++ b/app/classes/query/species_list_base.rb @@ -16,6 +16,7 @@ def parameter_declarations updated_at?: [:time], date?: [:date], users?: [User], + ids: [SpeciesList], locations?: [:string], projects?: [:string], title_has?: :string, @@ -29,6 +30,7 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("species_lists") add_date_condition("species_lists.when", params[:date]) + add_ids_condition initialize_name_parameters(:species_list_observations, :observations) initialize_association_parameters initialize_boolean_parameters diff --git a/app/classes/query/species_list_in_set.rb b/app/classes/query/species_list_in_set.rb deleted file mode 100644 index 5423a692ef..0000000000 --- a/app/classes/query/species_list_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::SpeciesListInSet < Query::SpeciesListBase - def parameter_declarations - super.merge( - ids: [SpeciesList] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/user_base.rb b/app/classes/query/user_base.rb index d35991385f..34be8da9cd 100644 --- a/app/classes/query/user_base.rb +++ b/app/classes/query/user_base.rb @@ -8,16 +8,36 @@ def model def parameter_declarations super.merge( created_at?: [:time], - updated_at?: [:time] + updated_at?: [:time], + ids?: [User], + pattern?: :string, + with_contribution?: :boolean ) end def initialize_flavor add_time_condition("users.created_at", params[:created_at]) add_time_condition("users.updated_at", params[:updated_at]) + add_ids_condition + add_pattern_condition + add_contribution_condition super end + def add_contribution_condition + return unless params[:with_contribution].to_s == "true" + + @title_tag = :query_title_with_contribution + where << "users.contribution > 0" + end + + def search_fields + "CONCAT(" \ + "users.login," \ + "users.name" \ + ")" + end + def self.default_order "name" end diff --git a/app/classes/query/user_in_set.rb b/app/classes/query/user_in_set.rb deleted file mode 100644 index baf68f3748..0000000000 --- a/app/classes/query/user_in_set.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class Query::UserInSet < Query::UserBase - def parameter_declarations - super.merge( - ids: [User] - ) - end - - def initialize_flavor - initialize_in_set_flavor - super - end -end diff --git a/app/classes/query/user_pattern_search.rb b/app/classes/query/user_pattern_search.rb deleted file mode 100644 index 8a9358b5ca..0000000000 --- a/app/classes/query/user_pattern_search.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -class Query::UserPatternSearch < Query::UserBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - super - end - - def search_fields - "CONCAT(" \ - "users.login," \ - "users.name" \ - ")" - end -end diff --git a/app/classes/query/user_with_contribution.rb b/app/classes/query/user_with_contribution.rb deleted file mode 100644 index bc7b1d8acd..0000000000 --- a/app/classes/query/user_with_contribution.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -class Query::UserWithContribution < Query::UserBase - def initialize_flavor - where << "users.contribution > 0" - super - end -end diff --git a/app/controllers/contributors_controller.rb b/app/controllers/contributors_controller.rb index 0a11a0ce2a..e22d2592df 100644 --- a/app/controllers/contributors_controller.rb +++ b/app/controllers/contributors_controller.rb @@ -6,7 +6,8 @@ class ContributorsController < ApplicationController # Contributors index def index - query = create_query(:User, :with_contribution, by: :contribution) + query = create_query(:User, :all, with_contribution: true, + by: :contribution) args = { action: :index, matrix: true, include: [:image] } show_index_of_objects(query, args) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 9cee799647..72d73e936b 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -206,7 +206,7 @@ def pattern set_ivars_for_show render("show", location: project_path(@project.id)) else - query = create_query(:Project, :pattern_search, pattern: pattern) + query = create_query(:Project, :all, pattern: pattern) show_selected_projects(query) end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3ffd011b17..eeb7c8a944 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -54,7 +54,7 @@ def user_search if (user = user_exact_match(pattern)) redirect_to(user_path(user.id)) else - query = create_query(:User, :pattern_search, pattern: pattern) + query = create_query(:User, :all, pattern: pattern) show_selected_users(query) end end diff --git a/test/models/query_test.rb b/test/models/query_test.rb index b3974b8b73..56d11c62cd 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -86,32 +86,31 @@ def test_validate_params Query.lookup(:Image, :by_user, user: rolf.id.to_s). params[:user]) - assert_raises(RuntimeError) { Query.lookup(:User, :in_set) } + assert_raises(RuntimeError) { Query.lookup(:User, :all) } # Oops, :in_set query is generic, # doesn't know to require Name instances here. # assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: rolf) } - assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: "one") } - assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: "1,2,3") } - assert_equal([], Query.lookup(:User, :in_set, ids: []).params[:ids]) - assert_equal([rolf.id], Query.lookup(:User, :in_set, + assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "one") } + assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "1,2,3") } + assert_equal([], Query.lookup(:User, :all, ids: []).params[:ids]) + assert_equal([rolf.id], Query.lookup(:User, :all, ids: rolf.id).params[:ids]) assert_equal([names(:fungi).id], - Query.lookup(:Name, :in_set, + Query.lookup(:Name, :all, ids: names(:fungi).id.to_s).params[:ids]) assert_equal([rolf.id, mary.id], - Query.lookup(:User, :in_set, + Query.lookup(:User, :all, ids: [rolf.id, mary.id]).params[:ids]) assert_equal([1, 2], - Query.lookup(:User, :in_set, ids: %w[1 2]).params[:ids]) + Query.lookup(:User, :all, ids: %w[1 2]).params[:ids]) assert_equal([rolf.id, mary.id], - Query.lookup(:User, :in_set, + Query.lookup(:User, :all, ids: [rolf.id.to_s, mary.id.to_s]).params[:ids]) - assert_equal([rolf.id], Query.lookup(:User, :in_set, - ids: rolf).params[:ids]) + assert_equal([rolf.id], Query.lookup(:User, :all, ids: rolf).params[:ids]) assert_equal([rolf.id, mary.id], - Query.lookup(:User, :in_set, ids: [rolf, mary]).params[:ids]) + Query.lookup(:User, :all, ids: [rolf, mary]).params[:ids]) assert_equal([rolf.id, mary.id, junk.id], - Query.lookup(:User, :in_set, + Query.lookup(:User, :all, ids: [rolf, mary.id, junk.id.to_s]).params[:ids]) assert_raises(RuntimeError) { Query.lookup(:Name, :pattern_search) } @@ -2966,22 +2965,22 @@ def test_project_by_rss_log def test_project_in_set assert_query([projects(:eol_project).id], :Project, :in_set, ids: [projects(:eol_project).id]) - assert_query([], :Project, :in_set, ids: []) + # assert_query([], :Project, :all, ids: []) end def test_project_pattern_search assert_query([], - :Project, :pattern_search, pattern: "no project has this") + :Project, :all, pattern: "no project has this") # title assert_query(Project.where(Project[:summary].matches("%bolete%"). or(Project[:title].matches("%bolete%"))), - :Project, :pattern_search, pattern: "bolete") + :Project, :all, pattern: "bolete") # summary assert_query(Project.where(Project[:summary].matches("%two lists%"). or(Project[:title].matches("%two lists%"))), - :Project, :pattern_search, pattern: "two lists") + :Project, :all, pattern: "two lists") assert_query(Project.all, - :Project, :pattern_search, pattern: "") + :Project, :all, pattern: "") end def test_rss_log_all @@ -2997,7 +2996,7 @@ def test_rss_log_type def test_rss_log_in_set rsslog_set_ids = [rss_logs(:species_list_rss_log).id, rss_logs(:name_rss_log).id] - assert_query(rsslog_set_ids, :RssLog, :in_set, ids: rsslog_set_ids) + assert_query(rsslog_set_ids, :RssLog, :all, ids: rsslog_set_ids) end def test_sequence_all @@ -3117,7 +3116,7 @@ def test_species_list_for_project def test_species_list_in_set list_set_ids = [species_lists(:first_species_list).id, species_lists(:unknown_species_list).id] - assert_query(list_set_ids, :SpeciesList, :in_set, ids: list_set_ids) + assert_query(list_set_ids, :SpeciesList, :all, ids: list_set_ids) end def test_species_list_pattern_search @@ -3160,19 +3159,19 @@ def test_user_in_set def test_user_pattern_search assert_query([], - :User, :pattern_search, pattern: "nonexistent pattern") + :User, :all, pattern: "nonexistent pattern") # in login assert_query(User.where(login: users(:spammer).login), - :User, :pattern_search, pattern: users(:spammer).login) + :User, :all, pattern: users(:spammer).login) # in name assert_query(User.where(name: users(:mary).name), - :User, :pattern_search, pattern: users(:mary).name) + :User, :all, pattern: users(:mary).name) assert_query(User.all, - :User, :pattern_search, pattern: "") + :User, :all, pattern: "") # sorted by location should include Users without location # (Differs from searches on other Classes or by other sort orders) assert_query(User.all, - :User, :pattern_search, pattern: "", by: "location") + :User, :all, pattern: "", by: "location") end ############################################################################## @@ -3245,8 +3244,7 @@ def test_filtering_content ############################################################################## def test_whiny_nil_in_map_locations - query = Query.lookup(:User, :in_set, - ids: [rolf.id, 1000, mary.id]) + query = Query.lookup(:User, :all, ids: [rolf.id, 1000, mary.id]) query.query assert_equal(2, query.results.length) end From 8527ccbd953d720a3f720d69fe0d98a2fd7fbf09 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 12:28:27 -0700 Subject: [PATCH 12/28] Fix tests --- app/classes/api2/comment_api.rb | 2 +- app/classes/query/modules/conditions.rb | 2 +- app/classes/query/rss_log_base.rb | 3 +- app/classes/query/species_list_base.rb | 2 +- app/controllers/comments_controller.rb | 2 +- app/controllers/lookups_controller.rb | 3 +- test/models/query_test.rb | 132 +++++++++++------------- 7 files changed, 68 insertions(+), 78 deletions(-) diff --git a/app/classes/api2/comment_api.rb b/app/classes/api2/comment_api.rb index ee0f60b7f6..aa7821308a 100644 --- a/app/classes/api2/comment_api.rb +++ b/app/classes/api2/comment_api.rb @@ -59,7 +59,7 @@ def update_params end def query_flavor - @target ? :for_target : :all + :all end def validate_create_params!(params) diff --git a/app/classes/query/modules/conditions.rb b/app/classes/query/modules/conditions.rb index 12ebba8394..09c68ce218 100644 --- a/app/classes/query/modules/conditions.rb +++ b/app/classes/query/modules/conditions.rb @@ -87,7 +87,7 @@ def add_indexed_enum_condition(col, vals, allowed, *) end def add_ids_condition(table = model.table_name) - return if params[:ids].blank? + return if params[:ids].nil? # [] is valid initialize_in_set_flavor(table) end diff --git a/app/classes/query/rss_log_base.rb b/app/classes/query/rss_log_base.rb index a0b8c65278..391598324f 100644 --- a/app/classes/query/rss_log_base.rb +++ b/app/classes/query/rss_log_base.rb @@ -11,7 +11,7 @@ def parameter_declarations super.merge( updated_at?: [:time], type?: :string, - ids: [RssLog] + ids?: [RssLog] ).merge(content_filter_parameter_declarations(Observation)). merge(content_filter_parameter_declarations(Location)) end @@ -19,6 +19,7 @@ def parameter_declarations def initialize_flavor add_time_condition("rss_logs.updated_at", params[:updated_at]) initialize_type_parameter + add_ids_condition initialize_content_filters_for_rss_log(Observation) initialize_content_filters_for_rss_log(Location) super diff --git a/app/classes/query/species_list_base.rb b/app/classes/query/species_list_base.rb index 712f59d756..10337499a1 100644 --- a/app/classes/query/species_list_base.rb +++ b/app/classes/query/species_list_base.rb @@ -16,7 +16,7 @@ def parameter_declarations updated_at?: [:time], date?: [:date], users?: [User], - ids: [SpeciesList], + ids?: [SpeciesList], locations?: [:string], projects?: [:string], title_has?: :string, diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 38664ae763..43f1edad69 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -126,7 +126,7 @@ def show_selected_comments(query, args = {}) args[:letters] = "users.login" end - @full_detail = (query.flavor == :for_target) + @full_detail = query.params[:for_target].present? show_index_of_objects(query, args) end diff --git a/app/controllers/lookups_controller.rb b/app/controllers/lookups_controller.rb index 2d036fe703..a03e7085a8 100644 --- a/app/controllers/lookups_controller.rb +++ b/app/controllers/lookups_controller.rb @@ -252,7 +252,8 @@ def handle_multiple_matches_or_suggestions( model:, id:, matches:, suggestions: ) obj = matches.first || suggestions.first - query = Query.lookup(model, :in_set, ids: matches + suggestions) + flavor = [Image, Location, Name].include?(model) ? :in_set : :all + query = Query.lookup(model, flavor, ids: matches + suggestions) if suggestions.any? flash_warning( :runtime_suggest_multiple_alternates.t(match: id, type: model.type_tag) diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 56d11c62cd..456e30308f 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -86,10 +86,10 @@ def test_validate_params Query.lookup(:Image, :by_user, user: rolf.id.to_s). params[:user]) - assert_raises(RuntimeError) { Query.lookup(:User, :all) } + # assert_raises(RuntimeError) { Query.lookup(:User, :all) } # Oops, :in_set query is generic, # doesn't know to require Name instances here. - # assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: rolf) } + # assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: rolf) } assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "one") } assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "1,2,3") } assert_equal([], Query.lookup(:User, :all, ids: []).params[:ids]) @@ -1349,7 +1349,7 @@ def test_article_by_rss_log def test_article_in_set assert_query([articles(:premier_article).id], :Article, :all, ids: [articles(:premier_article).id]) - # assert_query([], :Article, :all, ids: []) + assert_query([], :Article, :all, ids: []) end def test_collection_number_all @@ -1491,32 +1491,6 @@ def test_herbarium_pattern_search assert_query(expect, :Herbarium, :all, pattern: "awesome") end - def test_image_advanced_search - assert_query([images(:agaricus_campestris_image).id], - :Image, :advanced_search, name: "Agaricus") - assert_query(Image.joins(observations: :location). - where(observations: { location: locations(:burbank) }). - where(observations: { is_collection_location: true }), - :Image, :advanced_search, location: "burbank") - assert_query([images(:connected_coprinus_comatus_image).id], :Image, - :advanced_search, location: "glendale") - assert_query(Image.includes(:observations). - where(observations: { user: mary }), - :Image, :advanced_search, user: "mary") - assert_query([images(:turned_over_image).id, images(:in_situ_image).id], - :Image, :advanced_search, content: "little") - assert_query([images(:connected_coprinus_comatus_image).id], - :Image, :advanced_search, content: "fruiting") - assert_query([], - :Image, :advanced_search, - name: "agaricus", location: "glendale") - assert_query([images(:agaricus_campestris_image).id], :Image, - :advanced_search, name: "agaricus", location: "burbank") - assert_query([images(:turned_over_image).id, images(:in_situ_image).id], - :Image, :advanced_search, - content: "little", location: "burbank") - end - def test_herbarium_record_all expect = HerbariumRecord.all.sort_by(&:herbarium_label) assert_query(expect, :HerbariumRecord, :all) @@ -1555,6 +1529,32 @@ def test_herbarium_record_pattern_search ) end + def test_image_advanced_search + assert_query([images(:agaricus_campestris_image).id], + :Image, :advanced_search, name: "Agaricus") + assert_query(Image.joins(observations: :location). + where(observations: { location: locations(:burbank) }). + where(observations: { is_collection_location: true }), + :Image, :advanced_search, location: "burbank") + assert_query([images(:connected_coprinus_comatus_image).id], :Image, + :advanced_search, location: "glendale") + assert_query(Image.includes(:observations). + where(observations: { user: mary }), + :Image, :advanced_search, user: "mary") + assert_query([images(:turned_over_image).id, images(:in_situ_image).id], + :Image, :advanced_search, content: "little") + assert_query([images(:connected_coprinus_comatus_image).id], + :Image, :advanced_search, content: "fruiting") + assert_query([], + :Image, :advanced_search, + name: "agaricus", location: "glendale") + assert_query([images(:agaricus_campestris_image).id], :Image, + :advanced_search, name: "agaricus", location: "burbank") + assert_query([images(:turned_over_image).id, images(:in_situ_image).id], + :Image, :advanced_search, + content: "little", location: "burbank") + end + def test_image_all expect = Image.all.reverse assert_query(expect, :Image, :all) @@ -1809,12 +1809,10 @@ def test_image_with_observations_in_set assert_query([images(:agaricus_campestris_image).id, images(:turned_over_image).id, images(:in_situ_image).id], - :Image, - :with_observations_in_set, + :Image, :with_observations_in_set, ids: [observations(:detailed_unknown_obs).id, observations(:agaricus_campestris_obs).id]) - assert_query([], :Image, - :with_observations_in_set, + assert_query([], :Image, :with_observations_in_set, ids: [observations(:minimal_unknown_obs).id]) end @@ -1857,8 +1855,8 @@ def test_image_with_observations_of_name assert_query([images(:connected_coprinus_comatus_image).id], :Image, :with_observations, names: [names(:coprinus_comatus).id]) - assert_query([images(:agaricus_campestris_image).id], :Image, - :with_observations, + assert_query([images(:agaricus_campestris_image).id], + :Image, :with_observations, names: [names(:agaricus_campestris).id]) assert_query([], :Image, :with_observations, names: [names(:conocybe_filaris).id]) @@ -2213,14 +2211,13 @@ def test_location_with_observations_in_set assert_query([locations(:burbank).id], :Location, :with_observations_in_set, ids: [observations(:minimal_unknown_obs).id]) - assert_query([], :Location, - :with_observations_in_set, + assert_query([], :Location, :with_observations_in_set, ids: [observations(:coprinus_comatus_obs).id]) end def test_location_with_observations_in_species_list - assert_query([locations(:burbank).id], :Location, - :with_observations_in_species_list, + assert_query([locations(:burbank).id], + :Location, :with_observations_in_species_list, species_list: species_lists(:unknown_species_list).id) assert_query([], :Location, :with_observations_in_species_list, species_list: species_lists(:first_species_list).id) @@ -2436,27 +2433,22 @@ def test_name_with_descriptions def test_name_with_descriptions_by_user assert_query([names(:agaricus_campestris).id, names(:peltigera).id], - :Name, - :with_descriptions_by_user, user: mary, by: :id) + :Name, :with_descriptions_by_user, user: mary, by: :id) assert_query([names(:boletus_edulis).id, names(:peltigera).id, names(:suillus).id], - :Name, - :with_descriptions_by_user, user: dick, by: :id) + :Name, :with_descriptions_by_user, user: dick, by: :id) end def test_name_with_descriptions_by_author assert_query([names(:coprinus_comatus).id, names(:peltigera).id], - :Name, - :with_descriptions_by_author, user: rolf, by: :id) + :Name, :with_descriptions_by_author, user: rolf, by: :id) assert_query([names(:agaricus_campestris).id, names(:peltigera).id], - :Name, - :with_descriptions_by_author, user: mary, by: :id) + :Name, :with_descriptions_by_author, user: mary, by: :id) assert_query([names(:boletus_edulis).id], - :Name, - :with_descriptions_by_author, user: dick, by: :id) + :Name, :with_descriptions_by_author, user: dick, by: :id) end def test_name_with_descriptions_by_editor @@ -2617,8 +2609,8 @@ def test_name_with_observations_at_location end def test_name_with_observations_at_where - assert_query([names(:coprinus_comatus).id], :Name, - :with_observations_at_where, + assert_query([names(:coprinus_comatus).id], + :Name, :with_observations_at_where, user_where: "glendale", location: "glendale") end @@ -2646,8 +2638,7 @@ def test_name_with_observations_in_set assert_query([names(:agaricus_campestras).id, names(:agaricus_campestris).id, names(:fungi).id], - :Name, - :with_observations_in_set, + :Name, :with_observations_in_set, ids: [observations(:detailed_unknown_obs).id, observations(:agaricus_campestris_obs).id, observations(:agaricus_campestras_obs).id]) @@ -2657,8 +2648,7 @@ def test_name_with_observations_in_species_list assert_query([names(:fungi).id], :Name, :with_observations_in_species_list, species_list: species_lists(:unknown_species_list).id) - assert_query([], :Name, - :with_observations_in_species_list, + assert_query([], :Name, :with_observations_in_species_list, species_list: species_lists(:first_species_list).id) end @@ -2719,19 +2709,19 @@ def test_name_description_in_set end def test_observation_advanced_search - assert_query([observations(:strobilurus_diminutivus_obs).id], :Observation, - :advanced_search, name: "diminutivus") - assert_query([observations(:coprinus_comatus_obs).id], :Observation, - :advanced_search, location: "glendale") # where + assert_query([observations(:strobilurus_diminutivus_obs).id], + :Observation, :advanced_search, name: "diminutivus") + assert_query([observations(:coprinus_comatus_obs).id], + :Observation, :advanced_search, location: "glendale") # where expect = Observation.where(location_id: locations(:burbank)).to_a - assert_query(expect, :Observation, - :advanced_search, location: "burbank", by: :id) # location + assert_query(expect, :Observation, :advanced_search, + location: "burbank", by: :id) # location expect = Observation.where(user_id: rolf.id).to_a assert_query(expect, :Observation, :advanced_search, user: "rolf", by: :id) - assert_query([observations(:coprinus_comatus_obs).id], :Observation, - :advanced_search, content: "second fruiting") # notes - assert_query([observations(:minimal_unknown_obs).id], :Observation, - :advanced_search, content: "agaricus") # comment + assert_query([observations(:coprinus_comatus_obs).id], # notes + :Observation,:advanced_search, content: "second fruiting") + assert_query([observations(:minimal_unknown_obs).id], + :Observation, :advanced_search, content: "agaricus") # comment end def test_observation_all @@ -2963,9 +2953,9 @@ def test_project_by_rss_log end def test_project_in_set - assert_query([projects(:eol_project).id], :Project, - :in_set, ids: [projects(:eol_project).id]) - # assert_query([], :Project, :all, ids: []) + assert_query([projects(:eol_project).id], :Project, :all, + ids: [projects(:eol_project).id]) + assert_query([], :Project, :all, ids: []) end def test_project_pattern_search @@ -3151,10 +3141,8 @@ def test_user_all end def test_user_in_set - assert_query([rolf.id, mary.id, junk.id], - :User, :in_set, - ids: [junk.id, mary.id, rolf.id], - by: :reverse_name) + assert_query([rolf.id, mary.id, junk.id], :User, :all, + ids: [junk.id, mary.id, rolf.id], by: :reverse_name) end def test_user_pattern_search From dd4c6f510dfe448dc39adaa43a4f69026927cb34 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 12:33:11 -0700 Subject: [PATCH 13/28] Update lookups_controller.rb --- app/controllers/lookups_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/lookups_controller.rb b/app/controllers/lookups_controller.rb index a03e7085a8..cb806df1a1 100644 --- a/app/controllers/lookups_controller.rb +++ b/app/controllers/lookups_controller.rb @@ -252,7 +252,9 @@ def handle_multiple_matches_or_suggestions( model:, id:, matches:, suggestions: ) obj = matches.first || suggestions.first - flavor = [Image, Location, Name].include?(model) ? :in_set : :all + unconverted = [Image, Location, Name, Observation] + flavor = unconverted.include?(model) ? :in_set : :all + query = Query.lookup(model, flavor, ids: matches + suggestions) if suggestions.any? flash_warning( From 7ccf62ca7ed404871dd6bc84ed5bce4897c35cab Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 13:44:52 -0700 Subject: [PATCH 14/28] pattern search, name --- app/classes/query/image_pattern_search.rb | 27 ---------------- app/classes/query/location_base.rb | 32 ++++++++++++++++++- app/classes/query/location_pattern_search.rb | 24 -------------- app/classes/query/location_regexp_search.rb | 15 --------- app/classes/query/observation_base.rb | 2 ++ app/classes/query/species_list_base.rb | 32 ++++++++++++++++++- app/classes/query/species_list_by_user.rb | 16 ---------- app/classes/query/species_list_for_project.rb | 17 ---------- .../query/species_list_pattern_search.rb | 23 ------------- app/controllers/locations_controller.rb | 5 ++- app/controllers/species_lists_controller.rb | 6 ++-- 11 files changed, 69 insertions(+), 130 deletions(-) delete mode 100644 app/classes/query/image_pattern_search.rb delete mode 100644 app/classes/query/location_pattern_search.rb delete mode 100644 app/classes/query/location_regexp_search.rb delete mode 100644 app/classes/query/species_list_by_user.rb delete mode 100644 app/classes/query/species_list_for_project.rb delete mode 100644 app/classes/query/species_list_pattern_search.rb diff --git a/app/classes/query/image_pattern_search.rb b/app/classes/query/image_pattern_search.rb deleted file mode 100644 index 29eb3d0b27..0000000000 --- a/app/classes/query/image_pattern_search.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -class Query::ImagePatternSearch < Query::ImageBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - add_join(:observation_images, :observations) - add_join(:observations, :locations!) - add_join(:observations, :names) - super - end - - def search_fields - "CONCAT(" \ - "names.search_name," \ - "COALESCE(images.original_name,'')," \ - "COALESCE(images.copyright_holder,'')," \ - "COALESCE(images.notes,'')," \ - "observations.where" \ - ")" - end -end diff --git a/app/classes/query/location_base.rb b/app/classes/query/location_base.rb index 34ce9dfd85..5211c78ed9 100644 --- a/app/classes/query/location_base.rb +++ b/app/classes/query/location_base.rb @@ -15,7 +15,9 @@ def parameter_declarations north?: :float, south?: :float, east?: :float, - west?: :float + west?: :float, + pattern?: :string, + regexp?: :string ).merge(content_filter_parameter_declarations(Location)) end @@ -24,10 +26,38 @@ def initialize_flavor add_owner_and_time_stamp_conditions("locations") end add_bounding_box_conditions_for_locations + add_pattern_condition + add_regexp_condition initialize_content_filters(Location) super end + # adds a join + def add_pattern_condition + return if params[:pattern].blank? + + add_search_condition(search_fields, params[:pattern]) + add_join(:"location_descriptions.default!") + super + end + + def add_regexp_condition + return if params[:regexp].blank? + + @title_tag = :query_title_regexp_search + regexp = escape(params[:regexp].to_s.strip_squeeze) + where << "locations.name REGEXP #{regexp}" + end + + def search_fields + "CONCAT(" \ + "locations.name," \ + "#{LocationDescription.all_note_fields.map do |x| + "COALESCE(location_descriptions.#{x},'')" + end.join(",")}" \ + ")" + end + def self.default_order "name" end diff --git a/app/classes/query/location_pattern_search.rb b/app/classes/query/location_pattern_search.rb deleted file mode 100644 index 1a90c1543a..0000000000 --- a/app/classes/query/location_pattern_search.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -class Query::LocationPatternSearch < Query::LocationBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - add_join(:"location_descriptions.default!") - super - end - - def search_fields - "CONCAT(" \ - "locations.name," \ - "#{LocationDescription.all_note_fields.map do |x| - "COALESCE(location_descriptions.#{x},'')" - end.join(",")}" \ - ")" - end -end diff --git a/app/classes/query/location_regexp_search.rb b/app/classes/query/location_regexp_search.rb deleted file mode 100644 index 6b6c7af0de..0000000000 --- a/app/classes/query/location_regexp_search.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class Query::LocationRegexpSearch < Query::LocationBase - def parameter_declarations - super.merge( - regexp: :string - ) - end - - def initialize_flavor - regexp = escape(params[:regexp].to_s.strip_squeeze) - where << "locations.name REGEXP #{regexp}" - super - end -end diff --git a/app/classes/query/observation_base.rb b/app/classes/query/observation_base.rb index 6dcf5598cd..cd7c5d5a4a 100644 --- a/app/classes/query/observation_base.rb +++ b/app/classes/query/observation_base.rb @@ -28,6 +28,7 @@ def parameter_declarations species_lists?: [:string], users?: [User], field_slips?: [:string], + # pattern?: :string, # numeric confidence?: [:float], @@ -51,6 +52,7 @@ def parameter_declarations def initialize_flavor add_owner_and_time_stamp_conditions("observations") add_date_condition("observations.when", params[:date]) + # add_pattern_condition initialize_name_parameters initialize_association_parameters initialize_boolean_parameters diff --git a/app/classes/query/species_list_base.rb b/app/classes/query/species_list_base.rb index 10337499a1..e0d5647da7 100644 --- a/app/classes/query/species_list_base.rb +++ b/app/classes/query/species_list_base.rb @@ -23,14 +23,20 @@ def parameter_declarations with_notes?: :boolean, notes_has?: :string, with_comments?: { boolean: [true] }, - comments_has?: :string + comments_has?: :string, + pattern?: :string, + project?: Project, + by_user?: User ).merge(names_parameter_declarations) end def initialize_flavor add_owner_and_time_stamp_conditions("species_lists") add_date_condition("species_lists.when", params[:date]) + add_pattern_condition add_ids_condition + add_by_user_condition("species_lists") + add_for_project_condition initialize_name_parameters(:species_list_observations, :observations) initialize_association_parameters initialize_boolean_parameters @@ -38,6 +44,22 @@ def initialize_flavor super end + def add_pattern_condition + add_search_condition(search_fields, params[:pattern]) + add_join(:locations!) + 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 << "project_species_lists.project_id = '#{params[:project]}'" + add_join("project_species_lists") + end + def initialize_association_parameters add_where_condition("species_lists", params[:locations]) add_id_condition( @@ -72,6 +94,14 @@ def initialize_search_parameters ) end + def search_fields + "CONCAT(" \ + "species_lists.title," \ + "COALESCE(species_lists.notes,'')," \ + "IF(locations.id,locations.name,species_lists.where)" \ + ")" + end + def self.default_order "title" end diff --git a/app/classes/query/species_list_by_user.rb b/app/classes/query/species_list_by_user.rb deleted file mode 100644 index 40229c8c1d..0000000000 --- a/app/classes/query/species_list_by_user.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -class Query::SpeciesListByUser < Query::SpeciesListBase - def parameter_declarations - super.merge( - user: User - ) - end - - def initialize_flavor - user = find_cached_parameter_instance(User, :user) - title_args[:user] = user.legal_name - where << "species_lists.user_id = '#{user.id}'" - super - end -end diff --git a/app/classes/query/species_list_for_project.rb b/app/classes/query/species_list_for_project.rb deleted file mode 100644 index 4c930585f2..0000000000 --- a/app/classes/query/species_list_for_project.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class Query::SpeciesListForProject < Query::SpeciesListBase - def parameter_declarations - super.merge( - project: Project - ) - end - - def initialize_flavor - project = find_cached_parameter_instance(Project, :project) - title_args[:project] = project.title - where << "project_species_lists.project_id = '#{params[:project]}'" - add_join("project_species_lists") - super - end -end diff --git a/app/classes/query/species_list_pattern_search.rb b/app/classes/query/species_list_pattern_search.rb deleted file mode 100644 index 7342f5a752..0000000000 --- a/app/classes/query/species_list_pattern_search.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -class Query::SpeciesListPatternSearch < Query::SpeciesListBase - def parameter_declarations - super.merge( - pattern: :string - ) - end - - def initialize_flavor - add_search_condition(search_fields, params[:pattern]) - add_join(:locations!) - super - end - - def search_fields - "CONCAT(" \ - "species_lists.title," \ - "COALESCE(species_lists.notes,'')," \ - "IF(locations.id,locations.name,species_lists.where)" \ - ")" - end -end diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 4cd2f03ede..78461fb35f 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -89,8 +89,7 @@ def pattern redirect_to(location_path(loc.id)) else query = create_query( - :Location, :pattern_search, - pattern: Location.user_format(@user, pattern) + :Location, :all, pattern: Location.user_format(@user, pattern) ) show_selected_locations(query, link_all_sorts: true) end @@ -99,7 +98,7 @@ def pattern # Displays a list of all locations whose country matches the id param. def country query = create_query( - :Location, :regexp_search, regexp: "#{params[:country]}$" + :Location, :all, regexp: "#{params[:country]}$" ) show_selected_locations(query, link_all_sorts: true) end diff --git a/app/controllers/species_lists_controller.rb b/app/controllers/species_lists_controller.rb index 4cfc0cc089..a5d8e1a615 100644 --- a/app/controllers/species_lists_controller.rb +++ b/app/controllers/species_lists_controller.rb @@ -162,7 +162,7 @@ def by_user ) return unless user - query = create_query(:SpeciesList, :by_user, user: user) + query = create_query(:SpeciesList, :all, by_user: user) show_selected_species_lists(query) end @@ -171,7 +171,7 @@ def for_project project = find_or_goto_index(Project, params[:for_project].to_s) return unless project - query = create_query(:SpeciesList, :for_project, project: project) + query = create_query(:SpeciesList, :all, project: project) show_selected_species_lists(query, always_index: 1) end @@ -183,7 +183,7 @@ def pattern if spl redirect_to(action: :show, id: spl.id) else - query = create_query(:SpeciesList, :pattern_search, pattern: pattern) + query = create_query(:SpeciesList, :all, pattern: pattern) show_selected_species_lists(query) end end From 7dbdc98548025a532c21563df9498da8b3dc3e4f Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 13:48:46 -0700 Subject: [PATCH 15/28] try to remove flavor --- app/classes/query.rb | 4 ++-- app/classes/query/rss_log_all.rb | 2 +- app/helpers/tabs/locations_helper.rb | 2 +- app/helpers/tabs/names_helper.rb | 2 +- app/helpers/tabs/species_lists_helper.rb | 2 +- app/models/rss_log.rb | 7 ------- test/models/query_test.rb | 20 ++++++++++---------- 7 files changed, 16 insertions(+), 23 deletions(-) diff --git a/app/classes/query.rb b/app/classes/query.rb index 0d2b9c0d33..3a874f7d92 100644 --- a/app/classes/query.rb +++ b/app/classes/query.rb @@ -17,8 +17,8 @@ # Each model has a default search flavor (:default), which is used by the prev # and next actions when the specified query no longer exists. For example, if # you click on an observation from the main index, prev and next travserse the -# results of an :Observation :by_rss_log query. If the user comes back a day -# later, this query will have been culled by the garbage collector (see +# results of an :Observation :all by: :rss_log query. If the user comes back +# a day later, this query will have been culled by the garbage collector (see # below), so prev and next need to be able to create a default query on the # fly. In this case it may be :Observation :all (see default_flavors array # below). diff --git a/app/classes/query/rss_log_all.rb b/app/classes/query/rss_log_all.rb index 5a47e79775..5ad3c7a61f 100644 --- a/app/classes/query/rss_log_all.rb +++ b/app/classes/query/rss_log_all.rb @@ -31,7 +31,7 @@ def coerce_into_species_list_query end def do_coerce(new_model) - Query.lookup(new_model, :by_rss_log, params_minus_type) + Query.lookup(new_model, :all, { by: :rss_log }.merge(params_minus_type)) end def params_minus_type diff --git a/app/helpers/tabs/locations_helper.rb b/app/helpers/tabs/locations_helper.rb index 76a8916419..b0d6ca0bfe 100644 --- a/app/helpers/tabs/locations_helper.rb +++ b/app/helpers/tabs/locations_helper.rb @@ -138,7 +138,7 @@ def location_index_sorts(query:) [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query&.flavor == :by_rss_log ? "rss_log" : "updated_at"), + [(query.params[:by] == :rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t], ["box_area", :sort_by_box_area.t] diff --git a/app/helpers/tabs/names_helper.rb b/app/helpers/tabs/names_helper.rb index 887787af9b..e3e5bcecae 100644 --- a/app/helpers/tabs/names_helper.rb +++ b/app/helpers/tabs/names_helper.rb @@ -277,7 +277,7 @@ def names_index_sorts(query:) [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query&.flavor == :by_rss_log ? "rss_log" : "updated_at"), + [(query.params[:by] == :rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t] ] diff --git a/app/helpers/tabs/species_lists_helper.rb b/app/helpers/tabs/species_lists_helper.rb index 4f32f35190..874afb988a 100644 --- a/app/helpers/tabs/species_lists_helper.rb +++ b/app/helpers/tabs/species_lists_helper.rb @@ -136,7 +136,7 @@ def species_lists_index_sorts(query:) ["date", :sort_by_date.t], ["user", :sort_by_user.t], ["created_at", :sort_by_created_at.t], - [(query&.flavor == :by_rss_log ? "rss_log" : "updated_at"), + [(query.params[:by] == :rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t] ] end diff --git a/app/models/rss_log.rb b/app/models/rss_log.rb index d58fbac3c4..3d4885ac23 100644 --- a/app/models/rss_log.rb +++ b/app/models/rss_log.rb @@ -59,13 +59,6 @@ # # <%= show_object_footer(@object) %> # -# 6) Add +by_rss_log+ flavor to Query for your model: -# -# self.allowed_model_flavors = { -# :Model => [ -# :by_rss_log, # Models with RSS logs, in RSS order. -# ] -# } # # == Usage # diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 456e30308f..2e5b703bd1 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1315,10 +1315,10 @@ def test_rss_log_coercion assert_equal("Observation", q4.model.to_s) assert_equal("SpeciesList", q5.model.to_s) - assert_equal(:by_rss_log, q2.flavor) - assert_equal(:by_rss_log, q3.flavor) - assert_equal(:by_rss_log, q4.flavor) - assert_equal(:by_rss_log, q5.flavor) + assert_equal(:rss_log, q2.params[:by]) + assert_equal(:rss_log, q3.params[:by]) + assert_equal(:rss_log, q4.params[:by]) + assert_equal(:rss_log, q5.params[:by]) assert_equal({}, q2.params) assert_equal({}, q3.params) @@ -1343,7 +1343,7 @@ def test_article_all def test_article_by_rss_log assert_query(Article.joins(:rss_log).distinct, - :Article, :by_rss_log) + :Article, :all, by: :rss_log) end def test_article_in_set @@ -1930,7 +1930,7 @@ def test_location_by_editor def test_location_by_rss_log assert_query(Location.joins(:rss_log).distinct, - :Location, :by_rss_log) + :Location, :all, by: :rss_log) end def test_location_in_set @@ -2371,7 +2371,7 @@ def test_name_by_editor def test_name_by_rss_log assert_query(Name.joins(:rss_log).order("rss_logs.updated_at"), - :Name, :by_rss_log) + :Name, :all, by: :rss_log) end def test_name_in_set @@ -2749,7 +2749,7 @@ def test_observation_at_where def test_observation_by_rss_log expect = Observation.where.not(rss_log: nil) - assert_query(expect, :Observation, :by_rss_log) + assert_query(expect, :Observation, :all, by: :rss_log) end def test_observation_by_user @@ -2949,7 +2949,7 @@ def test_project_all def test_project_by_rss_log assert_query(Project.joins(:rss_log).distinct, - :Project, :by_rss_log) + :Project, :all, by: :rss_log) end def test_project_in_set @@ -3080,7 +3080,7 @@ def test_species_list_at_where def test_species_list_by_rss_log assert_query([species_lists(:first_species_list).id], - :SpeciesList, :by_rss_log) + :SpeciesList, :all, by: :rss_log) end def test_species_list_by_user From aa02e8a4bb9868a0532b3da4cfdfcfb500ddc268 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 14:48:25 -0700 Subject: [PATCH 16/28] Try to fix location pattern regexp --- app/classes/query/location_base.rb | 1 - app/controllers/locations_controller.rb | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/classes/query/location_base.rb b/app/classes/query/location_base.rb index 5211c78ed9..09599399e0 100644 --- a/app/classes/query/location_base.rb +++ b/app/classes/query/location_base.rb @@ -36,7 +36,6 @@ def initialize_flavor def add_pattern_condition return if params[:pattern].blank? - add_search_condition(search_fields, params[:pattern]) add_join(:"location_descriptions.default!") super end diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 78461fb35f..ae1c6a42de 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -182,7 +182,9 @@ def get_matching_undefined_locations(query, args) def coerce_query_for_undefined_locations(query) model = :Observation flavor = query.flavor - args = query.params.dup + # params used by :Location :all, but not yet :Observation :all + exceptions = [:regexp, :pattern] + args = query.params.dup.except(*exceptions) result = nil # Select only observations with undefined location. From 8ee53a9cdfb423a54a8f54c7629bd64d750d4cc6 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 14:59:00 -0700 Subject: [PATCH 17/28] fix species list --- test/models/query_test.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 456e30308f..900237fa8c 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -2719,7 +2719,7 @@ def test_observation_advanced_search expect = Observation.where(user_id: rolf.id).to_a assert_query(expect, :Observation, :advanced_search, user: "rolf", by: :id) assert_query([observations(:coprinus_comatus_obs).id], # notes - :Observation,:advanced_search, content: "second fruiting") + :Observation, :advanced_search, content: "second fruiting") assert_query([observations(:minimal_unknown_obs).id], :Observation, :advanced_search, content: "agaricus") # comment end @@ -3086,20 +3086,20 @@ def test_species_list_by_rss_log def test_species_list_by_user assert_query([species_lists(:first_species_list).id, species_lists(:another_species_list).id], - :SpeciesList, :by_user, user: rolf, by: :id) + :SpeciesList, :all, user: rolf, by: :id) assert_query(SpeciesList.where(user: mary), - :SpeciesList, :by_user, user: mary) - assert_query([], :SpeciesList, :by_user, user: dick) + :SpeciesList, :all, user: mary) + assert_query([], :SpeciesList, :all, user: dick) end def test_species_list_for_project assert_query([], - :SpeciesList, :for_project, project: projects(:empty_project)) + :SpeciesList, :all, project: projects(:empty_project)) assert_query(projects(:bolete_project).species_lists, - :SpeciesList, :for_project, project: projects(:bolete_project)) + :SpeciesList, :all, project: projects(:bolete_project)) assert_query( projects(:two_list_project).species_lists, - :SpeciesList, :for_project, project: projects(:two_list_project) + :SpeciesList, :all, project: projects(:two_list_project) ) end @@ -3111,26 +3111,26 @@ def test_species_list_in_set def test_species_list_pattern_search assert_query([], - :SpeciesList, :pattern_search, pattern: "nonexistent pattern") + :SpeciesList, :all, pattern: "nonexistent pattern") # in title assert_query(SpeciesList.where(title: "query_first_list"), - :SpeciesList, :pattern_search, pattern: "query_first_list") + :SpeciesList, :all, pattern: "query_first_list") # in notes pattern = species_lists(:query_notes_list).notes assert_query(SpeciesList.where(notes: pattern), - :SpeciesList, :pattern_search, pattern: pattern) + :SpeciesList, :all, pattern: pattern) # in location assert_query( SpeciesList.where(location: locations(:burbank)), - :SpeciesList, :pattern_search, pattern: locations(:burbank).name + :SpeciesList, :all, pattern: locations(:burbank).name ) # in where pattern = species_lists(:where_list).where assert_query(SpeciesList.where(where: pattern), - :SpeciesList, :pattern_search, pattern: pattern) + :SpeciesList, :all, pattern: pattern) assert_query(SpeciesList.all, - :SpeciesList, :pattern_search, pattern: "") + :SpeciesList, :all, pattern: "") end def test_user_all From 6ca7d08d48dd35df96a87a72b9b14ace18435e6c Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Sun, 6 Oct 2024 15:39:48 -0700 Subject: [PATCH 18/28] Back out of location changes --- app/classes/query/location_base.rb | 57 ++++++++++---------- app/classes/query/location_pattern_search.rb | 24 +++++++++ app/classes/query/location_regexp_search.rb | 15 ++++++ app/controllers/locations_controller.rb | 9 ++-- test/models/query_test.rb | 21 ++++---- 5 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 app/classes/query/location_pattern_search.rb create mode 100644 app/classes/query/location_regexp_search.rb diff --git a/app/classes/query/location_base.rb b/app/classes/query/location_base.rb index 09599399e0..841484b9c5 100644 --- a/app/classes/query/location_base.rb +++ b/app/classes/query/location_base.rb @@ -15,9 +15,9 @@ def parameter_declarations north?: :float, south?: :float, east?: :float, - west?: :float, - pattern?: :string, - regexp?: :string + west?: :float + # pattern?: :string, + # regexp?: :string ).merge(content_filter_parameter_declarations(Location)) end @@ -26,36 +26,35 @@ def initialize_flavor add_owner_and_time_stamp_conditions("locations") end add_bounding_box_conditions_for_locations - add_pattern_condition - add_regexp_condition + # add_pattern_condition + # add_regexp_condition initialize_content_filters(Location) super end - # adds a join - def add_pattern_condition - return if params[:pattern].blank? - - add_join(:"location_descriptions.default!") - super - end - - def add_regexp_condition - return if params[:regexp].blank? - - @title_tag = :query_title_regexp_search - regexp = escape(params[:regexp].to_s.strip_squeeze) - where << "locations.name REGEXP #{regexp}" - end - - def search_fields - "CONCAT(" \ - "locations.name," \ - "#{LocationDescription.all_note_fields.map do |x| - "COALESCE(location_descriptions.#{x},'')" - end.join(",")}" \ - ")" - end + # def add_pattern_condition + # return if params[:pattern].blank? + + # add_join(:"location_descriptions.default!") + # super + # end + + # def add_regexp_condition + # return if params[:regexp].blank? + + # @title_tag = :query_title_regexp_search + # regexp = escape(params[:regexp].to_s.strip_squeeze) + # where << "locations.name REGEXP #{regexp}" + # end + + # def search_fields + # "CONCAT(" \ + # "locations.name," \ + # "#{LocationDescription.all_note_fields.map do |x| + # "COALESCE(location_descriptions.#{x},'')" + # end.join(",")}" \ + # ")" + # end def self.default_order "name" diff --git a/app/classes/query/location_pattern_search.rb b/app/classes/query/location_pattern_search.rb new file mode 100644 index 0000000000..1a90c1543a --- /dev/null +++ b/app/classes/query/location_pattern_search.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class Query::LocationPatternSearch < Query::LocationBase + def parameter_declarations + super.merge( + pattern: :string + ) + end + + def initialize_flavor + add_search_condition(search_fields, params[:pattern]) + add_join(:"location_descriptions.default!") + super + end + + def search_fields + "CONCAT(" \ + "locations.name," \ + "#{LocationDescription.all_note_fields.map do |x| + "COALESCE(location_descriptions.#{x},'')" + end.join(",")}" \ + ")" + end +end diff --git a/app/classes/query/location_regexp_search.rb b/app/classes/query/location_regexp_search.rb new file mode 100644 index 0000000000..6b6c7af0de --- /dev/null +++ b/app/classes/query/location_regexp_search.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class Query::LocationRegexpSearch < Query::LocationBase + def parameter_declarations + super.merge( + regexp: :string + ) + end + + def initialize_flavor + regexp = escape(params[:regexp].to_s.strip_squeeze) + where << "locations.name REGEXP #{regexp}" + super + end +end diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index ae1c6a42de..4cd2f03ede 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -89,7 +89,8 @@ def pattern redirect_to(location_path(loc.id)) else query = create_query( - :Location, :all, pattern: Location.user_format(@user, pattern) + :Location, :pattern_search, + pattern: Location.user_format(@user, pattern) ) show_selected_locations(query, link_all_sorts: true) end @@ -98,7 +99,7 @@ def pattern # Displays a list of all locations whose country matches the id param. def country query = create_query( - :Location, :all, regexp: "#{params[:country]}$" + :Location, :regexp_search, regexp: "#{params[:country]}$" ) show_selected_locations(query, link_all_sorts: true) end @@ -182,9 +183,7 @@ def get_matching_undefined_locations(query, args) def coerce_query_for_undefined_locations(query) model = :Observation flavor = query.flavor - # params used by :Location :all, but not yet :Observation :all - exceptions = [:regexp, :pattern] - args = query.params.dup.except(*exceptions) + args = query.params.dup result = nil # Select only observations with undefined location. diff --git a/test/models/query_test.rb b/test/models/query_test.rb index 900237fa8c..db5fbc407e 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -86,18 +86,19 @@ def test_validate_params Query.lookup(:Image, :by_user, user: rolf.id.to_s). params[:user]) - # assert_raises(RuntimeError) { Query.lookup(:User, :all) } # Oops, :in_set query is generic, # doesn't know to require Name instances here. - # assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: rolf) } - assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "one") } - assert_raises(RuntimeError) { Query.lookup(:Name, :all, ids: "1,2,3") } + # assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: rolf) } + assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: "one") } + assert_raises(RuntimeError) { Query.lookup(:Name, :in_set, ids: "1,2,3") } + assert_equal([names(:fungi).id], + Query.lookup(:Name, :in_set, + ids: names(:fungi).id.to_s).params[:ids]) + + # assert_raises(RuntimeError) { Query.lookup(:User, :all) } assert_equal([], Query.lookup(:User, :all, ids: []).params[:ids]) assert_equal([rolf.id], Query.lookup(:User, :all, ids: rolf.id).params[:ids]) - assert_equal([names(:fungi).id], - Query.lookup(:Name, :all, - ids: names(:fungi).id.to_s).params[:ids]) assert_equal([rolf.id, mary.id], Query.lookup(:User, :all, ids: [rolf.id, mary.id]).params[:ids]) @@ -3086,10 +3087,10 @@ def test_species_list_by_rss_log def test_species_list_by_user assert_query([species_lists(:first_species_list).id, species_lists(:another_species_list).id], - :SpeciesList, :all, user: rolf, by: :id) + :SpeciesList, :all, by_user: rolf, by: :id) assert_query(SpeciesList.where(user: mary), - :SpeciesList, :all, user: mary) - assert_query([], :SpeciesList, :all, user: dick) + :SpeciesList, :all, by_user: mary) + assert_query([], :SpeciesList, :all, by_user: dick) end def test_species_list_for_project From c7f217492a205f128df0602e5fc0a4acff859012 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Mon, 7 Oct 2024 00:34:33 -0700 Subject: [PATCH 19/28] Update rss_log_all.rb --- app/classes/query/rss_log_all.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/classes/query/rss_log_all.rb b/app/classes/query/rss_log_all.rb index 5ad3c7a61f..9a67393b37 100644 --- a/app/classes/query/rss_log_all.rb +++ b/app/classes/query/rss_log_all.rb @@ -31,7 +31,7 @@ def coerce_into_species_list_query end def do_coerce(new_model) - Query.lookup(new_model, :all, { by: :rss_log }.merge(params_minus_type)) + Query.lookup(new_model, :all, params_minus_type.merge(by: :rss_log)) end def params_minus_type From 56ebea3d91baafef494dbcdb39cdcf4c85bff4f5 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Mon, 7 Oct 2024 01:05:31 -0700 Subject: [PATCH 20/28] fix a few things --- app/classes/query/modules/ordering.rb | 1 + app/helpers/tabs/locations_helper.rb | 5 ++++- app/helpers/tabs/names_helper.rb | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/classes/query/modules/ordering.rb b/app/classes/query/modules/ordering.rb index 5d466828d8..132b9d91e3 100644 --- a/app/classes/query/modules/ordering.rb +++ b/app/classes/query/modules/ordering.rb @@ -200,6 +200,7 @@ def sort_by_rss_log(model) # use cached column if exists, and don't join # calling index method should include rss_logs if model.column_names.include?("log_updated_at") + where << "#{model.table_name}.rss_log_id IS NOT NULL" "#{model.table_name}.log_updated_at DESC" else add_join(:rss_logs) diff --git a/app/helpers/tabs/locations_helper.rb b/app/helpers/tabs/locations_helper.rb index b0d6ca0bfe..01bc1d325f 100644 --- a/app/helpers/tabs/locations_helper.rb +++ b/app/helpers/tabs/locations_helper.rb @@ -134,16 +134,19 @@ def location_countries_tabs end # Add some alternate sorting criteria. + # false positive + # rubocop:disable Layout/SpaceAroundOperators def location_index_sorts(query:) [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query.params[:by] == :rss_log ? "rss_log" : "updated_at"), + [(query&.params&[:by] == :rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t], ["box_area", :sort_by_box_area.t] ] end + # rubocop:enable Layout/SpaceAroundOperators # link attribute arrays def location_form_new_tabs(location:) diff --git a/app/helpers/tabs/names_helper.rb b/app/helpers/tabs/names_helper.rb index e3e5bcecae..09831c40f0 100644 --- a/app/helpers/tabs/names_helper.rb +++ b/app/helpers/tabs/names_helper.rb @@ -273,15 +273,18 @@ def all_names_tab(query) { class: tab_id(__method__.to_s) }] end + # false positive + # rubocop:disable Layout/SpaceAroundOperators def names_index_sorts(query:) [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query.params[:by] == :rss_log ? "rss_log" : "updated_at"), + [(query&.params&[:by] == :rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t] ] end + # rubocop:enable Layout/SpaceAroundOperators ### Forms def name_form_new_tabs From 3b80fb3bcc711d205455b2cd320b9d851e71de79 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Mon, 7 Oct 2024 01:24:12 -0700 Subject: [PATCH 21/28] hopefully fix sorts --- app/helpers/tabs/locations_helper.rb | 5 +++-- app/helpers/tabs/names_helper.rb | 8 +++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/helpers/tabs/locations_helper.rb b/app/helpers/tabs/locations_helper.rb index 01bc1d325f..bc78a24b2b 100644 --- a/app/helpers/tabs/locations_helper.rb +++ b/app/helpers/tabs/locations_helper.rb @@ -137,11 +137,12 @@ def location_countries_tabs # false positive # rubocop:disable Layout/SpaceAroundOperators def location_index_sorts(query:) + rss_log = query&.params&.dig(:by) == :rss_log || + query&.flavor == :rss_log [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query&.params&[:by] == :rss_log ? "rss_log" : "updated_at"), - :sort_by_updated_at.t], + [(rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t], ["box_area", :sort_by_box_area.t] ] diff --git a/app/helpers/tabs/names_helper.rb b/app/helpers/tabs/names_helper.rb index 09831c40f0..4a419a301c 100644 --- a/app/helpers/tabs/names_helper.rb +++ b/app/helpers/tabs/names_helper.rb @@ -273,18 +273,16 @@ def all_names_tab(query) { class: tab_id(__method__.to_s) }] end - # false positive - # rubocop:disable Layout/SpaceAroundOperators def names_index_sorts(query:) + rss_log = query&.params&.dig(:by) == :rss_log || + query&.flavor == :rss_log [ ["name", :sort_by_name.t], ["created_at", :sort_by_created_at.t], - [(query&.params&[:by] == :rss_log ? "rss_log" : "updated_at"), - :sort_by_updated_at.t], + [(rss_log ? "rss_log" : "updated_at"), :sort_by_updated_at.t], ["num_views", :sort_by_num_views.t] ] end - # rubocop:enable Layout/SpaceAroundOperators ### Forms def name_form_new_tabs From b93671925b2a28a10b9eb5682eab3db17e2718a3 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Mon, 7 Oct 2024 02:13:38 -0700 Subject: [PATCH 22/28] fix obs rss_logs --- app/helpers/tabs/locations_helper.rb | 3 --- .../observations/identify_controller_test.rb | 1 + test/fixtures/observations.yml | 21 ++++++++++++++++++- test/fixtures/rss_logs.yml | 14 +++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/helpers/tabs/locations_helper.rb b/app/helpers/tabs/locations_helper.rb index bc78a24b2b..9129396b5f 100644 --- a/app/helpers/tabs/locations_helper.rb +++ b/app/helpers/tabs/locations_helper.rb @@ -134,8 +134,6 @@ def location_countries_tabs end # Add some alternate sorting criteria. - # false positive - # rubocop:disable Layout/SpaceAroundOperators def location_index_sorts(query:) rss_log = query&.params&.dig(:by) == :rss_log || query&.flavor == :rss_log @@ -147,7 +145,6 @@ def location_index_sorts(query:) ["box_area", :sort_by_box_area.t] ] end - # rubocop:enable Layout/SpaceAroundOperators # link attribute arrays def location_form_new_tabs(location:) diff --git a/test/controllers/observations/identify_controller_test.rb b/test/controllers/observations/identify_controller_test.rb index f5b3f7d72e..89ef84021a 100644 --- a/test/controllers/observations/identify_controller_test.rb +++ b/test/controllers/observations/identify_controller_test.rb @@ -16,6 +16,7 @@ def test_identify_observations_index query = Query.lookup_and_save(:Observation, :needs_naming) assert_equal(query.num_results, obs_count) + get(:index) assert_no_flash assert_select(".matrix-box", obs_count) diff --git a/test/fixtures/observations.yml b/test/fixtures/observations.yml index 644ff220c7..de092a2e8c 100644 --- a/test/fixtures/observations.yml +++ b/test/fixtures/observations.yml @@ -25,6 +25,9 @@ DEFAULTS: &DEFAULTS where: MO Inc., 68 Bay Rd., North Falmouth, Massachusetts, USA user: dick vote_cache: 0.0 + created_at: 2023-09-15 04:05:03 + updated_at: 2023-09-15 04:05:03 + log_updated_at: 2023-09-15 04:05:03 when: <%= Time.now.strftime("%Y-%m-%d") %> # yyyy-mm-dd is_collection_location: 1 lifeform: " " @@ -726,19 +729,22 @@ wolf_fart: images: lone_wolf_image # images: lone_wolf_image, lone_wolf_image2 +# did not have rss_log reused_observation: <<: *DEFAULTS user: rolf + created_at: 2023-11-19 04:05:03 + log_updated_at: 2023-11-19 04:05:03 when: 2023-11-19 location: obs_default_location where: MO Inc., 68 Bay Rd., North Falmouth, Massachusetts, USA species_lists: reused_list rss_log: +# did not have rss_log deprecated_name_obs: <<: *DEFAULTS created_at: 2014-04-04 04:05:03 - updated_at: 2014-04-04 04:05:03 log_updated_at: 2014-04-04 04:05:03 when: 2012-06-07 user: vouchered_only_user @@ -752,12 +758,16 @@ deprecated_name_obs: # meets constraints of falmouth_2023_09_project falmouth_2023_09_obs: <<: *DEFAULTS + created_at: 2023-09-16 04:05:03 + log_updated_at: 2023-09-16 04:05:03 when: 2023-09-15 where: MO Inc., 68 Bay Rd., North Falmouth, Massachusetts, USA location: falmouth brett_woods_2023_09_obs: <<: *DEFAULTS + created_at: 2023-09-16 04:05:03 + log_updated_at: 2023-09-16 04:05:03 when: 2023-09-16 where: Brett Woods, Fairfield Co., Connecticut, USA lat: 41.2039 @@ -767,6 +777,8 @@ brett_woods_2023_09_obs: falmouth_2022_obs: <<: *DEFAULTS + created_at: 2022-09-15 04:05:03 + log_updated_at: 2022-09-15 04:05:03 when: 2022-09-15 # outside of dates of falmouth_2023_09_project where: MO Inc., 68 Bay Rd., North Falmouth, Massachusetts, USA location: falmouth @@ -796,3 +808,10 @@ newbie_obs: imported_inat_obs: <<: *DEFAULTS inat_id: 12345 +# +# +# IMPORTANT - AVOID CREATING FUTURE TEST ERRORS +# - When creating a new observation fixture, you MUST also create a +# corresponding rss_log, since the observation index now sorts by rss_log. +# - Observation fixtures require `created_at` and `log_updated_at` values. +# If you want to be able to find them in integration tests, give unique dates! diff --git a/test/fixtures/rss_logs.yml b/test/fixtures/rss_logs.yml index 01e3089f7d..cfc5772ba8 100644 --- a/test/fixtures/rss_logs.yml +++ b/test/fixtures/rss_logs.yml @@ -70,6 +70,20 @@ wolf_fart_rss_log: # updated_at: 2023-11-15 12:00:01 # created_at: 2023-11-15 12:00:00 +reused_observation_rss_log: + <<: *DEFAULTS + observation: reused_observation + created_at: 2023-11-19 04:05:03 + updated_at: 2023-11-19 04:05:03 + # notes: " log_observation_created user \n" + +deprecated_name_obs_rss_log: + <<: *DEFAULTS + observation: deprecated_name_obs + created_at: 2014-04-04 04:05:03 + updated_at: 2014-04-04 04:05:03 + # notes: " log_observation_created user \n" + strobilurus_diminutivus_obs_rss_log: <<: *DEFAULTS observation: strobilurus_diminutivus_obs From 419c452e5fe89ed3a40fd99396c0a6065d0f992e Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Tue, 8 Oct 2024 14:24:40 -0700 Subject: [PATCH 23/28] Add fixtures tests --- app/extensions/fixture_set_extensions.rb | 12 ++++++++++++ test/models/fixtures_test.rb | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 app/extensions/fixture_set_extensions.rb create mode 100644 test/models/fixtures_test.rb diff --git a/app/extensions/fixture_set_extensions.rb b/app/extensions/fixture_set_extensions.rb new file mode 100644 index 0000000000..60285be2a7 --- /dev/null +++ b/app/extensions/fixture_set_extensions.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class ActiveRecord::FixtureSet + # Reverse lookup of fixture label (how we refer to fixtures) by id. + def self.reverse_lookup(table, id) + ActiveRecord::FixtureSet.all_loaded_fixtures[table.to_s]. + fixtures.each_key do |key| + return key if ActiveRecord::FixtureSet.identify(key) == id + end + nil + end +end diff --git a/test/models/fixtures_test.rb b/test/models/fixtures_test.rb new file mode 100644 index 0000000000..6b499d583a --- /dev/null +++ b/test/models/fixtures_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "test_helper" + +class FixturesTest < ActiveSupport::TestCase + def test_observations_all_have_rss_logs + Observation.find_each do |obs| + name = ActiveRecord::FixtureSet.reverse_lookup("observations", obs.id) + assert_not_nil( + obs.rss_log_id, + "Observation #{name} needs a corresponding RssLog fixture." + ) + assert_not_nil( + obs.log_updated_at, + "Observation #{name} needs a log_updated_at value." + ) + end + end +end From 7c4ea381a5b34398e7dbe3d14d91c60a4d52a143 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Tue, 8 Oct 2024 14:29:23 -0700 Subject: [PATCH 24/28] Better comments --- app/extensions/fixture_set_extensions.rb | 4 +++- test/models/fixtures_test.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/extensions/fixture_set_extensions.rb b/app/extensions/fixture_set_extensions.rb index 60285be2a7..3a3a772d17 100644 --- a/app/extensions/fixture_set_extensions.rb +++ b/app/extensions/fixture_set_extensions.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true class ActiveRecord::FixtureSet - # Reverse lookup of fixture label (how we refer to fixtures) by id. + # Reverse lookup of a fixture's label (e.g. `deprecated_name_obs`) by its id. + # For better fixture test messages. + # https://stackoverflow.com/a/32322296/3357635 def self.reverse_lookup(table, id) ActiveRecord::FixtureSet.all_loaded_fixtures[table.to_s]. fixtures.each_key do |key| diff --git a/test/models/fixtures_test.rb b/test/models/fixtures_test.rb index 6b499d583a..24fbf008b8 100644 --- a/test/models/fixtures_test.rb +++ b/test/models/fixtures_test.rb @@ -5,7 +5,7 @@ class FixturesTest < ActiveSupport::TestCase def test_observations_all_have_rss_logs Observation.find_each do |obs| - name = ActiveRecord::FixtureSet.reverse_lookup("observations", obs.id) + name = ActiveRecord::FixtureSet.reverse_lookup(:observations, obs.id) assert_not_nil( obs.rss_log_id, "Observation #{name} needs a corresponding RssLog fixture." From 62ee35634ddbd9d58a5bf7378a290d92eb620dbf Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Tue, 8 Oct 2024 14:30:39 -0700 Subject: [PATCH 25/28] Fix null values --- test/fixtures/observations.yml | 2 -- test/models/fixtures_test.rb | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/test/fixtures/observations.yml b/test/fixtures/observations.yml index de092a2e8c..a3f5ad84c9 100644 --- a/test/fixtures/observations.yml +++ b/test/fixtures/observations.yml @@ -739,7 +739,6 @@ reused_observation: location: obs_default_location where: MO Inc., 68 Bay Rd., North Falmouth, Massachusetts, USA species_lists: reused_list - rss_log: # did not have rss_log deprecated_name_obs: @@ -753,7 +752,6 @@ deprecated_name_obs: name: petigera text_name: Petigera lifeform: " lichen " - rss_log: # meets constraints of falmouth_2023_09_project falmouth_2023_09_obs: diff --git a/test/models/fixtures_test.rb b/test/models/fixtures_test.rb index 24fbf008b8..52abf38c2d 100644 --- a/test/models/fixtures_test.rb +++ b/test/models/fixtures_test.rb @@ -8,11 +8,11 @@ def test_observations_all_have_rss_logs name = ActiveRecord::FixtureSet.reverse_lookup(:observations, obs.id) assert_not_nil( obs.rss_log_id, - "Observation #{name} needs a corresponding RssLog fixture." + "Observation #{name} needs a corresponding RssLog fixture" ) assert_not_nil( obs.log_updated_at, - "Observation #{name} needs a log_updated_at value." + "Observation #{name} needs a log_updated_at value" ) end end From 205434e3620c058a824296751b8044bd7428c1cc Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Wed, 9 Oct 2024 00:22:14 -0700 Subject: [PATCH 26/28] Remove `where rss_log_id iS NOT NULL` condition --- app/classes/query/modules/ordering.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/classes/query/modules/ordering.rb b/app/classes/query/modules/ordering.rb index 132b9d91e3..5d466828d8 100644 --- a/app/classes/query/modules/ordering.rb +++ b/app/classes/query/modules/ordering.rb @@ -200,7 +200,6 @@ def sort_by_rss_log(model) # use cached column if exists, and don't join # calling index method should include rss_logs if model.column_names.include?("log_updated_at") - where << "#{model.table_name}.rss_log_id IS NOT NULL" "#{model.table_name}.log_updated_at DESC" else add_join(:rss_logs) From 31b49d1ddd1b8ae8323acb2dec2b131fab9942e9 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Wed, 9 Oct 2024 00:28:14 -0700 Subject: [PATCH 27/28] Fix query_test expectation --- test/models/query_test.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/models/query_test.rb b/test/models/query_test.rb index a229cad65d..f1eae6f76c 100644 --- a/test/models/query_test.rb +++ b/test/models/query_test.rb @@ -1316,15 +1316,10 @@ def test_rss_log_coercion assert_equal("Observation", q4.model.to_s) assert_equal("SpeciesList", q5.model.to_s) - assert_equal(:rss_log, q2.params[:by]) - assert_equal(:rss_log, q3.params[:by]) - assert_equal(:rss_log, q4.params[:by]) - assert_equal(:rss_log, q5.params[:by]) - - assert_equal({}, q2.params) - assert_equal({}, q3.params) - assert_equal({}, q4.params) - assert_equal({}, q5.params) + assert_equal(:rss_log, q2.params[:by].to_sym) + assert_equal(:rss_log, q3.params[:by].to_sym) + assert_equal(:rss_log, q4.params[:by].to_sym) + assert_equal(:rss_log, q5.params[:by].to_sym) end def test_coercable From 06665c35633c83e763e167f18d04387466f66c84 Mon Sep 17 00:00:00 2001 From: andrew nimmo Date: Wed, 9 Oct 2024 00:35:03 -0700 Subject: [PATCH 28/28] remove _by_rss_log flavors --- app/classes/query/article_by_rss_log.rb | 12 ------------ app/classes/query/location_by_rss_log.rb | 12 ------------ app/classes/query/name_by_rss_log.rb | 12 ------------ app/classes/query/observation_by_rss_log.rb | 12 ------------ app/classes/query/project_by_rss_log.rb | 12 ------------ app/classes/query/species_list_by_rss_log.rb | 12 ------------ 6 files changed, 72 deletions(-) delete mode 100644 app/classes/query/article_by_rss_log.rb delete mode 100644 app/classes/query/location_by_rss_log.rb delete mode 100644 app/classes/query/name_by_rss_log.rb delete mode 100644 app/classes/query/observation_by_rss_log.rb delete mode 100644 app/classes/query/project_by_rss_log.rb delete mode 100644 app/classes/query/species_list_by_rss_log.rb diff --git a/app/classes/query/article_by_rss_log.rb b/app/classes/query/article_by_rss_log.rb deleted file mode 100644 index 7a0cae53e1..0000000000 --- a/app/classes/query/article_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::ArticleByRssLog < Query::ArticleBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end diff --git a/app/classes/query/location_by_rss_log.rb b/app/classes/query/location_by_rss_log.rb deleted file mode 100644 index c38b87a2a4..0000000000 --- a/app/classes/query/location_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::LocationByRssLog < Query::LocationBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end diff --git a/app/classes/query/name_by_rss_log.rb b/app/classes/query/name_by_rss_log.rb deleted file mode 100644 index c723efa28a..0000000000 --- a/app/classes/query/name_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::NameByRssLog < Query::NameBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end diff --git a/app/classes/query/observation_by_rss_log.rb b/app/classes/query/observation_by_rss_log.rb deleted file mode 100644 index e78325368f..0000000000 --- a/app/classes/query/observation_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::ObservationByRssLog < Query::ObservationBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end diff --git a/app/classes/query/project_by_rss_log.rb b/app/classes/query/project_by_rss_log.rb deleted file mode 100644 index 158776f9e3..0000000000 --- a/app/classes/query/project_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::ProjectByRssLog < Query::ProjectBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end diff --git a/app/classes/query/species_list_by_rss_log.rb b/app/classes/query/species_list_by_rss_log.rb deleted file mode 100644 index 8400e332e9..0000000000 --- a/app/classes/query/species_list_by_rss_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class Query::SpeciesListByRssLog < Query::SpeciesListBase - def initialize_flavor - add_join(:rss_logs) - super - end - - def default_order - "rss_log" - end -end