From 238a458131ca758aeccdf9d30817e0f4d2e26e8b Mon Sep 17 00:00:00 2001 From: Matt Dunn-Rankin Date: Tue, 16 Mar 2021 10:40:01 -0700 Subject: [PATCH] Fix body ID bug saving SwfAsset outside modeling Oops, if you saved `SwfAsset` outside of modeling code, the `item` field would be empty, and so `item.body_specific?` wouldn't happen. This would trigger when you even just report a broken image! Now, we always run the SQL query to check for that flag. --- app/models/swf_asset.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 5bd39863..8c464f46 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -209,8 +209,22 @@ class SwfAsset < ActiveRecord::Base end def body_specific? - Rails.logger.debug("my zone id is: #{zone_id}") - self.zone.type_id < 3 || (item && item.body_specific?) + self.zone.type_id < 3 || item_is_body_specific? + end + + def item_is_body_specific? + # Get items that we're already bound to in the database, and + # also the one passed to us from the current modeling operation, + # if any. + # + # NOTE: I know this has perf impact... it would be better for + # modeling to preload this probably? But oh well! + items = parent_swf_asset_relationships.includes(:parent).where(parent_type: "Item").map { |r| r.parent } + items << item if item + + # Return whether any of them is known to be body-specific. + # This ensures that we always respect the explicitly_body_specific flag! + return items.any? { |i| i.body_specific? } end def origin_pet_type=(pet_type)