From fe6035d43871d1d11a27c4e064b8505e70d34634 Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 1 Jul 2024 17:20:38 -0700 Subject: [PATCH] Default to compatible pet types in new item page preview Just adapted from Impress 2020 logic again, easy peasy! --- app/controllers/items_controller.rb | 5 +---- app/models/item.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index ad7ec488..8df9b4bf 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -214,10 +214,7 @@ class ItemsController < ApplicationController end def load_default_preview_pet_type - PetType.find_by_color_id_and_species_id( - Color.find_by_name("Blue"), - Species.find_by_name("Acara"), - ) + @item.compatible_pet_type end def validate_preview diff --git a/app/models/item.rb b/app/models/item.rb index 1371e345..718a1ad0 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -489,6 +489,24 @@ class Item < ApplicationRecord }.merge(options)) end + def compatible_body_ids + swf_assets.map(&:body_id).uniq + end + + def compatible_pet_types + return PetType.all if compatible_body_ids.include?(0) + 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)