From 5c119abcd880b89bd2f4b485f69115b3bda8952a Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 26 Jul 2023 12:41:37 -0700 Subject: [PATCH] Add restricts filter back to item search --- app/models/item.rb | 18 ++++++++++++++---- app/models/item/search/query.rb | 12 ++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index 369fb136..6a5febcf 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -65,13 +65,13 @@ class Item < ActiveRecord::Base } scope :is_pb, -> { it = Item::Translation.arel_table - Item.joins(:translations).where(it[:locale].eq('en')). + joins(:translations).where(it[:locale].eq('en')). where('description LIKE ?', '%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') } scope :is_not_pb, -> { it = Item::Translation.arel_table - Item.joins(:translations).where(it[:locale].eq('en')). + joins(:translations).where(it[:locale].eq('en')). where('description NOT LIKE ?', '%' + Item.sanitize_sql_like(PAINTBRUSH_SET_DESCRIPTION) + '%') } @@ -79,7 +79,7 @@ class Item < ActiveRecord::Base zone_ids = Zone.matching_label(zone_label, locale).map(&:id) i = Item.arel_table sa = SwfAsset.arel_table - Item.joins(:swf_assets).where(sa[:zone_id].in(zone_ids)).distinct + joins(:swf_assets).where(sa[:zone_id].in(zone_ids)).distinct } scope :not_occupies, ->(zone_label, locale = I18n.locale) { # TODO: The perf on this is miserable on its own, the query plan chooses @@ -89,7 +89,17 @@ class Item < ActiveRecord::Base zone_ids = Zone.matching_label(zone_label, locale).map(&:id) i = Item.arel_table sa = SwfAsset.arel_table - Item.joins(:swf_assets).where(sa[:zone_id].not_in(zone_ids)).distinct + joins(:swf_assets).where(sa[:zone_id].not_in(zone_ids)).distinct + } + scope :restricts, ->(zone_label, locale = I18n.locale) { + zone_ids = Zone.matching_label(zone_label, locale).map(&:id) + condition = zone_ids.map { '(SUBSTR(zones_restrict, ?, 1) = "1")' }.join(' OR ') + where(condition, *zone_ids) + } + scope :not_restricts, ->(zone_label, locale = I18n.locale) { + zone_ids = Zone.matching_label(zone_label, locale).map(&:id) + condition = zone_ids.map { '(SUBSTR(zones_restrict, ?, 1) = "1")' }.join(' OR ') + where("NOT (#{condition})", *zone_ids) } def closeted? diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index c0b3f167..849b6a34 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -42,6 +42,10 @@ class Item filters << (is_positive ? Filter.occupies(value, locale) : Filter.not_occupies(value, locale)) + when 'restricts' + filters << (is_positive ? + Filter.restricts(value, locale) : + Filter.not_restricts(value, locale)) when 'is' case value when 'nc' @@ -113,6 +117,14 @@ class Item self.new Item.not_occupies(value, locale), "-occupies:#{value}" end + def self.restricts(value, locale) + self.new Item.restricts(value, locale), "restricts:#{value}" + end + + def self.not_restricts(value, locale) + self.new Item.not_restricts(value, locale), "-restricts:#{value}" + end + def self.is_nc self.new Item.is_nc, 'is:nc' end