From 5d577db649c28f2c0f4ed52f689b4b42b626f773 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 21 Jan 2024 06:42:24 -0800 Subject: [PATCH] Oops, load the data for the bulk item quantity form on the trades page! --- app/controllers/item_trades_controller.rb | 8 ++++++-- app/controllers/items_controller.rb | 19 +++---------------- app/models/closet_list.rb | 7 +++++++ app/models/user.rb | 12 ++++++++++++ app/views/item_trades/index.html.haml | 7 +++++-- app/views/items/_item_header.haml | 12 +++++++----- app/views/items/show.html.haml | 4 +++- 7 files changed, 43 insertions(+), 26 deletions(-) diff --git a/app/controllers/item_trades_controller.rb b/app/controllers/item_trades_controller.rb index 34c28451..a96f662d 100644 --- a/app/controllers/item_trades_controller.rb +++ b/app/controllers/item_trades_controller.rb @@ -1,14 +1,18 @@ class ItemTradesController < ApplicationController def index @item = Item.find params[:item_id] - @type = type_from_params @item_trades = @item.closet_hangers.trading.includes(:user, :list). user_is_active.order('users.last_trade_activity_at DESC').to_trades - @trades = @item_trades[@type] + if user_signed_in? + @current_user_lists = current_user.closet_lists.alphabetical. + group_by_owned + @current_user_quantities = current_user.item_quantities_for(@item) + end + render layout: 'items' end diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index c4d5207b..c102d9f0 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -63,23 +63,10 @@ class ItemsController < ApplicationController @contributors_with_counts = @item.contributors_with_counts if user_signed_in? - # Empty arrays are important so that we can loop over this and still - # show the generic no-list case - @current_user_lists = {true => [], false => []} - current_user.closet_lists.alphabetical.each do |list| - @current_user_lists[list.hangers_owned] << list - end - - @current_user_quantities = Hash.new(0) # default is zero - hangers = current_user.closet_hangers.where(item_id: @item.id). - select([:owned, :list_id, :quantity]) - - hangers.each do |hanger| - key = hanger.list_id || hanger.owned - @current_user_quantities[key] = hanger.quantity - end + @current_user_lists = current_user.closet_lists.alphabetical. + group_by_owned + @current_user_quantities = current_user.item_quantities_for(@item) end - end format.gif do diff --git a/app/models/closet_list.rb b/app/models/closet_list.rb index 82855c60..20665032 100644 --- a/app/models/closet_list.rb +++ b/app/models/closet_list.rb @@ -87,6 +87,13 @@ class ClosetList < ApplicationRecord end end + def self.group_by_owned + h = all.group_by(&:hangers_owned?) + h[true] ||= [] + h[false] ||= [] + h + end + include VisibilityMethods class Null diff --git a/app/models/user.rb b/app/models/user.rb index a60ba7a2..a6050a8f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -175,6 +175,18 @@ class User < ApplicationRecord contact_neopets_connection.try(:neopets_username) end + def item_quantities_for(item_id) + quantities = Hash.new(0) + + hangers = closet_hangers.where(item_id: item_id). + select([:owned, :list_id, :quantity]) + hangers.each do |hanger| + quantities[hanger.list_id || hanger.owned?] = hanger.quantity + end + + quantities + end + def log_trade_activity touch(:last_trade_activity_at) end diff --git a/app/views/item_trades/index.html.haml b/app/views/item_trades/index.html.haml index 238bbe02..29d474af 100644 --- a/app/views/item_trades/index.html.haml +++ b/app/views/item_trades/index.html.haml @@ -1,8 +1,11 @@ - title t(".title.#{@type}") - hide_title_header -= render partial: "items/item_header", locals: {item: @item, - trades: @item_trades, current_subpage: "trades_#{@type}"} += render partial: "items/item_header", + locals: {item: @item, trades: @item_trades, + current_subpage: "trades_#{@type}", + current_user_lists: @current_user_lists, + current_user_quantities: @current_user_quantities} %h2.item-subpage-title= t(".title.#{@type}") diff --git a/app/views/items/_item_header.haml b/app/views/items/_item_header.haml index 5b10d49e..71f30358 100644 --- a/app/views/items/_item_header.haml +++ b/app/views/items/_item_header.haml @@ -1,5 +1,7 @@ - raise ArgumentError unless defined? item - raise ArgumentError unless defined? trades +- raise ArgumentError unless defined? current_user_lists +- raise ArgumentError unless defined? current_user_quantities - raise ArgumentError unless defined? current_subpage %header.item-header @@ -38,22 +40,22 @@ = t 'items.show.closet_hangers.header_html', user_items_link: link_to(t('your_items'), user_closet_hangers_path(current_user)) - = form_tag update_quantities_user_item_closet_hangers_path(:user_id => current_user, :item_id => @item), :method => :put do + = form_tag update_quantities_user_item_closet_hangers_path(:user_id => current_user, :item_id => item), :method => :put do .closet-hangers-ownership-groups - - @current_user_lists.each do |owned, lists| + - current_user_lists.each do |owned, lists| %div %h4= closet_lists_group_name(:you, owned) %ul - lists.each_with_index do |list, index| %li = number_field_tag "quantity[#{list.id}]", - @current_user_quantities[list.id], min: 0, + current_user_quantities[list.id], min: 0, autofocus: owned && index == 0 = label_tag "quantity[#{list.id}]", list.name %li = number_field_tag "quantity[#{owned}]", - @current_user_quantities[owned], min: 0, + current_user_quantities[owned], min: 0, autofocus: owned && lists.empty? - unless lists.empty? @@ -65,7 +67,7 @@ t('items.show.closet_hangers.quantity_label') = submit_tag t('items.show.closet_hangers.submit') - %p.item-description= @item.description + %p.item-description= item.description %nav.item-subpages-nav = link_to t('items.show.subpages_nav.preview'), item, diff --git a/app/views/items/show.html.haml b/app/views/items/show.html.haml index 2688c962..6a011b70 100644 --- a/app/views/items/show.html.haml +++ b/app/views/items/show.html.haml @@ -2,7 +2,9 @@ - canonical_path @item = render partial: "item_header", - locals: {item: @item, trades: @trades, current_subpage: "preview"} + locals: {item: @item, trades: @trades, current_subpage: "preview", + current_user_lists: @current_user_lists, + current_user_quantities: @current_user_quantities} #outfit-preview-root{'data-item-id': @item.id}