From 3781c9810ac66e1242fd1d533009eb2b10245e3e Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 27 Feb 2024 14:32:54 -0800 Subject: [PATCH] Item search can filter by fitting alt styles (but missing many details!) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A query like this works for finding items containing "hat" that fit alt style #87296 (the Faerie Acara): http://localhost:3000/items?q%5B0%5D%5Bkey%5D=name&q%5B0%5D%5Bvalue%5D=hat&q%5B1%5D%5Bkey%5D=fits&q%5B1%5D%5Bvalue%5D%5Bspecies_id%5D=1&q%5B1%5D%5Bvalue%5D%5Bcolor_id%5D=8&q%5B1%5D%5Bvalue%5D%5Balt_style_id%5D=87296 But there's two main missing pieces still: 1. You can't do a text-filter version of this—in fact, clicking Search immediately on the page this loads will return an error! 2. We haven't extended this to the `with_appearances_for` parameter, so we add a `NotImplementedError` to that bit for now too. I'm also thinking that the text filter should ideally be like, `fits:nostalgic-faerie-acara` if we can pull it off; and then fall back to the plainer `fits:alt-style-87296` if e.g. we don't know the set it's from yet. --- app/controllers/items_controller.rb | 1 + app/models/item/search/query.rb | 44 ++++++++++++++++++++++++----- config/locales/en.yml | 2 ++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 3fe99d9c..9fa84911 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -123,6 +123,7 @@ class ItemsController < ApplicationController def load_appearances appearance_params = params[:with_appearances_for] return {} if appearance_params.blank? + raise NotImplementedError if appearance_params[:alt_style_id].present? pet_type = Item::Search::Query.load_pet_type_by_color_and_species( appearance_params[:color_id], appearance_params[:species_id]) diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index 6a685c97..53b59364 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -128,12 +128,18 @@ class Item Filter.restricts(value) : Filter.not_restricts(value)) when 'fits' - raise NotImplementedError if value[:alt_style_id].present? - pet_type = load_pet_type_by_color_and_species( - value[:color_id], value[:species_id]) - filters << (is_positive ? - Filter.fits_pet_type(pet_type) : - Filter.not_fits_pet_type(pet_type)) + if value[:alt_style_id].present? + alt_style = load_alt_style_by_id(value[:alt_style_id]) + filters << (is_positive ? + Filter.fits_alt_style(alt_style) : + Filter.fits_alt_style(alt_style)) + else + pet_type = load_pet_type_by_color_and_species( + value[:color_id], value[:species_id]) + filters << (is_positive ? + Filter.fits_pet_type(pet_type) : + Filter.not_fits_pet_type(pet_type)) + end when 'user_closet_hanger_ownership' case value when 'true' @@ -174,6 +180,16 @@ class Item raise Item::Search::Error, message end end + + def self.load_alt_style_by_id(alt_style_id) + begin + AltStyle.find(alt_style_id) + rescue + message = I18n.translate('items.search.errors.not_found.alt_style', + filter_text: "alt-style-#{alt_style_id}") + raise Item::Search::Error, message + end + end end class Error < Exception @@ -235,6 +251,16 @@ class Item self.new Item.not_fits(pet_type.body_id), "-fits:#{q value}" end + def self.fits_alt_style(alt_style) + value = alt_style_to_filter_text(alt_style) + self.new Item.fits(alt_style.body_id), "fits:#{q value}" + end + + def self.not_fits_alt_style(alt_style) + value = alt_style_to_filter_text(alt_style) + self.new Item.not_fits(alt_style.body_id), "-fits:#{q value}" + end + def self.fits_species(body_id, species_name) self.new Item.fits(body_id), "species:#{q species_name}" end @@ -297,7 +323,11 @@ class Item species_name ||= pet_type.species.name # NOTE: Some color syntaxes are weird, like `fits:"polka dot-aisha"`! - value = "#{color_name}-#{species_name}".downcase + "#{color_name}-#{species_name}".downcase + end + + def self.alt_style_to_filter_text(alt_style) + "alt-style-#{alt_style.id}" end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 91942e88..268cc883 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -358,6 +358,8 @@ en: user:wants? pet_type: We have no record of the %{name1} %{name2}. It is spelled correctly? + 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. not_logged_in: The "user" filters are only available if you're logged in. flag_keywords: