From 52ca41dbff4c08165012afa774677c938bdfcaf3 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 3 Nov 2024 11:46:29 -0800 Subject: [PATCH] Extract biology processing from AltStyle into Pet I'm trying to pull more of the modeling code out of the individual classes, and into this encapsulated preprocessing, so it's a lot more in-one-place! --- app/models/alt_style.rb | 9 --------- app/models/pet.rb | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 0e9a47bf..b822a2dd 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -74,15 +74,6 @@ class AltStyle < ApplicationRecord Item.appearances_for(items, self, ...) end - def biology=(biology) - # TODO: This is very similar to what `PetState` does, but like… much much - # more compact? Idk if I'm missing something, or if I was just that much - # more clueless back when I wrote it, lol 😅 - self.swf_assets = biology.values.map do |asset_data| - SwfAsset.from_biology_data(self.body_id, asset_data) - end - end - # At time of writing, most batches of Alt Styles thumbnails used a simple # pattern for the item thumbnail URL, but that's not always the case anymore. # For now, let's keep using this format as the default value when creating a diff --git a/app/models/pet.rb b/app/models/pet.rb index c2ee7e51..189794bf 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -110,9 +110,7 @@ class Pet < ApplicationRecord def alt_style @alt_style ||= begin return nil unless @custom_pet[:alt_style] - raise UnexpectedDataFormat unless @custom_pet[:alt_color] - raise UnexpectedDataFormat if @custom_pet[:biology_by_zone].empty? id = @custom_pet[:alt_style].to_i AltStyle.find_or_initialize_by(id:).tap do |alt_style| @@ -120,7 +118,7 @@ class Pet < ApplicationRecord color_id: @custom_pet[:alt_color].to_i, species_id: @custom_pet[:species_id].to_i, body_id: @custom_pet[:body_id].to_i, - biology: @custom_pet[:biology_by_zone], + swf_assets: alt_style_assets, ) end end @@ -139,12 +137,20 @@ class Pet < ApplicationRecord biology = @custom_pet[:alt_style].present? ? @custom_pet[:original_biology] : @custom_pet[:biology_by_zone] - raise UnexpectedDataFormat if biology.empty? - - body_id = @custom_pet[:body_id].to_i - biology.values.map { |b| SwfAsset.from_biology_data(body_id, b) } + assets_from_biology(biology) end end + + def alt_style_assets + raise UnexpectedDataFormat if @custom_pet[:biology_by_zone].empty? + assets_from_biology(@custom_pet[:biology_by_zone]) + end + + def assets_from_biology(biology) + raise UnexpectedDataFormat if biology.empty? + body_id = @custom_pet[:body_id].to_i + biology.values.map { |b| SwfAsset.from_biology_data(body_id, b) } + end end end