diff --git a/app/models/item.rb b/app/models/item.rb index 653cdaae..c58f2074 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -26,6 +26,8 @@ class Item < ApplicationRecord validates_presence_of :name, :description, :thumbnail_url, :rarity, :price, :zones_restrict + before_validation :update_cached_fields + attr_writer :current_body_id, :owned, :wanted NCRarities = [0, 500] @@ -265,7 +267,11 @@ class Item < ApplicationRecord def update_cached_fields self.cached_occupied_zone_ids = occupied_zone_ids self.cached_compatible_body_ids = compatible_body_ids(use_cached: false) - self.save! + end + + def update_cached_fields! + update_cached_fields + save! end def species_support_ids diff --git a/app/models/parent_swf_asset_relationship.rb b/app/models/parent_swf_asset_relationship.rb index 6f24bfad..71d8b0cc 100644 --- a/app/models/parent_swf_asset_relationship.rb +++ b/app/models/parent_swf_asset_relationship.rb @@ -1,7 +1,7 @@ class ParentSwfAssetRelationship < ApplicationRecord self.table_name = 'parents_swf_assets' - belongs_to :parent, :polymorphic => true + belongs_to :parent, polymorphic: true belongs_to :swf_asset @@ -21,6 +21,6 @@ class ParentSwfAssetRelationship < ApplicationRecord end def update_parent_cached_fields - parent.try(:update_cached_fields) + parent.try(:update_cached_fields!) end end diff --git a/spec/models/pet_spec.rb b/spec/models/pet_spec.rb index 53e35f83..15473982 100644 --- a/spec/models/pet_spec.rb +++ b/spec/models/pet_spec.rb @@ -216,6 +216,7 @@ RSpec.describe Pet, type: :model do describe "its items" do subject(:items) { pet.items } 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("match the expected IDs") do @@ -268,6 +269,14 @@ RSpec.describe Pet, type: :model do ), ) 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 context "its item assets" do @@ -356,6 +365,7 @@ RSpec.describe Pet, type: :model do they("already exist") { should all be_persisted } they("are the same as before") { should eq pet.items } 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 end end