1
0
Fork 0
forked from OpenNeo/impress

Move Item name search logic to model scope

This commit is contained in:
Matchu 2023-07-26 11:15:35 -07:00 committed by Matchu
parent 4cd8944bf4
commit 3e3aa6a126
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