From fe6d42ef67dee43e72bd89a9369c85244fc89309 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 29 Dec 2024 15:52:50 -0800 Subject: [PATCH] Remove "" from pet style search filters When there's an unlabeled style, previously we'd include the placeholder value "" in the search dropdown, even though we don't actually support searching by it. Now, we don't! I did this in part by just refactoring how we look this stuff up, with queries that don't load *all* alt styles into memory, which will help perf a bit as more of them are released. --- app/controllers/alt_styles_controller.rb | 13 ++++--------- app/models/alt_style.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/alt_styles_controller.rb b/app/controllers/alt_styles_controller.rb index 742034d7..e7777799 100644 --- a/app/controllers/alt_styles_controller.rb +++ b/app/controllers/alt_styles_controller.rb @@ -2,20 +2,15 @@ class AltStylesController < ApplicationController before_action :support_staff_only, except: [:index] def index - @all_alt_styles = AltStyle.includes(:species, :color) - - @all_colors = @all_alt_styles.map(&:color).uniq.sort_by(&:name) - @all_species = @all_alt_styles.map(&:species).uniq.sort_by(&:name) - - @all_series_names = @all_alt_styles.map(&:series_name).uniq.sort - @all_color_names = @all_colors.map(&:human_name) - @all_species_names = @all_species.map(&:human_name) + @all_series_names = AltStyle.all_series_names + @all_color_names = AltStyle.all_supported_colors.map(&:human_name).sort + @all_species_names = AltStyle.all_supported_species.map(&:human_name).sort @series_name = params[:series] @color = find_color @species = find_species - @alt_styles = @all_alt_styles.includes(:swf_assets) + @alt_styles = AltStyle.includes(:color, :species, :swf_assets) @alt_styles.where!(series_name: @series_name) if @series_name.present? @alt_styles.merge!(@color.alt_styles) if @color @alt_styles.merge!(@species.alt_styles) if @species diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 857f0e9f..583ea0ac 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -101,6 +101,18 @@ class AltStyle < ApplicationRecord "" end + def self.all_series_names + distinct.where.not(series_name: nil).pluck(:series_name).sort + end + + def self.all_supported_colors + Color.find(distinct.pluck(:color_id)) + end + + def self.all_supported_species + Species.find(distinct.pluck(:species_id)) + end + # For convenience in the console! def self.find_by_name(color_name, species_name) color = Color.find_by_name(color_name)