From 61b1a1aed14f2b2d44b07ddb4ed9664ee802f0e7 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 27 Feb 2024 14:56:36 -0800 Subject: [PATCH] Improve parsing `fits:blue-acara` filter Previously, passing in `fits:blue` would cause a crash, because `species_name` part of the split would be `nil`, oops! In this change, we use a regex for more explicitness about the pattern we're trying to match. We'll also add more cases next! (You'll note the error message mentions `fits:nostalgic-faerie-draik`, which isn't actually possible yet, but will be!) --- app/models/item/search/query.rb | 16 +++++++++++----- config/locales/en.yml | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index f335b400..089afea7 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -62,11 +62,17 @@ class Item when 'restricts' is_positive ? Filter.restricts(value) : Filter.not_restricts(value) when 'fits' - color_name, species_name = value.split("-") - pet_type = load_pet_type_by_name(color_name, species_name) - is_positive ? - Filter.fits_pet_type(pet_type, color_name:, species_name:) : - Filter.not_fits_pet_type(pet_type, color_name:, species_name:) + match = value.match(/^([^-]+)-([^-]+)$/) + if match.present? + color_name, species_name = match.captures + pet_type = load_pet_type_by_name(color_name, species_name) + return is_positive ? + Filter.fits_pet_type(pet_type, color_name:, species_name:) : + Filter.not_fits_pet_type(pet_type, color_name:, species_name:) + end + message = I18n.translate('items.search.errors.not_found.fits_target', + value: value) + raise Item::Search::Error, message when 'species' begin species = Species.find_by_name!(value) diff --git a/config/locales/en.yml b/config/locales/en.yml index 268cc883..2e1afe07 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -361,6 +361,8 @@ en: alt_style: We have no record of the "%{filter_text}" alt style. Is it spelled correctly? pet_type_id: We have no record of pet type %{id}. Weird. + fits_target: I'm not sure what "fits:%{value}" means. You can + use "fits:blue-acara", "fits:nostalgic-faerie-draik", or similar! not_logged_in: The "user" filters are only available if you're logged in. flag_keywords: is: is