Add user:owns/wants back to item search

Not so bad, using a condition on `has_many` `through` was a cute trick!!
This commit is contained in:
Matchu 2023-07-28 15:06:33 -07:00
parent 070599e2ca
commit ca6a573c0a
2 changed files with 29 additions and 1 deletions

View file

@ -58,6 +58,22 @@ class Item
filters << (is_positive ?
Filter.fits(pet_type.body_id, color_name, species_name) :
Filter.not_fits(pet_type.body_id, color_name, species_name))
when 'user'
if user.nil?
message = I18n.translate('items.search.errors.not_logged_in')
raise Item::Search::Error, message
end
case value
when 'owns'
filters << (is_positive ?
Filter.user_owns(user) :
Filter.user_wants(user))
when 'wants'
else
message = I18n.translate('items.search.errors.not_found.ownership',
keyword: value)
raise Item::Search::Error, message
end
when 'is'
case value
when 'nc'
@ -151,6 +167,14 @@ class Item
self.new Item.not_fits(body_id), "-fits:#{value}"
end
def self.user_owns(user)
self.new user.owned_items, 'user:owns'
end
def self.user_wants(user)
self.new user.wanted_items, 'user:wants'
end
def self.is_nc
self.new Item.is_nc, 'is:nc'
end

View file

@ -6,7 +6,11 @@ class User < ActiveRecord::Base
has_many :closet_hangers
has_many :closet_lists
has_many :closeted_items, :through => :closet_hangers, :source => :item
has_many :closeted_items, through: :closet_hangers, source: :item
has_many :owned_items, -> { where(ClosetHanger.arel_table[:owned].eq(true)) },
through: :closet_hangers, source: :item
has_many :wanted_items, -> { where(ClosetHanger.arel_table[:owned].eq(false)) },
through: :closet_hangers, source: :item
has_many :contributions
has_many :neopets_connections
has_many :outfits