From 16328d3840c640e3f344435d4c57c3bf26c2f9e6 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Fri, 19 Jan 2024 00:28:28 -0800 Subject: [PATCH] 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. --- app/controllers/items_controller.rb | 7 +++++-- app/models/closet_hanger.rb | 18 ++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 0938a4ac..0b315f8e 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -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? diff --git a/app/models/closet_hanger.rb b/app/models/closet_hanger.rb index 78a820f4..6b15ddc6 100644 --- a/app/models/closet_hanger.rb +++ b/app/models/closet_hanger.rb @@ -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