Move Item name search logic to model scope

This commit is contained in:
Matchu 2023-07-26 11:15:35 -07:00
parent c04bab55d0
commit 2ac806333d
2 changed files with 11 additions and 6 deletions

View file

@ -47,6 +47,14 @@ 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) {
Item.joins(:translations).where('locale = ?', locale).
where('name LIKE ?', '%' + Item.sanitize_sql_like(value) + '%')
}
scope :name_excludes, ->(value, locale = I18n.locale) {
Item.joins(:translations).where('locale = ?', locale).
where('name NOT LIKE ?', '%' + Item.sanitize_sql_like(value) + '%')
}
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]) condition = i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc])

View file

@ -75,12 +75,9 @@ class Item
end end
def to_query def to_query
base = Item.joins(:translations).where('locale = ?', @locale) @is_positive ?
if @is_positive Item.name_includes(@value, @locale) :
base.where('name LIKE ?', '%' + Item.sanitize_sql_like(@value) + '%') Item.name_excludes(@value, @locale)
else
base.where('name NOT LIKE ?', '%' + Item.sanitize_sql_like(@value) + '%')
end
end end
def to_s def to_s