Add translated item fields directly to the Item model

Like with Species, Color, and Zone, we're moving the translation data
directly onto the model, and just using English. This will simplify
some of our queries a lot (way fewer joins!), and it's what Neopets
does now anyway, and I have a secret hope that removing the complexity
along the codepath for `item.name` might help speed up large item lists
if we're lucky?? 🤞

Anyway, this is the first step, performing the migration to copy the
data onto the `items` table, making sure to keep them in sync for the
2020 app for now!
This commit is contained in:
Emi Matchu 2024-02-20 15:25:03 -08:00
parent c215ebee09
commit a1066d9c8a
3 changed files with 54 additions and 2 deletions

View file

@ -6,7 +6,12 @@ class Item < ApplicationRecord
SwfAssetType = 'object' SwfAssetType = 'object'
translates :name, :description, :rarity # Keep the reference to the deprecated `Item::Translation` record, but don't
# bind it directly to any attributes anymore. We have some temporary writers
# that hack around the API to keep the attributes synced, while no longer
# reading *from* them by default.
# TODO: Remove once we're all done with translations, both here and in 2020!
translates
has_many :closet_hangers has_many :closet_hangers
has_one :contribution, :as => :contributed, :inverse_of => :contributed has_one :contribution, :as => :contributed, :inverse_of => :contributed
@ -122,6 +127,24 @@ class Item < ApplicationRecord
distinct distinct
} }
# Temporary writers to keep the English translation record updated, while
# primarily using the attributes on the model itself.
#
# Once this app and DTI 2020 are both comfortably off the translation system,
# we can remove this!
def name=(new_name)
globalize.write(:en, :name, new_name)
write_attribute(:name, new_name)
end
def description=(new_description)
globalize.write(:en, :description, new_description)
write_attribute(:description, new_description)
end
def rarity=(new_rarity)
globalize.write(:en, :rarity, new_rarity)
write_attribute(:rarity, new_rarity)
end
def nc_trade_value def nc_trade_value
return nil unless nc? return nil unless nc?
begin begin

View file

@ -0,0 +1,26 @@
class AddTranslatedFieldsDirectlyToItems < ActiveRecord::Migration[7.1]
def change
add_column :items, :name, :string, null: false
add_column :items, :description, :text, null: false, default: ""
add_column :items, :rarity, :string, null: false, default: ""
reversible do |direction|
direction.up do
total_count = Item.count
saved_count = 0
Item.includes(:translations).find_in_batches do |items|
Item.transaction do
items.each do |item|
item.name = item.translation_for(:en).name
item.description = item.translation_for(:en).description || ""
item.rarity = item.translation_for(:en).rarity || ""
item.save!
end
saved_count += items.size
puts "Saved #{saved_count} of #{total_count} items"
end
end
end
end
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_02_03_161355) do ActiveRecord::Schema[7.1].define(version: 2024_02_20_230420) do
create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.integer "species_id", null: false t.integer "species_id", null: false
t.integer "color_id", null: false t.integer "color_id", null: false
@ -144,6 +144,9 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_03_161355) do
t.integer "manual_special_color_id" t.integer "manual_special_color_id"
t.column "modeling_status_hint", "enum('done','glitchy')" t.column "modeling_status_hint", "enum('done','glitchy')"
t.boolean "is_manually_nc", default: false, null: false t.boolean "is_manually_nc", default: false, null: false
t.string "name", null: false
t.text "description", default: "", null: false
t.string "rarity", default: "", null: false
t.index ["modeling_status_hint", "created_at", "id"], name: "items_modeling_status_hint_and_created_at_and_id" t.index ["modeling_status_hint", "created_at", "id"], name: "items_modeling_status_hint_and_created_at_and_id"
t.index ["modeling_status_hint", "created_at"], name: "items_modeling_status_hint_and_created_at" t.index ["modeling_status_hint", "created_at"], name: "items_modeling_status_hint_and_created_at"
t.index ["modeling_status_hint", "id"], name: "items_modeling_status_hint_and_id" t.index ["modeling_status_hint", "id"], name: "items_modeling_status_hint_and_id"