From 3dab235335244cd80d0b8c386d9c731801875d37 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Wed, 5 Jun 2024 19:20:57 -0700 Subject: [PATCH] Handle newly-released PB items in Item Getting Guide Oh right, it's possible for `Item#pb?` to return true, but `Item#pb_color` to return `nil`, if the item has the paintbrush item description but we can't find a color whose name matches the item name. This would be expected if a new color were added to Neopets, and PB items for it were modeled by the community, but we hadn't manually added the color to the database yet. Previously, the Item Getting Guide would crash in this scenario. Now, it correctly handles the possibility of a `nil` value for `pb_color`, and shows some placeholder info. To test this, I temporarily edited some item names to not contain the color name anymore (e.g. "P-rate Elephante Shirt and Vest"), then loaded the guide and made changes until it no longer crashed. --- app/controllers/items_controller.rb | 6 +++--- app/views/items/sources.html.haml | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 0a2544bf..01e7cd6e 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -134,10 +134,10 @@ class ItemsController < ApplicationController # Also, PB items have some special handling: we group them by color, then # load example pet types for the colors that don't have paint brushes. @pb_items_by_color = @pb_items.group_by(&:pb_color). - sort_by { |color, items| color.name }.to_h + sort_by { |color, items| color&.name }.to_h - colors_without_thumbnails = - @pb_items_by_color.keys.reject(&:pb_item_thumbnail_url?) + colors_without_thumbnails = @pb_items_by_color.keys. + select(&:present?).reject(&:pb_item_thumbnail_url?) @pb_color_pet_types = colors_without_thumbnails.map do |color| # Infer the ideal species from the first item we can, then try to find a diff --git a/app/views/items/sources.html.haml b/app/views/items/sources.html.haml index df732572..6fd9cc71 100644 --- a/app/views/items/sources.html.haml +++ b/app/views/items/sources.html.haml @@ -64,27 +64,33 @@ %table.item-list{"data-group-type": "bundle"} %thead %td.thumbnail-cell - - if color.pb_item_thumbnail_url? + - if color&.pb_item_thumbnail_url? = image_tag color.pb_item_thumbnail_url, alt: "Item thumbnail for #{color.pb_item_name}" - - else + - elsif color = image_tag pet_type_image_url(@pb_color_pet_types[color], size: :face), srcset: ["#{pet_type_image_url(@pb_color_pet_types[color], size: :face_2x)} 2x"], alt: @pb_color_pet_types[color].human_name %th - #{color.pb_item_name || color.name.humanize} + - if color + #{color.pb_item_name || color.name.humanize} + - else + ??? New color (#{pluralize items.size, "item"}) %td.actions-cell - - if color.pb_item_name? + - if color&.pb_item_name? = button_link_to "Shops", shop_wizard_url_for(color.pb_item_name), target: "_blank", icon: search_icon = button_link_to "Trades", trading_post_url_for(color.pb_item_name), target: "_blank", icon: search_icon - - else + - elsif color .special-color-explanation Get via Lab Ray, morphing potions, etc. + - else + .special-color-explanation + We don't have info about this color yet. %tbody - items.each do |item| = render "item_list_row", item: