From 540ce08caa54827adfa964961c5e511e233b62b5 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Mon, 21 Oct 2024 14:24:45 -0700 Subject: [PATCH] Handle invalid Item state a bit better Catch missing fields in validation before sending it to the DB, and skip the Dyeworks stuff if the name is missing. I ran into this looking into `test/trade_activity_test.rb`, which fails right now because we try to create a boring placeholder item with minimal fields, which Dyeworks can't call `name.match()` on! Now, the test fails with a more helpful error about the item being invalid. Next, I'll fix that! --- app/models/item.rb | 2 ++ app/models/item/dyeworks.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/item.rb b/app/models/item.rb index cef09f8d..8be75718 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -23,6 +23,8 @@ class Item < ApplicationRecord has_many :dyeworks_variants, class_name: "Item", inverse_of: :dyeworks_base_item + validates_presence_of :name, :description, :thumbnail_url, :rarity, :price, + :zones_restrict attr_writer :current_body_id, :owned, :wanted diff --git a/app/models/item/dyeworks.rb b/app/models/item/dyeworks.rb index 06a07109..79a7b9de 100644 --- a/app/models/item/dyeworks.rb +++ b/app/models/item/dyeworks.rb @@ -117,7 +117,7 @@ class Item )\z }x def inferred_dyeworks_base_item - name_match = name.match(DYEWORKS_NAME_PATTERN) + name_match = (name || "").match(DYEWORKS_NAME_PATTERN) return nil if name_match.nil? Item.find_by_name(name_match["base"])