Sort pet types alphabetically when filtering the Rainbow Pool

The default is latest first, but when you're searching a color or
species, you probably just want a consistent alphabetical order.
This commit is contained in:
Emi Matchu 2024-10-11 16:24:24 -07:00
parent 5648f55d2c
commit ad51690617

View file

@ -13,17 +13,23 @@ class PetTypesController < ApplicationController
@selected_color = Color.find_by!(name: params[:color]) @selected_color = Color.find_by!(name: params[:color])
@selected_color_name = @selected_color.human_name @selected_color_name = @selected_color.human_name
end end
@selected_order =
if @selected_species.present? || @selected_color.present?
:alphabetical
else
:newest
end
@pet_types = PetType. @pet_types = PetType.
includes(:color, :species, :pet_states). includes(:color, :species, :pet_states).
order(created_at: :desc).
paginate(page: params[:page], per_page: 30) paginate(page: params[:page], per_page: 30)
if @selected_species @pet_types.where!(species_id: @selected_species) if @selected_species
@pet_types = @pet_types.where(species_id: @selected_species) @pet_types.where!(color_id: @selected_color) if @selected_color
end if @selected_order == :newest
if @selected_color @pet_types.order!(created_at: :desc)
@pet_types = @pet_types.where(color_id: @selected_color) elsif @selected_order == :alphabetical
@pet_types.merge!(Color.alphabetical).merge!(Species.alphabetical)
end end
if @selected_species && @selected_color && @pet_types.size == 1 if @selected_species && @selected_color && @pet_types.size == 1