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.
This commit is contained in:
Emi Matchu 2024-06-09 15:03:35 -07:00
parent b22ccbc2a3
commit 1f0c8b87a6
3 changed files with 16 additions and 24 deletions

View file

@ -156,25 +156,6 @@ module ItemsHelper
"Last updated: #{date_str} (#{time_ago_str} ago)" "Last updated: #{date_str} (#{time_ago_str} ago)"
end 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) def nc_total_for(items)
items.map(&:current_nc_price).sum items.map(&:current_nc_price).sum
end end

View file

@ -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 %tr
%td.thumbnail-cell %td.thumbnail-cell
@ -8,4 +10,4 @@
= link_to item.name, item, target: "_blank" = link_to item.name, item, target: "_blank"
- if subtitle.present? - if subtitle.present?
.subtitle= subtitle .subtitle= subtitle
%td.actions-cell= yield %td.actions-cell= content

View file

@ -61,8 +61,12 @@
Buy all in NC Mall Buy all in NC Mall
%tbody %tbody
- @buyable_dyeworks_items.each do |item| - @buyable_dyeworks_items.each do |item|
= render "item_list_row", item:, = render "item_list_row", item: do
subtitle: dyeworks_explanation_subtitle_for(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!')"} %button{onclick: "alert('Todo!')"}
= 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)
@ -153,7 +157,12 @@
Total: #{pluralize @other_nc_items.size, "item"} Total: #{pluralize @other_nc_items.size, "item"}
%tbody %tbody
- @other_nc_items.each do |item| - @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", = button_link_to "NC Trades",
item_trades_path(item, type: "offering"), item_trades_path(item, type: "offering"),
target: "_blank", icon: search_icon target: "_blank", icon: search_icon