From 3ac9e7ce6994352171bdbd26f2a0e7fb91c0dedd Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 20 Feb 2024 16:04:41 -0800 Subject: [PATCH] Migrate item search away from item translations Lightning fast for simple name queries now, gotta say!! --- app/controllers/items_controller.rb | 9 ++++----- app/models/item.rb | 27 ++++++--------------------- app/models/item/search/query.rb | 3 +-- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index c28fd8a6..6b19bc6d 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -11,8 +11,8 @@ class ItemsController < ApplicationController else per_page = 30 end - @items = @query.results.includes(:translations). - paginate(page: params[:page], per_page: per_page) + @items = @query.results.paginate( + page: params[:page], per_page: per_page) assign_closeted! respond_to do |format| format.html { @@ -44,7 +44,7 @@ class ItemsController < ApplicationController respond_to do |format| format.html { @campaign = Fundraising::Campaign.current rescue nil - @newest_items = Item.newest.includes(:translations).limit(18) + @newest_items = Item.newest.limit(18) } format.js { render json: {error: '$q required'}} end @@ -86,8 +86,7 @@ class ItemsController < ApplicationController raise ActiveRecord::RecordNotFound, 'Pet type not found' end - @items = @pet_type.needed_items.includes(:translations). - alphabetize_by_translations + @items = @pet_type.needed_items.order(:name) assign_closeted! respond_to do |format| diff --git a/app/models/item.rb b/app/models/item.rb index 71945e98..ae6560f8 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -28,13 +28,6 @@ class Item < ApplicationRecord cattr_reader :per_page @@per_page = 30 - scope :alphabetize_by_translations, ->(locale) { - locale = locale or I18n.locale - it = Item::Translation.arel_table - joins(:translations).where(it[:locale].eq('en')). - order(it[:name].asc) - } - scope :newest, -> { order(arel_table[:created_at].desc) if arel_table[:created_at] } @@ -44,14 +37,10 @@ class Item < ApplicationRecord scope :with_closet_hangers, -> { joins(:closet_hangers) } scope :name_includes, ->(value, locale = I18n.locale) { - it = Item::Translation.arel_table - Item.joins(:translations).where(it[:locale].eq(locale)). - where(it[:name].matches('%' + sanitize_sql_like(value) + '%')) + Item.where("name LIKE ?", "%" + sanitize_sql_like(value) + "%") } scope :name_excludes, ->(value, locale = I18n.locale) { - it = Item::Translation.arel_table - Item.joins(:translations).where(it[:locale].eq(locale)). - where(it[:name].matches('%' + sanitize_sql_like(value) + '%').not) + Item.where("name NOT LIKE ?", "%" + sanitize_sql_like(value) + "%") } scope :is_nc, -> { i = Item.arel_table @@ -62,16 +51,12 @@ class Item < ApplicationRecord where(i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc].eq(true)).not) } scope :is_pb, -> { - it = Item::Translation.arel_table - joins(:translations).where(it[:locale].eq('en')). - where('description LIKE ?', - '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') + where('description LIKE ?', + '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') } scope :is_not_pb, -> { - it = Item::Translation.arel_table - joins(:translations).where(it[:locale].eq('en')). - where('description NOT LIKE ?', - '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') + where('description NOT LIKE ?', + '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') } scope :occupies, ->(zone_label) { zone_ids = Zone.matching_label(zone_label).map(&:id) diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index c476443c..b1f5a186 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -11,8 +11,7 @@ class Item end def results - @filters.map(&:to_query).inject(Item.all, &:merge). - alphabetize_by_translations(Query.locale) + @filters.map(&:to_query).inject(Item.all, &:merge).order(:name) end def to_s