Refactor appearances data to use a "body" object

This is more similar to what impress-2020 does, I was working on the
wardrobe-2020 code and took some inspiration!

The body has an ID and a species, or is the string "all".
This commit is contained in:
Emi Matchu 2023-11-11 07:54:56 -08:00
parent 6c31094c2d
commit 133895b213
2 changed files with 13 additions and 4 deletions

View file

@ -512,7 +512,8 @@ class Item < ApplicationRecord
@parent_swf_asset_relationships_to_update = rels @parent_swf_asset_relationships_to_update = rels
end end
Appearance = Struct.new(:body_id, :swf_assets) Body = Struct.new(:id, :species)
Appearance = Struct.new(:body, :swf_assets)
def appearances def appearances
all_swf_assets = swf_assets.to_a all_swf_assets = swf_assets.to_a
@ -525,7 +526,7 @@ class Item < ApplicationRecord
# If there are no body-specific assets, return one appearance for them all. # If there are no body-specific assets, return one appearance for them all.
if swf_assets_by_body_id.empty? if swf_assets_by_body_id.empty?
return Appearance.new(0, swf_assets_for_all_bodies) return Appearance.new(:all, swf_assets_for_all_bodies)
end end
# Otherwise, create an appearance for each real (nonzero) body ID. We don't # Otherwise, create an appearance for each real (nonzero) body ID. We don't
@ -533,7 +534,9 @@ class Item < ApplicationRecord
# uhh, let's merge the body_id = 0 ones in? # uhh, let's merge the body_id = 0 ones in?
swf_assets_by_body_id.map do |body_id, body_specific_assets| swf_assets_by_body_id.map do |body_id, body_specific_assets|
swf_assets_for_body = body_specific_assets + swf_assets_for_all_bodies swf_assets_for_body = body_specific_assets + swf_assets_for_all_bodies
Appearance.new(body_id, swf_assets_for_body) species = Species.with_body_id(body_id).first!
body = Body.new(body_id, species)
Appearance.new(body, swf_assets_for_body)
end end
end end

View file

@ -1,5 +1,6 @@
class Species < ApplicationRecord class Species < ApplicationRecord
translates :name translates :name
has_many :pet_types
scope :alphabetical, -> { scope :alphabetical, -> {
st = Species::Translation.arel_table st = Species::Translation.arel_table
@ -12,6 +13,11 @@ class Species < ApplicationRecord
where(st[:name].matches(sanitize_sql_like(name))) where(st[:name].matches(sanitize_sql_like(name)))
} }
scope :with_body_id, -> body_id {
pt = PetType.arel_table
joins(:pet_types).where(pt[:body_id].eq(body_id)).limit(1)
}
# TODO: Should we consider replacing this at call sites? This used to be # TODO: Should we consider replacing this at call sites? This used to be
# built into the globalize gem but isn't anymore! # built into the globalize gem but isn't anymore!
def self.find_by_name(name) def self.find_by_name(name)
@ -19,7 +25,7 @@ class Species < ApplicationRecord
end end
def as_json(options={}) def as_json(options={})
{:id => id, :name => human_name} super({only: [:id, :name], methods: [:human_name]}.merge(options))
end end
def human_name def human_name