1
0
Fork 0
forked from OpenNeo/impress

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!)
This commit is contained in:
Emi Matchu 2024-02-27 14:56:36 -08:00
parent 1860f5b6be
commit 61b1a1aed1
2 changed files with 13 additions and 5 deletions

View file

@ -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)

View file

@ -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