From 1f0c8b87a64400b2fca1a5dc973397ed2054d2ab Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 9 Jun 2024 15:03:35 -0700 Subject: [PATCH] Move Item Getting Guide subtitle UI out of helpers, into templates I thought to myself, "I wonder if it's possible to use a sneaky hacky `content_for` trick to be able to run this code in the template." And indeed it is! It's tricky cuz like, I want to render this template, and I want to provide _multiple_ slots of content to it. So, in this variant, we keep the block as being primarily for the actions, but also optionally accept `content_for :subtitle` inside that block, too. Executing that correctly is a bit tricky! The subtitle comes *before* the actions. So, we `yield` the actions block immediately, save it to a variable, and *then* get the subtitle block. --- app/helpers/items_helper.rb | 19 ------------------- app/views/items/_item_list_row.html.haml | 6 ++++-- app/views/items/sources.html.haml | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index b5409952..28cea014 100644 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -156,25 +156,6 @@ module ItemsHelper "Last updated: #{date_str} (#{time_ago_str} ago)" end - def nc_trade_value_subtitle_for(item) - value = item.nc_trade_value - return nil if value.nil? - - link_to "Owls listing: #{item.nc_trade_value.value_text}", - "https://www.neopets.com/~owls", target: "_blank", - title: 'Owls keeps track of approximate "capsule" values of NC items ' + - "for trading. Items with similar values can often be traded for one " + - "another. This is an estimate, not a rule!" - end - - def dyeworks_explanation_subtitle_for(item) - base_item = item.dyeworks_base_item - content_tag :span, class: "dyeworks-explanation" do - concat link_to(base_item.name, base_item, target: "_blank") - concat " + 1 Potion" - end - end - def nc_total_for(items) items.map(&:current_nc_price).sum end diff --git a/app/views/items/_item_list_row.html.haml b/app/views/items/_item_list_row.html.haml index 09b7e52d..7e26c84f 100644 --- a/app/views/items/_item_list_row.html.haml +++ b/app/views/items/_item_list_row.html.haml @@ -1,4 +1,6 @@ -- subtitle = nil unless defined? subtitle +-# Run the block first, so we can get the subtitle content if provided! +- content = yield +- subtitle = yield :subtitle %tr %td.thumbnail-cell @@ -8,4 +10,4 @@ = link_to item.name, item, target: "_blank" - if subtitle.present? .subtitle= subtitle - %td.actions-cell= yield + %td.actions-cell= content diff --git a/app/views/items/sources.html.haml b/app/views/items/sources.html.haml index d27e0c8b..1c35d5a6 100644 --- a/app/views/items/sources.html.haml +++ b/app/views/items/sources.html.haml @@ -61,8 +61,12 @@ Buy all in NC Mall %tbody - @buyable_dyeworks_items.each do |item| - = render "item_list_row", item:, - subtitle: dyeworks_explanation_subtitle_for(item) do + = render "item_list_row", item: do + - base_item = item.dyeworks_base_item + - content_for :subtitle, flush: true do + %span.dyeworks-explanation + = link_to base_item.name, base_item, target: "_blank" + + 1 potion %button{onclick: "alert('Todo!')"} = cart_icon alt: "" Buy base (#{item.dyeworks_base_item.current_nc_price} NC) @@ -153,7 +157,12 @@ Total: #{pluralize @other_nc_items.size, "item"} %tbody - @other_nc_items.each do |item| - = render "item_list_row", item:, subtitle: nc_trade_value_subtitle_for(item) do + = render "item_list_row", item: do + - content_for :subtitle, flush: true do + - if item.nc_trade_value.present? + = link_to "Owls listing: #{item.nc_trade_value.value_text}", + "https://www.neopets.com/~owls", target: "_blank", + title: 'Owls keeps track of approximate "capsule" values of NC items for trading. Items with similar values can often be traded for one another. This is an estimate, not a rule!' = button_link_to "NC Trades", item_trades_path(item, type: "offering"), target: "_blank", icon: search_icon