diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 8df9b4bf..a0f05f69 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -202,7 +202,12 @@ class ItemsController < ApplicationController return load_default_preview_pet_type if color_id.nil? || species_id.nil? - PetType.find_or_initialize_by(color_id:, species_id:) + PetType.find_or_initialize_by(color_id:, species_id:).tap do |pet_type| + if pet_type.persisted? + cookies["preferred-preview-color-id"] = color_id + cookies["preferred-preview-species-id"] = species_id + end + end end def load_preview_pet_type @@ -214,7 +219,10 @@ class ItemsController < ApplicationController end def load_default_preview_pet_type - @item.compatible_pet_type + @item.compatible_pet_types. + preferring_species(cookies["preferred-preview-species-id"] || ""). + preferring_color(cookies["preferred-preview-color-id"] || ""). + preferring_simple.first end def validate_preview diff --git a/app/models/item.rb b/app/models/item.rb index 718a1ad0..2eb2aa31 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -498,15 +498,6 @@ class Item < ApplicationRecord PetType.where(body_id: compatible_body_ids) end - # Return a pet type that can wear this item, preferring simple colors and - # early-alphabetically species. - def compatible_pet_type - compatible_pet_types.joins(:color, :species).order(:species_id). - merge(Color.order(basic: :desc, standard: :desc, name: :asc)). - merge(Species.order(name: :asc)). - first - end - def handle_assets! if @parent_swf_asset_relationships_to_update && @current_body_id new_swf_asset_ids = @parent_swf_asset_relationships_to_update.map(&:swf_asset_id) diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index 412ddc88..32286bf9 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -16,6 +16,17 @@ class PetType < ApplicationRecord species = Species.find_by_name!(species_name) where(color_id: color.id, species_id: species.id) } + scope :preferring_species, ->(species_id) { + joins(:species).order([Arel.sql("species_id = ? DESC"), species_id]) + } + scope :preferring_color, ->(color_id) { + joins(:color).order([Arel.sql("color_id = ? DESC"), color_id]) + } + scope :preferring_simple, -> { + joins(:species, :color). + merge(Species.order(name: :asc)). + merge(Color.order(basic: :desc, standard: :desc, name: :asc)) + } def self.special_color_or_basic(special_color) color_ids = special_color ? [special_color.id] : Color.basic.select([:id]).map(&:id)