From d983a20989687d33fd6f86c7ff5ecdfc23d39e2a Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 27 Feb 2024 15:33:08 -0800 Subject: [PATCH] Add support for `fits:nostalgic-faerie-draik` item searches Nice and easy now that we have `series_name` in the database! I didn't add the `fits:alt-style-12345` case yet though! --- app/models/alt_style.rb | 6 ++++++ app/models/item/search/query.rb | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 1f31c562..4671c0dd 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -6,6 +6,12 @@ class AltStyle < ApplicationRecord has_many :swf_assets, through: :parent_swf_asset_relationships has_many :contributions, as: :contributed, inverse_of: :contributed + scope :matching_name, ->(series_name, color_name, species_name) { + color = Color.find_by_name!(color_name) + species = Species.find_by_name!(species_name) + where(series_name:, color_id: color.id, species_id: species.id) + } + def name I18n.translate('pet_types.human_name', color_human_name: color.human_name, species_human_name: species.human_name) diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index a88ebf9f..342e04e2 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -62,6 +62,7 @@ class Item when 'restricts' is_positive ? Filter.restricts(value) : Filter.not_restricts(value) when 'fits' + # First, try the `fits:blue-acara` case. match = value.match(/^([^-]+)-([^-]+)$/) if match.present? color_name, species_name = match.captures @@ -70,6 +71,22 @@ class Item Filter.fits_pet_type(pet_type, color_name:, species_name:) : Filter.not_fits_pet_type(pet_type, color_name:, species_name:) end + + # Next, try the `fits:nostalgic-faerie-draik` case. + match = value.match(/^([^-]+)-([^-]+)-([^-]+)$/) + if match.present? + series_name, color_name, species_name = match.captures + alt_style = load_alt_style_by_name( + series_name, color_name, species_name) + return is_positive ? + Filter.fits_alt_style(alt_style) : + Filter.not_fits_alt_style(alt_style) + end + + # TODO: We could make `fits:acara` an alias for `species:acara`, or + # even the primary syntax? + + # If none of these cases work, raise an error. raise_search_error "not_found.fits_target", value: value when 'species' begin @@ -176,10 +193,19 @@ class Item end end + def self.load_alt_style_by_name(series_name, color_name, species_name) + begin + AltStyle.matching_name(series_name, color_name, species_name).first! + rescue ActiveRecord::RecordNotFound + raise_search_error "not_found.alt_style", + filter_text: "#{series_name}-#{color_name}-#{species_name}" + end + end + def self.load_alt_style_by_id(alt_style_id) begin AltStyle.find(alt_style_id) - rescue + rescue ActiveRecord::RecordNotFound raise_search_error "not_found.alt_style", filter_text: "alt-style-#{alt_style_id}" end