Refactor how we load closet hangers

Ahh I see, the way we got away with not having a `trading` scope before
was a weird metaprogramming `{owned/wanted}_trading` situation. Okay,
let's trash that in favor of our new stuff! And that helps us bulk the
queries too which is nice.
This commit is contained in:
Emi Matchu 2024-01-19 00:28:28 -08:00
parent 470c805880
commit 16328d3840
2 changed files with 9 additions and 16 deletions

View file

@ -71,9 +71,12 @@ class ItemsController < ApplicationController
@basic_colored_pet_types_by_species_id = PetType.special_color_or_basic(@item.special_color).
includes_child_translations.group_by(&:species)
trading_closet_hangers = @item.closet_hangers.trading.newest.
includes(:user)
@trading_closet_hangers_by_owned = {
true => @item.closet_hangers.owned_trading.newest.includes(:user),
false => @item.closet_hangers.wanted_trading.newest.includes(:user)
true => trading_closet_hangers.filter { |c| c.owned? },
false => trading_closet_hangers.filter { |c| c.wanted? },
}
if user_signed_in?

View file

@ -47,20 +47,6 @@ class ClosetHanger < ApplicationRecord
scope :owned_before_wanted, -> { order(arel_table[:owned].desc) }
scope :unlisted, -> { where(:list_id => nil) }
{:owned => true, :wanted => false}.each do |name, owned|
scope "#{name}_trading", -> {
joins(:user).includes(:list).
where(:owned => owned).
where((
arel_table[:list_id].eq(nil).and(
User.arel_table["#{name}_closet_hangers_visibility"].gteq(ClosetVisibility[:trading].id)
)
).or(
ClosetList.arel_table[:visibility].gteq(ClosetVisibility[:trading].id)
))
}
end
before_validation :merge_quantities, :set_owned_by_list
after_save :log_trade_activity, if: :trading?
@ -74,6 +60,10 @@ class ClosetHanger < ApplicationRecord
possibly_null_closet_list.trading?
end
def wanted?
!owned?
end
def possibly_null_list_id=(list_id_or_owned)
if list_id_or_owned.to_s == 'true' || list_id_or_owned.to_s == 'false'
self.list_id = nil