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!
This commit is contained in:
Emi Matchu 2024-06-16 12:37:53 -07:00
parent 77f01a6cb9
commit 9f536f81b3
3 changed files with 53 additions and 47 deletions

View file

@ -13,7 +13,7 @@ class ItemsController < ApplicationController
@items = @query.results.paginate( @items = @query.results.paginate(
page: params[:page], per_page: per_page) page: params[:page], per_page: per_page)
assign_closeted! assign_closeted!(@items)
respond_to do |format| respond_to do |format|
format.html { format.html {
@ -54,7 +54,7 @@ class ItemsController < ApplicationController
end end
elsif params.has_key?(:ids) && params[:ids].is_a?(Array) elsif params.has_key?(:ids) && params[:ids].is_a?(Array)
@items = Item.find(params[:ids]) @items = Item.find(params[:ids])
assign_closeted! assign_closeted!(@items)
respond_to do |format| respond_to do |format|
format.json { render json: @items } format.json { render json: @items }
end end
@ -104,7 +104,7 @@ class ItemsController < ApplicationController
end end
@items = @pet_type.needed_items.order(:name) @items = @pet_type.needed_items.order(:name)
assign_closeted! assign_closeted!(@items)
respond_to do |format| respond_to do |format|
format.html { @pet_name = params[:name] ; render :layout => 'application' } format.html { @pet_name = params[:name] ; render :layout => 'application' }
@ -113,12 +113,15 @@ class ItemsController < ApplicationController
end end
def sources def sources
# Load all the items, then group them by source.
item_ids = params[:ids].split(",") 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) 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 render file: "public/404.html", status: :not_found, layout: nil
return return
end end
@ -126,23 +129,14 @@ class ItemsController < ApplicationController
# For Dyeworks items whose base is currently in the NC Mall, preload their # 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 # trade values. We'll use this to determine which ones are fully buyable rn
# (because Owls tracks this data and we don't). # (because Owls tracks this data and we don't).
Item.preload_nc_trade_values(@items.select(&:dyeworks_base_buyable?)) Item.preload_nc_trade_values(@items[:dyeworks])
# 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?)
# Start loading the NC trade values for the non-Mall NC items. # 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 # 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. # 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 sort_by { |color, items| color&.name }.to_h
colors_without_thumbnails = @pb_items_by_color.keys. 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 # Create a second value that only include the items the user *needs*: that
# is, that they don't already own. # is, that they don't already own.
@nc_mall_items_needed = @nc_mall_items.reject(&:owned?) @items_needed = @items.transform_values { |items| 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?)
@pb_items_needed_by_color = @pb_items_needed_by_color =
@pb_items_by_color.transform_values { |items| items.reject(&:owned?) } @pb_items_by_color.transform_values { |items| items.reject(&:owned?) }
@ -174,8 +164,8 @@ class ItemsController < ApplicationController
protected protected
def assign_closeted! def assign_closeted!(items)
current_user.assign_closeted_to_items!(@items) if user_signed_in? current_user.assign_closeted_to_items!(items) if user_signed_in?
end end
def load_appearances def load_appearances

View file

@ -240,6 +240,22 @@ class Item < ApplicationRecord
Item.find_by_name(name_match["base"]) Item.find_by_name(name_match["base"])
end 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? def owned?
@owned || false @owned || false
end end

View file

@ -1,7 +1,7 @@
- title "Item Getting Guide" - title "Item Getting Guide"
%h1#title Item Getting Guide %h1#title Item Getting Guide
- if @nc_mall_items.present? - if @items[:nc_mall].present?
%h2 NC Mall items %h2 NC Mall items
:markdown :markdown
These items are available in the NC Mall right now! You can buy them These items are available in the NC Mall right now! You can buy them
@ -16,22 +16,22 @@
%tr %tr
%td %td
%th %th
Total: #{nc_total_for @nc_mall_items_needed} NC Total: #{nc_total_for @items_needed[:nc_mall]} NC
(#{pluralize @nc_mall_items_needed.size, "item"}) (#{pluralize @items_needed[:nc_mall].size, "item"})
%td.actions-cell %td.actions-cell
- if @nc_mall_items_needed.present? - if @items_needed[:nc_mall].present?
%button{onclick: "alert('Todo!')"} %button{onclick: "alert('Todo!')"}
= cart_icon alt: "" = cart_icon alt: ""
Buy all in NC Mall Buy all in NC Mall
%tbody %tbody
- @nc_mall_items.each do |item| - @items[:nc_mall].each do |item|
= render "item_list_row", item: do = render "item_list_row", item: do
%button{onclick: "alert('Todo!')"} %button{onclick: "alert('Todo!')"}
= cart_icon alt: "" = cart_icon alt: ""
Buy (#{item.current_nc_price} NC) Buy (#{item.current_nc_price} NC)
- if @buyable_dyeworks_items.present? - if @items[:dyeworks].present?
%h2 Dyeworks items %h2 Dyeworks items
:markdown :markdown
These are recolored "Dyeworks" variants of items. First get the "base" 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", = image_tag "https://images.neopets.com/items/mall_80x80_cleaning.gif",
alt: "Dyeworks Hue Brew Potion" alt: "Dyeworks Hue Brew Potion"
%th %th
Total: #{dyeworks_nc_total_for @buyable_dyeworks_items_needed} NC Total: #{dyeworks_nc_total_for @items_needed[:dyeworks]} NC
= surround "(", ")" do = surround "(", ")" do
%span.price-breakdown{ %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{ %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 %td.actions-cell
- if @buyable_dyeworks_items_needed.present? - if @items_needed[:dyeworks].present?
%button{onclick: "alert('Todo!')"} %button{onclick: "alert('Todo!')"}
= cart_icon alt: "" = cart_icon alt: ""
Buy all in NC Mall Buy all in NC Mall
%tbody %tbody
- @buyable_dyeworks_items.each do |item| - @items[:dyeworks].each do |item|
= render "item_list_row", item: do = render "item_list_row", item: do
- base_item = item.dyeworks_base_item - base_item = item.dyeworks_base_item
@ -85,7 +85,7 @@
= cart_icon alt: "" = cart_icon alt: ""
Buy base (#{item.dyeworks_base_item.current_nc_price} NC) Buy base (#{item.dyeworks_base_item.current_nc_price} NC)
- if @np_items.present? - if @items[:np].present?
%h2 Neopoint items %h2 Neopoint items
:markdown :markdown
These items can be purchased with Neopoints. For less-expensive items, These items can be purchased with Neopoints. For less-expensive items,
@ -102,16 +102,16 @@
%tr %tr
%td %td
%th{colspan: 2} %th{colspan: 2}
Total: #{pluralize @np_items_needed.size, "item"} Total: #{pluralize @items_needed[:np].size, "item"}
%tbody %tbody
- @np_items.each do |item| - @items[:np].each do |item|
= render "item_list_row", item: do = render "item_list_row", item: do
= button_link_to "Shops", shop_wizard_url_for(item), = button_link_to "Shops", shop_wizard_url_for(item),
target: "_blank", icon: search_icon target: "_blank", icon: search_icon
= button_link_to "Trades", trading_post_url_for(item), = button_link_to "Trades", trading_post_url_for(item),
target: "_blank", icon: search_icon target: "_blank", icon: search_icon
- if @pb_items.present? - if @pb_items_by_color.present?
%h2 Paint Brush items %h2 Paint Brush items
:markdown :markdown
These items are part of a paint brush set. Once you paint your pet, These items are part of a paint brush set. Once you paint your pet,
@ -162,7 +162,7 @@
- items.each do |item| - items.each do |item|
= render "item_list_row", item: = render "item_list_row", item:
- if @other_nc_items.present? - if @items[:other_nc].present?
%h2 Neocash items (Capsules, events, retired, etc.) %h2 Neocash items (Capsules, events, retired, etc.)
:markdown :markdown
These items are part of the Neocash economy, but not directly for sale. These items are part of the Neocash economy, but not directly for sale.
@ -172,7 +172,7 @@
[mall]: https://ncmall.neopets.com/ [mall]: https://ncmall.neopets.com/
- if @other_nc_items.any?(&:nc_trade_value) - if @items[:other_nc].any?(&:nc_trade_value)
:markdown :markdown
The [Owls Value Guide][owls] often has more details about how to get 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 these items, and how much they're usually worth in the NC Trading
@ -184,9 +184,9 @@
%thead %thead
%td %td
%th{colspan: 2} %th{colspan: 2}
Total: #{pluralize @other_nc_items_needed.size, "item"} Total: #{pluralize @items_needed[:other_nc].size, "item"}
%tbody %tbody
- @other_nc_items.each do |item| - @items[:other_nc].each do |item|
= render "item_list_row", item: do = render "item_list_row", item: do
- content_for :subtitle, flush: true do - content_for :subtitle, flush: true do
- if item.nc_trade_value.present? - if item.nc_trade_value.present?