From cd28c26ae72a1fa1d25c0133b0e169f6f9b7d6a0 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sat, 15 Jun 2024 17:35:12 -0700 Subject: [PATCH] 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" ``` --- app/models/alt_style.rb | 37 +++++++++++-------- ...6001002_add_thumbnail_url_to_alt_styles.rb | 14 +++++++ db/schema.rb | 3 +- 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20240616001002_add_thumbnail_url_to_alt_styles.rb diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 5eccdee1..53d958b6 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -11,6 +11,7 @@ class AltStyle < ApplicationRecord validates :body_id, presence: true before_create :infer_series_name + before_create :infer_thumbnail_url scope :matching_name, ->(series_name, color_name, species_name) { color = Color.find_by_name!(color_name) @@ -41,22 +42,6 @@ class AltStyle < ApplicationRecord "#{series_name} #{color.human_name}" end - 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 thumbnail_url - return DEFAULT_THUMBNAIL_URL unless has_real_series_name? - - # HACK: We're assuming this is the format long-term! But if it changes, we - # may need to add it as a database field instead. - THUMBNAIL_URL_TEMPLATE.expand( - series: series_name.gsub(/\s+/, '').downcase, - color: color.name.gsub(/\s+/, '').downcase, - species: species.name.gsub(/\s+/, '').downcase, - ).to_s - end - def preview_image_url swf_asset = swf_assets.first return nil if swf_asset.nil? @@ -91,6 +76,26 @@ class AltStyle < ApplicationRecord 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 + # 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 + if has_real_series_name? + 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 + # For convenience in the console! def self.find_by_name(color_name, species_name) color = Color.find_by_name(color_name) diff --git a/db/migrate/20240616001002_add_thumbnail_url_to_alt_styles.rb b/db/migrate/20240616001002_add_thumbnail_url_to_alt_styles.rb new file mode 100644 index 00000000..ea8ffc80 --- /dev/null +++ b/db/migrate/20240616001002_add_thumbnail_url_to_alt_styles.rb @@ -0,0 +1,14 @@ +class AddThumbnailUrlToAltStyles < ActiveRecord::Migration[7.1] + def change + add_column :alt_styles, :thumbnail_url, :string, null: false + + reversible do |direction| + direction.up do + AltStyle.find_each do |alt_style| + alt_style.infer_thumbnail_url + alt_style.save! + end + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f071ff9a..2fd7b016 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_06_08_022149) do +ActiveRecord::Schema[7.1].define(version: 2024_06_16_001002) do create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_520_ci", force: :cascade do |t| t.integer "species_id", null: false t.integer "color_id", null: false @@ -18,6 +18,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_08_022149) do t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.string "series_name" + t.string "thumbnail_url", null: false t.index ["color_id"], name: "index_alt_styles_on_color_id" t.index ["species_id"], name: "index_alt_styles_on_species_id" end