From 9f536f81b3ef0669bb1ef9e307476aafc3b4e275 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 16 Jun 2024 12:37:53 -0700 Subject: [PATCH] Refactor to use a new `Item#source` method for where an item is from I'm doing this in preparation for maybe trying to load some of this info into the outfit editor, too! --- app/controllers/items_controller.rb | 40 ++++++++++---------------- app/models/item.rb | 16 +++++++++++ app/views/items/sources.html.haml | 44 ++++++++++++++--------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 1b9c20e1..ef0810f7 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -13,7 +13,7 @@ class ItemsController < ApplicationController @items = @query.results.paginate( page: params[:page], per_page: per_page) - assign_closeted! + assign_closeted!(@items) respond_to do |format| format.html { @@ -54,7 +54,7 @@ class ItemsController < ApplicationController end elsif params.has_key?(:ids) && params[:ids].is_a?(Array) @items = Item.find(params[:ids]) - assign_closeted! + assign_closeted!(@items) respond_to do |format| format.json { render json: @items } end @@ -104,7 +104,7 @@ class ItemsController < ApplicationController end @items = @pet_type.needed_items.order(:name) - assign_closeted! + assign_closeted!(@items) respond_to do |format| format.html { @pet_name = params[:name] ; render :layout => 'application' } @@ -113,12 +113,15 @@ class ItemsController < ApplicationController end def sources + # Load all the items, then group them by source. item_ids = params[:ids].split(",") - @items = Item.where(id: item_ids).includes(:nc_mall_record). + @all_items = Item.where(id: item_ids).includes(:nc_mall_record). includes(:dyeworks_base_item).order(:name).limit(50) - assign_closeted! + @items = @all_items.group_by(&:source) - if @items.empty? + assign_closeted!(@all_items) + + if @all_items.empty? render file: "public/404.html", status: :not_found, layout: nil return end @@ -126,23 +129,14 @@ class ItemsController < ApplicationController # For Dyeworks items whose base is currently in the NC Mall, preload their # trade values. We'll use this to determine which ones are fully buyable rn # (because Owls tracks this data and we don't). - Item.preload_nc_trade_values(@items.select(&:dyeworks_base_buyable?)) - - # Group the items by category! - @nc_mall_items = @items.select(&:currently_in_mall?). - reject(&:dyeworks_buyable?) - @buyable_dyeworks_items = @items.select(&:dyeworks_buyable?) - @np_items = @items.select(&:np?) - @pb_items = @items.select(&:pb?) - @other_nc_items = @items.select(&:nc?).reject(&:currently_in_mall?). - reject(&:dyeworks_buyable?) + Item.preload_nc_trade_values(@items[:dyeworks]) # Start loading the NC trade values for the non-Mall NC items. - trade_values_task = Async { Item.preload_nc_trade_values(@other_nc_items) } + trade_values_task = Async { Item.preload_nc_trade_values(@items[:other_nc]) } # Also, PB items have some special handling: we group them by color, then # load example pet types for the colors that don't have paint brushes. - @pb_items_by_color = @pb_items.group_by(&:pb_color). + @pb_items_by_color = @items[:pb].group_by(&:pb_color). sort_by { |color, items| color&.name }.to_h colors_without_thumbnails = @pb_items_by_color.keys. @@ -158,11 +152,7 @@ class ItemsController < ApplicationController # Create a second value that only include the items the user *needs*: that # is, that they don't already own. - @nc_mall_items_needed = @nc_mall_items.reject(&:owned?) - @buyable_dyeworks_items_needed = @buyable_dyeworks_items.reject(&:owned?) - @np_items_needed = @np_items.reject(&:owned?) - @pb_items_needed = @pb_items.reject(&:owned?) - @other_nc_items_needed = @other_nc_items.reject(&:owned?) + @items_needed = @items.transform_values { |items| items.reject(&:owned?) } @pb_items_needed_by_color = @pb_items_by_color.transform_values { |items| items.reject(&:owned?) } @@ -174,8 +164,8 @@ class ItemsController < ApplicationController protected - def assign_closeted! - current_user.assign_closeted_to_items!(@items) if user_signed_in? + def assign_closeted!(items) + current_user.assign_closeted_to_items!(items) if user_signed_in? end def load_appearances diff --git a/app/models/item.rb b/app/models/item.rb index 48009725..072a516a 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -240,6 +240,22 @@ class Item < ApplicationRecord Item.find_by_name(name_match["base"]) end + def source + if dyeworks_buyable? + :dyeworks + elsif currently_in_mall? + :nc_mall + elsif nc? + :other_nc + elsif np? + :np + elsif pb? + :pb + else + raise "Item has no matching source (should not happen?)" + end + end + def owned? @owned || false end diff --git a/app/views/items/sources.html.haml b/app/views/items/sources.html.haml index b48b606d..8cf9b9be 100644 --- a/app/views/items/sources.html.haml +++ b/app/views/items/sources.html.haml @@ -1,7 +1,7 @@ - title "Item Getting Guide" %h1#title Item Getting Guide -- if @nc_mall_items.present? +- if @items[:nc_mall].present? %h2 NC Mall items :markdown These items are available in the NC Mall right now! You can buy them @@ -16,22 +16,22 @@ %tr %td %th - Total: #{nc_total_for @nc_mall_items_needed} NC - (#{pluralize @nc_mall_items_needed.size, "item"}) - + Total: #{nc_total_for @items_needed[:nc_mall]} NC + (#{pluralize @items_needed[:nc_mall].size, "item"}) + %td.actions-cell - - if @nc_mall_items_needed.present? + - if @items_needed[:nc_mall].present? %button{onclick: "alert('Todo!')"} = cart_icon alt: "" Buy all in NC Mall %tbody - - @nc_mall_items.each do |item| + - @items[:nc_mall].each do |item| = render "item_list_row", item: do %button{onclick: "alert('Todo!')"} = cart_icon alt: "" Buy (#{item.current_nc_price} NC) -- if @buyable_dyeworks_items.present? +- if @items[:dyeworks].present? %h2 Dyeworks items :markdown These are recolored "Dyeworks" variants of items. First get the "base" @@ -48,24 +48,24 @@ = image_tag "https://images.neopets.com/items/mall_80x80_cleaning.gif", alt: "Dyeworks Hue Brew Potion" %th - Total: #{dyeworks_nc_total_for @buyable_dyeworks_items_needed} NC + Total: #{dyeworks_nc_total_for @items_needed[:dyeworks]} NC = surround "(", ")" do %span.price-breakdown{ - title: "#{dyeworks_items_nc_total_for(@buyable_dyeworks_items_needed)} NC" + title: "#{dyeworks_items_nc_total_for(@items_needed[:dyeworks])} NC" }< - #{pluralize @buyable_dyeworks_items_needed.size, "item"} + #{pluralize @items_needed[:dyeworks].size, "item"} + %span.price-breakdown{ - title: dyeworks_potions_nc_summary(@buyable_dyeworks_items_needed.size) + title: dyeworks_potions_nc_summary(@items_needed[:dyeworks].size) }< - #{pluralize @buyable_dyeworks_items_needed.size, "potion"} + #{pluralize @items_needed[:dyeworks].size, "potion"} %td.actions-cell - - if @buyable_dyeworks_items_needed.present? + - if @items_needed[:dyeworks].present? %button{onclick: "alert('Todo!')"} = cart_icon alt: "" Buy all in NC Mall %tbody - - @buyable_dyeworks_items.each do |item| + - @items[:dyeworks].each do |item| = render "item_list_row", item: do - base_item = item.dyeworks_base_item @@ -85,7 +85,7 @@ = cart_icon alt: "" Buy base (#{item.dyeworks_base_item.current_nc_price} NC) -- if @np_items.present? +- if @items[:np].present? %h2 Neopoint items :markdown These items can be purchased with Neopoints. For less-expensive items, @@ -102,16 +102,16 @@ %tr %td %th{colspan: 2} - Total: #{pluralize @np_items_needed.size, "item"} + Total: #{pluralize @items_needed[:np].size, "item"} %tbody - - @np_items.each do |item| + - @items[:np].each do |item| = render "item_list_row", item: do = button_link_to "Shops", shop_wizard_url_for(item), target: "_blank", icon: search_icon = button_link_to "Trades", trading_post_url_for(item), target: "_blank", icon: search_icon -- if @pb_items.present? +- if @pb_items_by_color.present? %h2 Paint Brush items :markdown These items are part of a paint brush set. Once you paint your pet, @@ -162,7 +162,7 @@ - items.each do |item| = render "item_list_row", item: -- if @other_nc_items.present? +- if @items[:other_nc].present? %h2 Neocash items (Capsules, events, retired, etc.) :markdown These items are part of the Neocash economy, but not directly for sale. @@ -172,7 +172,7 @@ [mall]: https://ncmall.neopets.com/ - - if @other_nc_items.any?(&:nc_trade_value) + - if @items[:other_nc].any?(&:nc_trade_value) :markdown The [Owls Value Guide][owls] often has more details about how to get these items, and how much they're usually worth in the NC Trading @@ -184,9 +184,9 @@ %thead %td %th{colspan: 2} - Total: #{pluralize @other_nc_items_needed.size, "item"} + Total: #{pluralize @items_needed[:other_nc].size, "item"} %tbody - - @other_nc_items.each do |item| + - @items[:other_nc].each do |item| = render "item_list_row", item: do - content_for :subtitle, flush: true do - if item.nc_trade_value.present?