From d8b577aab1cb84212ad4143096946e9a018dff6e Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 14 May 2024 16:03:35 -0700 Subject: [PATCH] Add more info to NC Mall section of Item Getting Guide MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NC prices, some CSS, and also a new application-level helper that adds a feature I've long wanted and been working around for Turbo: the ability to specific that a stylesheet is specific to the current page, and should be unloaded when removed! I use this to write `sources.sass` without the usual `body.items-sources` scoping that we've historically used to control what pages a stylesheet applies to. (In the long past, this was because a lot of stylesheets were—and still are–routed through the `application.sass` stylesheet! But even for more recent standalone page stylesheets, I've done the scoping, to avoid issues with styles leaking beyond the page they're meant for when Turbo does a navigation.) --- app/assets/stylesheets/items/sources.sass | 14 ++++++++++++++ app/helpers/application_helper.rb | 8 ++++++++ app/models/item.rb | 4 ++++ app/models/nc_mall_record.rb | 4 ++++ app/views/items/sources.html.haml | 14 +++++++++++++- 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/items/sources.sass diff --git a/app/assets/stylesheets/items/sources.sass b/app/assets/stylesheets/items/sources.sass new file mode 100644 index 00000000..2bb97224 --- /dev/null +++ b/app/assets/stylesheets/items/sources.sass @@ -0,0 +1,14 @@ +.nc-mall-items + list-style: none + display: flex + flex-wrap: wrap + gap: 1em + + li + display: flex + flex-direction: column + align-items: center + width: auto + + .add-to-cart + font-size: 85% diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 587e020d..0db3a801 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -175,6 +175,14 @@ module ApplicationHelper end end + # Includes a stylesheet designed for this specific page of the app, which + # should be removed when navigating to another page. We use Turbo's + # `data-turbo-track="dynamic"` option to do this. + def page_stylesheet_link_tag(src, options={}) + options = {data: {"turbo-track": "dynamic"}}.deep_merge(options) + stylesheet_link_tag src, options + end + def secondary_nav(&block) content_for :before_flashes, content_tag(:nav, :id => 'secondary-nav', &block) diff --git a/app/models/item.rb b/app/models/item.rb index 49b9c3c9..e5295be1 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -175,6 +175,10 @@ class Item < ApplicationRecord @wanted || false end + def current_nc_price + nc_mall_record.current_price + end + def restricted_zones(options={}) options[:scope] ||= Zone.all options[:scope].find(restricted_zone_ids) diff --git a/app/models/nc_mall_record.rb b/app/models/nc_mall_record.rb index 657e7379..d21d1075 100644 --- a/app/models/nc_mall_record.rb +++ b/app/models/nc_mall_record.rb @@ -1,3 +1,7 @@ class NCMallRecord < ApplicationRecord belongs_to :item + + def current_price + discount_price || price + end end diff --git a/app/views/items/sources.html.haml b/app/views/items/sources.html.haml index 296c28d0..d0bab906 100644 --- a/app/views/items/sources.html.haml +++ b/app/views/items/sources.html.haml @@ -3,7 +3,16 @@ - if @nc_mall_items.present? %h2 NC Mall items - = render @nc_mall_items + %p + These items are available in the NC Mall right now! You can buy them + with #{link_to "Neocash", "https://secure.nc.neopets.com/get-neocash"}, + a special currency you can purchase directly from Neopets or redeem via gift cards. + %ul.nc-mall-items + - @nc_mall_items.each do |item| + %li + = render item + %button.add-to-cart{onclick: "alert('Todo!')"} + Buy (#{item.current_nc_price} NC) - if @np_items.present? %h2 Neopoint items @@ -16,3 +25,6 @@ - if @other_nc_items.present? %h2 Other NC items = render @other_nc_items + +- content_for :stylesheets do + = page_stylesheet_link_tag "items/sources"