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!
This commit is contained in:
Emi Matchu 2024-02-27 15:33:08 -08:00
parent 9f74e6020e
commit d983a20989
2 changed files with 33 additions and 1 deletions

View file

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

View file

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