2024-05-29 18:36:30 -07:00
|
|
|
require "addressable/template"
|
|
|
|
|
2024-01-24 03:25:23 -08:00
|
|
|
class AltStyle < ApplicationRecord
|
|
|
|
belongs_to :species
|
|
|
|
belongs_to :color
|
|
|
|
|
2024-10-18 17:06:13 -07:00
|
|
|
has_many :parent_swf_asset_relationships, as: :parent, dependent: :destroy
|
2024-01-24 03:25:23 -08:00
|
|
|
has_many :swf_assets, through: :parent_swf_asset_relationships
|
2024-01-24 03:54:43 -08:00
|
|
|
has_many :contributions, as: :contributed, inverse_of: :contributed
|
|
|
|
|
2024-05-29 18:42:41 -07:00
|
|
|
validates :body_id, presence: true
|
2024-09-30 17:21:45 -07:00
|
|
|
validates :series_name, presence: true, allow_nil: true
|
|
|
|
validates :thumbnail_url, presence: true
|
2024-05-29 18:42:41 -07:00
|
|
|
|
2024-10-18 17:04:26 -07:00
|
|
|
before_validation :infer_thumbnail_url, unless: :thumbnail_url?
|
2024-05-29 18:46:17 -07:00
|
|
|
|
2024-02-27 15:33:08 -08:00
|
|
|
scope :matching_name, ->(series_name, color_name, species_name) {
|
|
|
|
color = Color.find_by_name!(color_name)
|
|
|
|
species = Species.find_by_name!(species_name)
|
|
|
|
where(series_name:, color_id: color.id, species_id: species.id)
|
|
|
|
}
|
2024-09-30 17:35:18 -07:00
|
|
|
scope :by_creation_date, -> {
|
Fix bug sorting pet styles by creation date
Before this change, the sort order when searching for
"Prismatic Pine: Nostalgic" showed:
- [Added Dec 18, 2024] Prismatic Pine: Nostalgic Christmas Flotsam
- [Added Dec 19, 2024] Prismatic Pine: Nostalgic Christmas Gelert
- [Added Dec 18, 2024] Prismatic Pine: Nostalgic Christmas Bruce
- [Added Dec 17, 2024] Prismatic Pine: Nostalgic Christmas Scorchio
- <more>
This is because the Gelert was created at 11:37 NST on Dec 19, whereas
the Flotsam was created at 18:11 NST on Dec 18—but in UTC, which is how
timestamps are stored in the database, these are both Dec 19, so the
Flotsam was sorting first alphabetically.
In this change, we do a hacky transform from UTC to NST-ish. I didn't
want to set up the deploy process to pull named time zones into MySQL,
and then have this as a potential gotcha for the dev environment
later—so instead, I pretend `-08:00` is a good-enough specification of
NST.
This will sometimes create slightly incorrect sort ordering when it
*is* Daylight Savings, and a record was created around midnight. I'm
okay with that!
2024-12-29 16:11:00 -08:00
|
|
|
# HACK: Setting up named time zones in MySQL takes effort, so we assume
|
|
|
|
# it's not Daylight Savings. This will produce slightly incorrect
|
|
|
|
# sorting when it *is* Daylight Savings, and records happen to be
|
|
|
|
# created around midnight.
|
|
|
|
order(Arel.sql("DATE(CONVERT_TZ(created_at, '+00:00', '-08:00')) DESC"))
|
2024-09-30 17:35:18 -07:00
|
|
|
}
|
2024-10-22 16:39:25 -07:00
|
|
|
scope :unlabeled, -> { where(series_name: nil) }
|
|
|
|
scope :newest, -> { order(created_at: :desc) }
|
2024-02-27 15:33:08 -08:00
|
|
|
|
2024-09-30 17:41:21 -07:00
|
|
|
def pet_name
|
2024-01-24 03:54:43 -08:00
|
|
|
I18n.translate('pet_types.human_name', color_human_name: color.human_name,
|
|
|
|
species_human_name: species.human_name)
|
|
|
|
end
|
2024-01-24 03:25:23 -08:00
|
|
|
|
2024-09-30 17:41:21 -07:00
|
|
|
alias_method :name, :pet_name
|
|
|
|
|
2024-02-27 15:48:28 -08:00
|
|
|
# If the series_name hasn't yet been set manually by support staff, show the
|
|
|
|
# string "<New?>" instead. But it won't be searchable by that string—that is,
|
|
|
|
# `fits:<New?>-faerie-draik` intentionally will not work, and the canonical
|
|
|
|
# filter name will be `fits:alt-style-IDNUMBER`, instead.
|
|
|
|
def series_name
|
2024-11-30 10:33:58 -08:00
|
|
|
real_series_name || AltStyle.placeholder_name
|
2024-10-18 17:13:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def real_series_name=(new_series_name)
|
|
|
|
self[:series_name] = new_series_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def real_series_name
|
|
|
|
self[:series_name]
|
2024-02-27 15:48:28 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# You can use this to check whether `series_name` is returning the actual
|
|
|
|
# value or its placeholder value.
|
2024-10-18 17:13:16 -07:00
|
|
|
def real_series_name?
|
|
|
|
real_series_name.present?
|
2024-02-27 15:48:28 -08:00
|
|
|
end
|
|
|
|
|
2024-01-29 03:20:48 -08:00
|
|
|
def adjective_name
|
2024-01-30 05:54:20 -08:00
|
|
|
"#{series_name} #{color.human_name}"
|
2024-01-29 03:20:48 -08:00
|
|
|
end
|
|
|
|
|
2024-09-30 17:21:45 -07:00
|
|
|
def full_name
|
|
|
|
"#{series_name} #{name}"
|
|
|
|
end
|
|
|
|
|
2024-11-15 20:04:45 -08:00
|
|
|
EMPTY_IMAGE_URL = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
|
2024-01-24 06:53:37 -08:00
|
|
|
def preview_image_url
|
2024-11-15 20:04:45 -08:00
|
|
|
# Use the image URL for the first asset. Or, fall back to an empty image.
|
|
|
|
swf_assets.first&.image_url || EMPTY_IMAGE_URL
|
2024-01-24 06:53:37 -08:00
|
|
|
end
|
|
|
|
|
2024-07-01 16:54:39 -07:00
|
|
|
# Given a list of items, return how they look on this alt style.
|
|
|
|
def appearances_for(items, ...)
|
|
|
|
Item.appearances_for(items, self, ...)
|
2024-02-27 16:11:06 -08:00
|
|
|
end
|
|
|
|
|
Make thumbnail_url a manually overridable field for Alt Styles
Oh jeez, okay, the latest batch of Alt Styles are using a different URL
format for the thumbnail image!
This isn't something we can import via modeling, so we're gonna have to
keep on top of it manually. For now, I'll keep inferring the previous
format in case they keep using it, but here's also a console script to
fix up the latest batch. (At time of writing, not all of these are in
our database, which is fine; when pasting it into the console, those
lines will error and the script will continue.)
```rb
def update_style(color_name, species_name, thumbnail_url)
AltStyle.find_by_color_id_and_species_id(
Color.find_by_name(color_name),
Species.find_by_name(species_name)
).update!(thumbnail_url:)
end
update_style "Grey", "Blumaroo", "https://images.neopets.com/items/c0gk16fk.gif"
update_style "Grey", "Bori", "https://images.neopets.com/items/55qvx6mr.gif"
update_style "Grey", "Bruce", "https://images.neopets.com/items/6y6pyiuw.gif"
update_style "Grey", "Buzz", "https://images.neopets.com/items/7fh4avry.gif"
update_style "Grey", "Chia", "https://images.neopets.com/items/7b2jtn10.gif"
update_style "Grey", "Elephante", "https://images.neopets.com/items/0ne41rao.gif"
update_style "Grey", "Gnorbu", "https://images.neopets.com/items/75mwtqmh.gif"
update_style "Grey", "Hissi", "https://images.neopets.com/items/dxdi2mhm.gif"
update_style "Grey", "Kiko", "https://images.neopets.com/items/b9yiruxt.gif"
update_style "Grey", "Lenny", "https://images.neopets.com/items/c6cboc7e.gif"
update_style "Grey", "Lutari", "https://images.neopets.com/items/33fs4eqf.gif"
update_style "Grey", "Nimmo", "https://images.neopets.com/items/4karmgbl.gif"
update_style "Grey", "Ogrin", "https://images.neopets.com/items/dlw78fhk.gif"
update_style "Grey", "Quiggle", "https://images.neopets.com/items/0aipl0iw.gif"
update_style "Grey", "Ruki", "https://images.neopets.com/items/bjnjxsem.gif"
update_style "Grey", "Tuskaninny", "https://images.neopets.com/items/7rh57a0o.gif"
update_style "Grey", "Vandagyre", "https://images.neopets.com/items/6p8sgs69.gif"
update_style "Grey", "Xweetok", "https://images.neopets.com/items/bge9vp5e.gif"
```
2024-06-15 17:35:12 -07:00
|
|
|
# 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
|
|
|
|
# new Alt Style, but the database field can be manually overridden as needed!
|
|
|
|
THUMBNAIL_URL_TEMPLATE = Addressable::Template.new(
|
|
|
|
"https://images.neopets.com/items/{series}_{color}_{species}.gif"
|
|
|
|
)
|
|
|
|
DEFAULT_THUMBNAIL_URL = "https://images.neopets.com/items/mall_bg_circle.gif"
|
|
|
|
def infer_thumbnail_url
|
2024-10-18 19:01:26 -07:00
|
|
|
if real_series_name?
|
Make thumbnail_url a manually overridable field for Alt Styles
Oh jeez, okay, the latest batch of Alt Styles are using a different URL
format for the thumbnail image!
This isn't something we can import via modeling, so we're gonna have to
keep on top of it manually. For now, I'll keep inferring the previous
format in case they keep using it, but here's also a console script to
fix up the latest batch. (At time of writing, not all of these are in
our database, which is fine; when pasting it into the console, those
lines will error and the script will continue.)
```rb
def update_style(color_name, species_name, thumbnail_url)
AltStyle.find_by_color_id_and_species_id(
Color.find_by_name(color_name),
Species.find_by_name(species_name)
).update!(thumbnail_url:)
end
update_style "Grey", "Blumaroo", "https://images.neopets.com/items/c0gk16fk.gif"
update_style "Grey", "Bori", "https://images.neopets.com/items/55qvx6mr.gif"
update_style "Grey", "Bruce", "https://images.neopets.com/items/6y6pyiuw.gif"
update_style "Grey", "Buzz", "https://images.neopets.com/items/7fh4avry.gif"
update_style "Grey", "Chia", "https://images.neopets.com/items/7b2jtn10.gif"
update_style "Grey", "Elephante", "https://images.neopets.com/items/0ne41rao.gif"
update_style "Grey", "Gnorbu", "https://images.neopets.com/items/75mwtqmh.gif"
update_style "Grey", "Hissi", "https://images.neopets.com/items/dxdi2mhm.gif"
update_style "Grey", "Kiko", "https://images.neopets.com/items/b9yiruxt.gif"
update_style "Grey", "Lenny", "https://images.neopets.com/items/c6cboc7e.gif"
update_style "Grey", "Lutari", "https://images.neopets.com/items/33fs4eqf.gif"
update_style "Grey", "Nimmo", "https://images.neopets.com/items/4karmgbl.gif"
update_style "Grey", "Ogrin", "https://images.neopets.com/items/dlw78fhk.gif"
update_style "Grey", "Quiggle", "https://images.neopets.com/items/0aipl0iw.gif"
update_style "Grey", "Ruki", "https://images.neopets.com/items/bjnjxsem.gif"
update_style "Grey", "Tuskaninny", "https://images.neopets.com/items/7rh57a0o.gif"
update_style "Grey", "Vandagyre", "https://images.neopets.com/items/6p8sgs69.gif"
update_style "Grey", "Xweetok", "https://images.neopets.com/items/bge9vp5e.gif"
```
2024-06-15 17:35:12 -07:00
|
|
|
self.thumbnail_url = THUMBNAIL_URL_TEMPLATE.expand(
|
|
|
|
series: series_name.gsub(/\s+/, '').downcase,
|
|
|
|
color: color.name.gsub(/\s+/, '').downcase,
|
|
|
|
species: species.name.gsub(/\s+/, '').downcase,
|
|
|
|
).to_s
|
|
|
|
else
|
|
|
|
self.thumbnail_url = DEFAULT_THUMBNAIL_URL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-14 19:03:44 -08:00
|
|
|
def real_thumbnail_url?
|
|
|
|
thumbnail_url != DEFAULT_THUMBNAIL_URL
|
|
|
|
end
|
|
|
|
|
2024-11-30 10:33:58 -08:00
|
|
|
def self.placeholder_name
|
|
|
|
"<New?>"
|
|
|
|
end
|
|
|
|
|
2024-12-29 15:52:50 -08:00
|
|
|
def self.all_series_names
|
|
|
|
distinct.where.not(series_name: nil).pluck(:series_name).sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all_supported_colors
|
|
|
|
Color.find(distinct.pluck(:color_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all_supported_species
|
|
|
|
Species.find(distinct.pluck(:species_id))
|
|
|
|
end
|
|
|
|
|
2024-01-24 06:12:03 -08:00
|
|
|
# For convenience in the console!
|
|
|
|
def self.find_by_name(color_name, species_name)
|
|
|
|
color = Color.find_by_name(color_name)
|
|
|
|
species = Species.find_by_name(species_name)
|
|
|
|
where(color_id: color, species_id: species).first
|
|
|
|
end
|
2024-01-24 03:25:23 -08:00
|
|
|
end
|