Migrate away from item translations in the Your Items feature

Just replacing references to the `Item::Translation` model to the
fields on `Item` itself!
This commit is contained in:
Emi Matchu 2024-02-20 15:36:20 -08:00
parent a1066d9c8a
commit c75d988497
4 changed files with 15 additions and 28 deletions

View file

@ -235,7 +235,6 @@ class ClosetHangersController < ApplicationController
lists, lists,
hangers_scope: hangers_scope, hangers_scope: hangers_scope,
items_scope: items_scope, items_scope: items_scope,
item_translations_scope: item_translations_scope,
) )
lists.group_by(&:hangers_owned) lists.group_by(&:hangers_owned)
end end
@ -248,7 +247,6 @@ class ClosetHangersController < ApplicationController
ClosetHanger.preload_items( ClosetHanger.preload_items(
hangers, hangers,
items_scope: items_scope, items_scope: items_scope,
item_translations_scope: item_translations_scope,
) )
hangers.group_by(&:owned) hangers.group_by(&:owned)
else else
@ -261,12 +259,8 @@ class ClosetHangersController < ApplicationController
end end
def items_scope def items_scope
Item.select(:id, :thumbnail_url, :rarity_index, :is_manually_nc) Item.select(:id, :name, :description, :thumbnail_url, :rarity_index,
end :is_manually_nc)
def item_translations_scope
Item::Translation.select(:id, :item_id, :locale, :name, :description).
where(locale: I18n.locale)
end end
def owned def owned

View file

@ -51,7 +51,7 @@ class OutfitsController < ApplicationController
@species = Species.alphabetical @species = Species.alphabetical
newest_items = Item.newest. newest_items = Item.newest.
select(:id, :updated_at, :thumbnail_url, :rarity_index, :is_manually_nc). select(:id, :name, :updated_at, :thumbnail_url, :rarity_index, :is_manually_nc).
includes(:translations).limit(18) includes(:translations).limit(18)
@newest_modeled_items, @newest_unmodeled_items = @newest_modeled_items, @newest_unmodeled_items =
newest_items.partition(&:predicted_fully_modeled?) newest_items.partition(&:predicted_fully_modeled?)

View file

@ -13,9 +13,8 @@ class ClosetHanger < ApplicationRecord
validate :list_belongs_to_user validate :list_belongs_to_user
scope :alphabetical_by_item_name, -> { scope :alphabetical_by_item_name, -> {
it = Item::Translation.arel_table i = Item.arel_table
joins(:item => :translations).where(it[:locale].eq(I18n.locale)). joins(:item).order(i[:name].asc)
order(it[:name].asc)
} }
scope :trading, -> { scope :trading, -> {
ch = arel_table ch = arel_table
@ -86,28 +85,24 @@ class ClosetHanger < ApplicationRecord
base base
end end
# TODO: Is the performance improvement on this actually much better than just
# `includes`, now that `Item::Translation` records aren't part of it anymore?
def self.preload_items( def self.preload_items(
hangers, hangers,
items_scope: Item.all, items_scope: Item.all
item_translations_scope: Item::Translation.all
) )
# Preload the records we need. (This is like `includes`, but `includes` # Preload the records we need. (This is like `includes`, but `includes`
# always selects all fields for all records, and we give the caller the # always selects all fields for all records, and we give the caller the
# opportunity to specify which fields it actually wants via scope!) # opportunity to specify which fields it actually wants via scope!)
items = items_scope.where(id: hangers.map(&:item_id)) items = items_scope.where(id: hangers.map(&:item_id))
translations = item_translations_scope.where(item_id: items.map(&:id))
# Group the records by relevant IDs. # Group the records by relevant IDs.
translations_by_item_id = translations.group_by(&:item_id)
items_by_id = items.to_h { |i| [i.id, i] } items_by_id = items.to_h { |i| [i.id, i] }
# Assign the preloaded records to the records they belong to. (This is like # Assign the preloaded records to the records they belong to. (This is like
# doing e.g. i.translations = ..., but that's a database write - we # doing e.g. h.item = ..., but that's a database write - we actually just
# actually just want to set the `translations` field itself directly! # want to set the `item` field itself directly! Hacky, ripped from how
# Hacky, ripped from how `ActiveRecord::Associations::Preloader` does it!) # `ActiveRecord::Associations::Preloader` does it!)
items.each do |item|
item.association(:translations).target = translations_by_item_id[item.id]
end
hangers.each do |hanger| hangers.each do |hanger|
hanger.association(:item).target = items_by_id[hanger.item_id] hanger.association(:item).target = items_by_id[hanger.item_id]
end end

View file

@ -45,8 +45,7 @@ class ClosetList < ApplicationRecord
def self.preload_items( def self.preload_items(
lists, lists,
hangers_scope: ClosetHanger.all, hangers_scope: ClosetHanger.all,
items_scope: Item.all, items_scope: Item.all
item_translations_scope: Item::Translation.all
) )
# Preload the records we need. (This is like `includes`, but `includes` # Preload the records we need. (This is like `includes`, but `includes`
# always selects all fields for all records, and we give the caller the # always selects all fields for all records, and we give the caller the
@ -57,9 +56,9 @@ class ClosetList < ApplicationRecord
hangers_by_list_id = hangers.group_by(&:list_id) hangers_by_list_id = hangers.group_by(&:list_id)
# Assign the preloaded records to the records they belong to. (This is like # Assign the preloaded records to the records they belong to. (This is like
# doing e.g. i.translations = ..., but that's a database write - we # doing e.g. h.item = ..., but that's a database write - we actually just
# actually just want to set the `translations` field itself directly! # want to set the `item` field itself directly! Hacky, ripped from how
# Hacky, ripped from how `ActiveRecord::Associations::Preloader` does it!) # `ActiveRecord::Associations::Preloader` does it!)
lists.each do |list| lists.each do |list|
list.association(:hangers).target = hangers_by_list_id[list.id] list.association(:hangers).target = hangers_by_list_id[list.id]
end end
@ -68,7 +67,6 @@ class ClosetList < ApplicationRecord
ClosetHanger.preload_items( ClosetHanger.preload_items(
hangers, hangers,
items_scope: items_scope, items_scope: items_scope,
item_translations_scope: item_translations_scope,
) )
end end