Fix NC style series sort order in search dropdown

Oops, I didn't realize that the MySQL function `SUBSTRING_INDEX` always
returns the full string if the split delimiter isn't found.

This meant that, for series names like "Regal", we read the main name as
"Regal" (correct) and the variant name as "Regal" (incorrect).

This caused sort order to be incorrect for some series, e.g.,
- Prismatic Dawn: Regal
- Prismatic Dusk: Regal
- Prismatic Mirage: Regal
- Regal

whereas the main series name is meant to be first, and *does* come first
in cases like "Festive" where the main name sorts before any of the
variant names!

In this change, we update the variant name definition to return an empty
string. That way, when there's no variant name and it's just the main
series, that one sorts to the top of the series variants.
This commit is contained in:
Emi Matchu 2025-06-22 12:37:28 -07:00
parent 14be40a292
commit 0e57a76ce6

View file

@ -32,11 +32,13 @@ class AltStyle < ApplicationRecord
}
scope :by_series_main_name, -> {
# The main part of the series name, like "Nostalgic".
# If there's no colon, uses the whole string.
order(Arel.sql("SUBSTRING_INDEX(series_name, ': ', -1)"))
}
scope :by_series_variant_name, -> {
# The variant part of the series name, like "Prismatic Cyan".
order(Arel.sql("SUBSTRING_INDEX(series_name, ': ', 1)"))
# If there's no colon, uses an empty string.
order(Arel.sql("SUBSTRING(series_name, 1, LOCATE(': ', series_name) - 1)"))
}
scope :by_color_name, -> {
joins(:color).order(Color.arel_table[:name])