Extract "raise_search_error" method in item search query parsing

Just to make all this a bit more wieldy, and not repeat the same prefix
all the time!
This commit is contained in:
Emi Matchu 2024-02-27 15:03:18 -08:00
parent 61b1a1aed1
commit 76d741091c

View file

@ -70,26 +70,22 @@ class Item
Filter.fits_pet_type(pet_type, color_name:, species_name:) : Filter.fits_pet_type(pet_type, color_name:, species_name:) :
Filter.not_fits_pet_type(pet_type, color_name:, species_name:) Filter.not_fits_pet_type(pet_type, color_name:, species_name:)
end end
message = I18n.translate('items.search.errors.not_found.fits_target', raise_search_error "not_found.fits_target", value: value
value: value)
raise Item::Search::Error, message
when 'species' when 'species'
begin begin
species = Species.find_by_name!(value) species = Species.find_by_name!(value)
color = Color.find_by_name!('blue') color = Color.find_by_name!('blue')
pet_type = PetType.where(color_id: color.id, species_id: species.id).first! pet_type = PetType.where(color_id: color.id, species_id: species.id).first!
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
message = I18n.translate('items.search.errors.not_found.species', raise_search_error "not_found.species",
species_name: value.capitalize) species_name: value.capitalize
raise Item::Search::Error, message
end end
is_positive ? is_positive ?
Filter.fits_species(pet_type.body_id, value) : Filter.fits_species(pet_type.body_id, value) :
Filter.not_fits_species(pet_type.body_id, value) Filter.not_fits_species(pet_type.body_id, value)
when 'user' when 'user'
if user.nil? if user.nil?
message = I18n.translate('items.search.errors.not_logged_in') raise_search_error "not_logged_in"
raise Item::Search::Error, message
end end
case value case value
when 'owns' when 'owns'
@ -97,9 +93,7 @@ class Item
when 'wants' when 'wants'
is_positive ? Filter.wanted_by(user) : Filter.not_wanted_by(user) is_positive ? Filter.wanted_by(user) : Filter.not_wanted_by(user)
else else
message = I18n.translate('items.search.errors.not_found.ownership', raise_search_error "not_found.ownership", keyword: value
keyword: value)
raise Item::Search::Error, message
end end
when 'is' when 'is'
case value case value
@ -110,14 +104,10 @@ class Item
when 'pb' when 'pb'
is_positive ? Filter.is_pb : Filter.is_not_pb is_positive ? Filter.is_pb : Filter.is_not_pb
else else
message = I18n.translate('items.search.errors.not_found.label', raise_search_error "not_found.label", label: "is:#{value}"
:label => "is:#{value}")
raise Item::Search::Error, message
end end
else else
message = I18n.translate('items.search.errors.not_found.label', raise_search_error "not_found.label", label: key
:label => key)
raise Item::Search::Error, message
end end
end end
@ -170,9 +160,8 @@ class Item
begin begin
PetType.matching_name(color_name, species_name).first! PetType.matching_name(color_name, species_name).first!
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
message = I18n.translate('items.search.errors.not_found.pet_type', raise_search_error "not_found.pet_type",
name1: color_name.capitalize, name2: species_name.capitalize) name1: color_name.capitalize, name2: species_name.capitalize
raise Item::Search::Error, message
end end
end end
@ -182,9 +171,8 @@ class Item
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
color_name = Color.find(color_id).name rescue "Color #{color_id}" color_name = Color.find(color_id).name rescue "Color #{color_id}"
species_name = Species.find(species_id).name rescue "Species #{species_id}" species_name = Species.find(species_id).name rescue "Species #{species_id}"
message = I18n.translate('items.search.errors.not_found.pet_type', raise_search_error "not_found.pet_type",
name1: color_name.capitalize, name2: species_name.capitalize) name1: color_name.capitalize, name2: species_name.capitalize
raise Item::Search::Error, message
end end
end end
@ -192,11 +180,15 @@ class Item
begin begin
AltStyle.find(alt_style_id) AltStyle.find(alt_style_id)
rescue rescue
message = I18n.translate('items.search.errors.not_found.alt_style', raise_search_error "not_found.alt_style",
filter_text: "alt-style-#{alt_style_id}") filter_text: "alt-style-#{alt_style_id}"
raise Item::Search::Error, message
end end
end end
def self.raise_search_error(kind, ...)
raise Item::Search::Error,
I18n.translate("items.search.errors.#{kind}", ...)
end
end end
class Error < Exception class Error < Exception