impress/app/helpers/items_helper.rb

97 lines
2.7 KiB
Ruby
Raw Normal View History

2010-05-15 10:47:46 -07:00
module ItemsHelper
2010-09-08 19:49:39 -07:00
NeoitemsURLFormat = 'http://neoitems.net/search2.php?Name=%s&AndOr=and&Category=All&Special=0&Status=Active&Sort=ItemID&results=15&SearchType=8'
2010-06-08 07:39:23 -07:00
module PetTypeImage
Format = 'http://pets.neopets.com/cp/%s/%i/%i.png'
2010-06-08 07:39:23 -07:00
Emotions = {
:happy => 1,
:sad => 2,
:angry => 3,
:ill => 4
}
2010-06-08 07:39:23 -07:00
Sizes = {
:face => 1,
:thumb => 2,
:zoom => 3,
:full => 4
}
end
2010-06-08 07:39:23 -07:00
def standard_species_search_links
build_on_pet_types(Species.all) do |pet_type|
2010-06-08 07:39:23 -07:00
image = pet_type_image(pet_type, :happy, :zoom)
query = "species:#{pet_type.species.name}"
link_to(image, items_path(:q => query))
end
end
def standard_species_images_for(item)
build_on_pet_types(item.supported_species, item.special_color) do |pet_type|
2010-06-08 07:39:23 -07:00
image = pet_type_image(pet_type, :happy, :face)
2010-06-07 13:08:53 -07:00
attributes = {
'data-id' => pet_type.id,
'data-body-id' => pet_type.body_id
}
[:color, :species].each do |pet_type_attribute_name|
pet_type_attribute = pet_type.send(pet_type_attribute_name)
[:id, :name].each do |subattribute_name|
attributes["data-#{pet_type_attribute_name}-#{subattribute_name}"] =
pet_type_attribute.send(subattribute_name)
end
end
2010-06-08 07:39:23 -07:00
link_to(
image,
'#',
2010-06-07 13:08:53 -07:00
attributes
)
2010-06-08 07:39:23 -07:00
end
end
2011-07-12 22:21:48 -07:00
def closeted_icon_for(item)
if item.closeted?
image_tag(
'closeted.png',
:title => 'You own this',
:alt => 'Closet',
:class => 'closeted-icon'
)
end
end
2010-09-08 19:49:39 -07:00
def list_zones(zones, method=:label)
zones.sort { |x,y| x.label <=> y.label }.map(&method).join(', ')
end
2010-09-08 19:49:39 -07:00
def nc_icon_for(item)
image_tag 'nc.png', :title => 'NC Mall Item', :alt => 'NC', :class => 'nc-icon' if item.nc?
end
2010-09-08 19:49:39 -07:00
def neoitems_url_for(item)
sprintf(NeoitemsURLFormat, CGI::escape(item.name))
end
2011-07-17 14:28:45 -07:00
def your_items_path
user_signed_in? ? user_closet_hangers_path(current_user) : login_path
end
2010-06-08 07:39:23 -07:00
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
2010-06-08 07:39:23 -07:00
end
2010-06-08 07:39:23 -07:00
def pet_type_image(pet_type, emotion, size)
emotion_id = PetTypeImage::Emotions[emotion]
size_id = PetTypeImage::Sizes[size]
2010-11-14 20:14:04 -08:00
src = sprintf(PetTypeImage::Format, pet_type.basic_image_hash, emotion_id, size_id)
2010-06-08 07:39:23 -07:00
human_name = pet_type.species.name.humanize
image_tag(src, :alt => human_name, :title => human_name)
end
2010-05-15 10:47:46 -07:00
end