1
0
Fork 0
forked from OpenNeo/impress
impress/app/helpers/items_helper.rb
Emi Matchu 19470f74d4 Use table layout for Item Getting Guide item list
This helps us be more efficient with our use of space, keep the CTAs well
aligned, show a clear total, and set up how we might do CTAs for more complex
cases like all the potential Neopoint CTAs like Wiz/Trades/Auction/etc!
2024-05-20 15:23:34 -07:00

156 lines
4.7 KiB
Ruby

require "addressable/template"
module ItemsHelper
JNItemsURLFormat = 'https://items.jellyneo.net/search/?name=%s&name_type=3'
module PetTypeImage
Format = 'https://pets.neopets.com/cp/%s/%i/%i.png'
Emotions = {
:happy => 1,
:sad => 2,
:angry => 3,
:ill => 4
}
Sizes = {
:face => 1,
:thumb => 2,
:zoom => 3,
:full => 4
}
end
def standard_species_search_links
build_on_pet_types(Species.alphabetical) do |pet_type|
image = pet_type_image(pet_type, :happy, :zoom)
query = "species:#{pet_type.species.name}"
link_to(image, items_path(:q => query))
end
end
def closet_list_verb(owned)
ClosetHanger.verb(:you, owned)
end
def owned_icon
image_tag 'owned.png', :title => t('items.item.owned.description'),
:alt => t('items.item.owned.abbr')
end
def wanted_icon
image_tag 'wanted.png', :title => t('items.item.wanted.description'),
:alt => t('items.item.wanted.abbr')
end
def closeted_icons_for(item)
content = ''.html_safe
content << owned_icon if item.owned?
content << wanted_icon if item.wanted?
content_tag :div, content, :class => 'closeted-icons'
end
# NOTE: Changing this requires bumping the cache at `_closet_list.html.haml`!
def nc_icon
image_tag 'nc.png', :title => t('items.item.nc.description'),
:alt => t('items.item.nc.abbr'), :class => 'nc-icon'
end
# NOTE: Changing this requires bumping the cache at `_closet_list.html.haml`!
def nc_icon_for(item)
nc_icon if item.nc?
end
# NOTE: Changing this requires bumping the cache at `_closet_list.html.haml`!
def item_thumbnail_for(item)
image_tag item.thumbnail_url, alt: "Thumbnail for #{item.name}",
title: item.description, loading: "lazy"
end
def time_with_only_month_if_old(first_seen_at)
# For this month and the previous month, show the full date, so people can
# understand *exactly* how recent it was.
beginning_of_prev_month = Date.today.beginning_of_month - 1.month
if first_seen_at >= beginning_of_prev_month
return first_seen_at.strftime("%b %e, %Y")
end
# Otherwise, show just the month and the year, to be concise. (We'll offer
# the full date as a tooltip, too.)
first_seen_at.strftime("%b %Y")
end
def jn_items_url_for(item)
sprintf(JNItemsURLFormat, CGI::escape(item.name))
end
IMPRESS_2020_ITEM_URL_TEMPLATE = Addressable::Template.new(
"#{Rails.configuration.impress_2020_origin}/items/{id}"
)
def impress_2020_url_for(item)
IMPRESS_2020_ITEM_URL_TEMPLATE.expand(id: item.id).to_s
end
def shop_wizard_url_for(item)
"https://www.neopets.com/market.phtml?type=wizard&string=#{CGI::escape item.name}"
end
def super_shop_wizard_url_for(item)
"https://www.neopets.com/portal/supershopwiz.phtml?string=#{CGI::escape item.name}"
end
def trading_post_url_for(item)
"https://www.neopets.com/island/tradingpost.phtml?type=browse&criteria=item_exact&search_string=#{CGI::escape item.name}"
end
def auction_genie_url_for(item)
"https://www.neopets.com/genie.phtml?type=process_genie&criteria=exact&auctiongenie=#{CGI::escape item.name}"
end
def format_contribution_count(count)
" (&times;#{count})".html_safe if count > 1
end
def render_item_link(item)
render(partial: 'items/item_link', locals: {item: item})
end
def nc_trade_value_updated_at_text(nc_trade_value)
return nil if nc_trade_value.updated_at.nil?
# Render both "[X] [days] ago", and also the exact date, only including the
# year if it's not this same year.
time_ago_str = time_ago_in_words nc_trade_value.updated_at
date_str = nc_trade_value.updated_at.year != Date.today.year ?
nc_trade_value.updated_at.strftime("%b %-d") :
nc_trade_value.updated_at.strftime("%b %-d, %Y")
"Last updated: #{date_str} (#{time_ago_str} ago)"
end
private
def build_on_pet_types(species, special_color=nil, &block)
species_ids = species.map(&:id)
pet_types = special_color ?
PetType.where(:color_id => special_color.id, :species_id => species_ids).
order(:species_id) :
PetType.random_basic_per_species(species.map(&:id))
pet_types.map(&block).join.html_safe
end
def pet_type_image(pet_type, emotion, size)
emotion_id = PetTypeImage::Emotions[emotion]
size_id = PetTypeImage::Sizes[size]
src = sprintf(PetTypeImage::Format, pet_type.basic_image_hash, emotion_id, size_id)
human_name = pet_type.species.name.humanize
image_tag(src, :alt => human_name, :title => human_name)
end
def item_header_user_lists_form_state
cookies.fetch("DTIItemPageUserListsFormState", "closed")
end
end