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
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 :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, -> {
i = Item.arel_table
condition = i[:rarity_index].in(Item::NCRarities).or(i[:is_manually_nc])

View file

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