Use arel for item translation joins

Just a bit defensive so we aren't setting up the possibility of an ambiguous query someday!
This commit is contained in:
Matchu 2023-07-26 11:58:05 -07:00
parent d12b21896c
commit da508ab33a

View file

@ -48,30 +48,32 @@ class Item < ActiveRecord::Base
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) {
Item.joins(:translations).where('locale = ?', locale). it = Item::Translation.arel_table
where('name LIKE ?', '%' + Item.sanitize_sql_like(value) + '%') Item.joins(:translations).where(it[:locale].eq(locale)).
where(it[:name].matches('%' + Item.sanitize_sql_like(value) + '%'))
} }
scope :name_excludes, ->(value, locale = I18n.locale) { scope :name_excludes, ->(value, locale = I18n.locale) {
Item.joins(:translations).where('locale = ?', locale). it = Item::Translation.arel_table
where('name NOT LIKE ?', '%' + Item.sanitize_sql_like(value) + '%') Item.joins(:translations).where(it[:locale].eq(locale)).
where(it[:name].matches('%' + Item.sanitize_sql_like(value) + '%').not)
} }
scope :is_nc, -> { scope :is_nc, -> {
i = Item.arel_table i = Item.arel_table
condition = i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc]) where(i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc]))
where(condition)
} }
scope :is_np, -> { scope :is_np, -> {
i = Item.arel_table i = Item.arel_table
condition = i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc]) where(i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc]).not)
where(condition.not)
} }
scope :is_pb, -> { scope :is_pb, -> {
Item.joins(:translations).where('locale = ?', 'en'). it = Item::Translation.arel_table
Item.joins(:translations).where(it[:locale].eq('en')).
where('description LIKE ?', where('description LIKE ?',
'%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') '%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%')
} }
scope :is_not_pb, -> { scope :is_not_pb, -> {
Item.joins(:translations).where('locale = ?', 'en'). it = Item::Translation.arel_table
Item.joins(:translations).where(it[:locale].eq('en')).
where('description NOT LIKE ?', where('description NOT LIKE ?',
'%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') '%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%')
} }