Fix modeling bug where compatible_body_ids field was not updating

Ah right, the callbacks in `ParentSwfAssetRelationship` don't get
called when Rails does automatic join-model management stuff. We need
the `Item` to call its `update_cached_fields` callback itself, too!

When fixing this, I found a new bug that arose, in how we infer
`body_id` for assets that fit all pets. Fixing that next!
This commit is contained in:
Emi Matchu 2024-11-06 13:39:32 -08:00
parent a208fca8d2
commit 42e7eabdd8
3 changed files with 19 additions and 3 deletions

View file

@ -26,6 +26,8 @@ class Item < ApplicationRecord
validates_presence_of :name, :description, :thumbnail_url, :rarity, :price, validates_presence_of :name, :description, :thumbnail_url, :rarity, :price,
:zones_restrict :zones_restrict
before_validation :update_cached_fields
attr_writer :current_body_id, :owned, :wanted attr_writer :current_body_id, :owned, :wanted
NCRarities = [0, 500] NCRarities = [0, 500]
@ -265,7 +267,11 @@ class Item < ApplicationRecord
def update_cached_fields def update_cached_fields
self.cached_occupied_zone_ids = occupied_zone_ids self.cached_occupied_zone_ids = occupied_zone_ids
self.cached_compatible_body_ids = compatible_body_ids(use_cached: false) self.cached_compatible_body_ids = compatible_body_ids(use_cached: false)
self.save! end
def update_cached_fields!
update_cached_fields
save!
end end
def species_support_ids def species_support_ids

View file

@ -1,7 +1,7 @@
class ParentSwfAssetRelationship < ApplicationRecord class ParentSwfAssetRelationship < ApplicationRecord
self.table_name = 'parents_swf_assets' self.table_name = 'parents_swf_assets'
belongs_to :parent, :polymorphic => true belongs_to :parent, polymorphic: true
belongs_to :swf_asset belongs_to :swf_asset
@ -21,6 +21,6 @@ class ParentSwfAssetRelationship < ApplicationRecord
end end
def update_parent_cached_fields def update_parent_cached_fields
parent.try(:update_cached_fields) parent.try(:update_cached_fields!)
end end
end end

View file

@ -216,6 +216,7 @@ RSpec.describe Pet, type: :model do
describe "its items" do describe "its items" do
subject(:items) { pet.items } subject(:items) { pet.items }
let(:item_ids) { items.map(&:id) } let(:item_ids) { items.map(&:id) }
let(:compatible_body_ids) { items.to_h { |i| [i.id, i.compatible_body_ids] } }
they("are all new") { should all be_new_record } they("are all new") { should all be_new_record }
they("match the expected IDs") do they("match the expected IDs") do
@ -268,6 +269,14 @@ RSpec.describe Pet, type: :model do
), ),
) )
end end
they("should be marked compatible with this pet's body ID") do
pet.save!
expect(compatible_body_ids).to eq(
39552 => [47],
53874 => [47],
71706 => [0],
)
end
end end
context "its item assets" do context "its item assets" do
@ -356,6 +365,7 @@ RSpec.describe Pet, type: :model do
they("already exist") { should all be_persisted } they("already exist") { should all be_persisted }
they("are the same as before") { should eq pet.items } they("are the same as before") { should eq pet.items }
they("are not changed when saving the pet") do they("are not changed when saving the pet") do
pending("Oops, we're updating the body ID from 0 to 47!")
new_pet.save!; expect(items.map(&:previous_changes)).to all be_empty new_pet.save!; expect(items.map(&:previous_changes)).to all be_empty
end end
end end