1
0
Fork 0
forked from OpenNeo/impress

Migrate item search away from item translations

Lightning fast for simple name queries now, gotta say!!
This commit is contained in:
Emi Matchu 2024-02-20 16:04:41 -08:00
parent 04af1ee319
commit 3ac9e7ce69
3 changed files with 11 additions and 28 deletions

View file

@ -11,8 +11,8 @@ class ItemsController < ApplicationController
else else
per_page = 30 per_page = 30
end end
@items = @query.results.includes(:translations). @items = @query.results.paginate(
paginate(page: params[:page], per_page: per_page) page: params[:page], per_page: per_page)
assign_closeted! assign_closeted!
respond_to do |format| respond_to do |format|
format.html { format.html {
@ -44,7 +44,7 @@ class ItemsController < ApplicationController
respond_to do |format| respond_to do |format|
format.html { format.html {
@campaign = Fundraising::Campaign.current rescue nil @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'}} format.js { render json: {error: '$q required'}}
end end
@ -86,8 +86,7 @@ class ItemsController < ApplicationController
raise ActiveRecord::RecordNotFound, 'Pet type not found' raise ActiveRecord::RecordNotFound, 'Pet type not found'
end end
@items = @pet_type.needed_items.includes(:translations). @items = @pet_type.needed_items.order(:name)
alphabetize_by_translations
assign_closeted! assign_closeted!
respond_to do |format| respond_to do |format|

View file

@ -28,13 +28,6 @@ class Item < ApplicationRecord
cattr_reader :per_page cattr_reader :per_page
@@per_page = 30 @@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, -> { scope :newest, -> {
order(arel_table[:created_at].desc) if arel_table[:created_at] 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 :with_closet_hangers, -> { joins(:closet_hangers) }
scope :name_includes, ->(value, locale = I18n.locale) { scope :name_includes, ->(value, locale = I18n.locale) {
it = Item::Translation.arel_table Item.where("name LIKE ?", "%" + sanitize_sql_like(value) + "%")
Item.joins(:translations).where(it[:locale].eq(locale)).
where(it[:name].matches('%' + sanitize_sql_like(value) + '%'))
} }
scope :name_excludes, ->(value, locale = I18n.locale) { scope :name_excludes, ->(value, locale = I18n.locale) {
it = Item::Translation.arel_table Item.where("name NOT LIKE ?", "%" + sanitize_sql_like(value) + "%")
Item.joins(:translations).where(it[:locale].eq(locale)).
where(it[:name].matches('%' + sanitize_sql_like(value) + '%').not)
} }
scope :is_nc, -> { scope :is_nc, -> {
i = Item.arel_table i = Item.arel_table
@ -62,14 +51,10 @@ class Item < ApplicationRecord
where(i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc].eq(true)).not) where(i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc].eq(true)).not)
} }
scope :is_pb, -> { scope :is_pb, -> {
it = Item::Translation.arel_table
joins(:translations).where(it[:locale].eq('en')).
where('description LIKE ?', where('description LIKE ?',
'%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%')
} }
scope :is_not_pb, -> { scope :is_not_pb, -> {
it = Item::Translation.arel_table
joins(:translations).where(it[:locale].eq('en')).
where('description NOT LIKE ?', where('description NOT LIKE ?',
'%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') '%' + sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%')
} }

View file

@ -11,8 +11,7 @@ class Item
end end
def results def results
@filters.map(&:to_query).inject(Item.all, &:merge). @filters.map(&:to_query).inject(Item.all, &:merge).order(:name)
alphabetize_by_translations(Query.locale)
end end
def to_s def to_s