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: