From 8f9daf4d52b3a8347a7e14652aee98f20ad73393 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 3 Nov 2024 12:16:27 -0800 Subject: [PATCH] Reapply "Use our IntegerSet serializer for PetState#swf_asset_ids" (cherry picked from commit 242b85470d1e5e161f2a49c16faaad38f052b74f) --- app/models/pet.rb | 2 +- app/models/pet_state.rb | 26 ++++++++++---------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/models/pet.rb b/app/models/pet.rb index a2f2796a..7efa7cdb 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -103,7 +103,7 @@ class Pet < ApplicationRecord def pet_state @pet_state ||= begin - swf_asset_ids = biology_assets.map(&:remote_id).sort.join(",") + swf_asset_ids = biology_assets.map(&:remote_id) pet_type.pet_states.find_or_initialize_by(swf_asset_ids:).tap do |pet_state| pet_state.swf_assets = biology_assets end diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index b59f9397..f3e33f9f 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -9,6 +9,8 @@ class PetState < ApplicationRecord has_many :parent_swf_asset_relationships, :as => :parent has_many :swf_assets, :through => :parent_swf_asset_relationships + serialize :swf_asset_ids, coder: Serializers::IntegerSet, type: Array + belongs_to :pet_type delegate :species_id, :species, :color_id, :color, to: :pet_type @@ -92,26 +94,18 @@ class PetState < ApplicationRecord end end - def sort_swf_asset_ids! - self.swf_asset_ids = swf_asset_ids_array.sort.join(',') - end - - def swf_asset_ids - self['swf_asset_ids'] - end - - def swf_asset_ids_array - swf_asset_ids.split(',').map(&:to_i) - end - - def swf_asset_ids=(ids) - self['swf_asset_ids'] = ids - end - def to_param "#{id}-#{pose.split('_').map(&:capitalize).join('-')}" end + # Because our column is named `swf_asset_ids`, we need to ensure writes to + # it go to the attribute, and not the thing ActiveRecord does of finding the + # relevant `swf_assets`. + # TODO: Consider renaming the column to `cached_swf_asset_ids`? + def swf_asset_ids=(new_swf_asset_ids) + write_attribute(:swf_asset_ids, new_swf_asset_ids) + end + private # A helper for the `pose=` method.