From 989c96fd2bf08b671e5748e86f9a2712713e23f5 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Fri, 13 Sep 2024 19:56:41 -0700 Subject: [PATCH] Oops, fix pb_item for "Royal Girl Elephante Gold Bracelets" and similar There's some funny bugs we had here, like "Relic Elephante Jewellery" and "Royal Girl Skeith Bodice" getting assigned "Ice", and "Tyrannian Meerca Spear" being "Pea" lmao I went and checked all the assignments now and they look good to me! ```ruby Item.is_pb.order(:name). map { |i| [i.pb_color&.human_name, i.name] } ``` --- app/models/item.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index 40abf60cc..6dc6e85b4 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -243,8 +243,14 @@ class Item < ApplicationRecord normalized_name = name.downcase.gsub("female", "girl").gsub("male", "boy"). gsub(/\s/, "") - Color.order(:name). - find { |c| normalized_name.include?(c.name.downcase.gsub(/\s/, "")) } + # For each color, normalize its name, look for it in the item name, and + # return the matching color that appears earliest. (This is important for + # items that contain multiple color names, like the "Royal Girl Elephante + # Gold Bracelets".) + Color.all.to_h { |c| [c, c.name.downcase.gsub(/\s/, "")] }. + transform_values { |n| normalized_name.index(n) }. + filter { |c, n| n.present? }. + min_by { |c, i| i }&.first end # If this is a PB item, return the corresponding Species, inferred from the