1
0
Fork 0
forked from OpenNeo/impress

Use our IntegerSet serializer for PetState#swf_asset_ids

This commit is contained in:
Emi Matchu 2024-11-03 12:16:27 -08:00
parent 43717e2535
commit 242b85470d
2 changed files with 11 additions and 17 deletions

View file

@ -91,7 +91,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

View file

@ -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.