1
0
Fork 0
forked from OpenNeo/impress

Track preferred color/species for new item page previews

Also adapted from the Impress 2020 logic!

Note that I refactored `compatible_pet_type` to a series of scopes on
`PetType`. I think this is a simpler, clearer, and more flexible API!
This commit is contained in:
Emi Matchu 2024-07-01 17:38:31 -07:00
parent fe6035d438
commit ac002f3151
3 changed files with 21 additions and 11 deletions

View file

@ -202,7 +202,12 @@ class ItemsController < ApplicationController
return load_default_preview_pet_type if color_id.nil? || species_id.nil?
PetType.find_or_initialize_by(color_id:, species_id:)
PetType.find_or_initialize_by(color_id:, species_id:).tap do |pet_type|
if pet_type.persisted?
cookies["preferred-preview-color-id"] = color_id
cookies["preferred-preview-species-id"] = species_id
end
end
end
def load_preview_pet_type
@ -214,7 +219,10 @@ class ItemsController < ApplicationController
end
def load_default_preview_pet_type
@item.compatible_pet_type
@item.compatible_pet_types.
preferring_species(cookies["preferred-preview-species-id"] || "<ignore>").
preferring_color(cookies["preferred-preview-color-id"] || "<ignore>").
preferring_simple.first
end
def validate_preview

View file

@ -498,15 +498,6 @@ class Item < ApplicationRecord
PetType.where(body_id: compatible_body_ids)
end
# Return a pet type that can wear this item, preferring simple colors and
# early-alphabetically species.
def compatible_pet_type
compatible_pet_types.joins(:color, :species).order(:species_id).
merge(Color.order(basic: :desc, standard: :desc, name: :asc)).
merge(Species.order(name: :asc)).
first
end
def handle_assets!
if @parent_swf_asset_relationships_to_update && @current_body_id
new_swf_asset_ids = @parent_swf_asset_relationships_to_update.map(&:swf_asset_id)

View file

@ -16,6 +16,17 @@ class PetType < ApplicationRecord
species = Species.find_by_name!(species_name)
where(color_id: color.id, species_id: species.id)
}
scope :preferring_species, ->(species_id) {
joins(:species).order([Arel.sql("species_id = ? DESC"), species_id])
}
scope :preferring_color, ->(color_id) {
joins(:color).order([Arel.sql("color_id = ? DESC"), color_id])
}
scope :preferring_simple, -> {
joins(:species, :color).
merge(Species.order(name: :asc)).
merge(Color.order(basic: :desc, standard: :desc, name: :asc))
}
def self.special_color_or_basic(special_color)
color_ids = special_color ? [special_color.id] : Color.basic.select([:id]).map(&:id)