From a1066d9c8a88ab3734d3f9beed26cbda0994e33b Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 20 Feb 2024 15:25:03 -0800 Subject: [PATCH] Add translated item fields directly to the Item model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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! --- app/models/item.rb | 25 +++++++++++++++++- ...add_translated_fields_directly_to_items.rb | 26 +++++++++++++++++++ db/schema.rb | 5 +++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240220230420_add_translated_fields_directly_to_items.rb diff --git a/app/models/item.rb b/app/models/item.rb index 91e300d3..ac3759a9 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -6,7 +6,12 @@ class Item < ApplicationRecord 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_one :contribution, :as => :contributed, :inverse_of => :contributed @@ -122,6 +127,24 @@ class Item < ApplicationRecord 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 return nil unless nc? begin diff --git a/db/migrate/20240220230420_add_translated_fields_directly_to_items.rb b/db/migrate/20240220230420_add_translated_fields_directly_to_items.rb new file mode 100644 index 00000000..5f7348f3 --- /dev/null +++ b/db/migrate/20240220230420_add_translated_fields_directly_to_items.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 3adc0b9d..e54e690d 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_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| t.integer "species_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.column "modeling_status_hint", "enum('done','glitchy')" 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"], name: "items_modeling_status_hint_and_created_at" t.index ["modeling_status_hint", "id"], name: "items_modeling_status_hint_and_id"