impress/app/helpers/items_helper.rb

64 lines
1.7 KiB
Ruby
Raw Normal View History

2010-05-15 10:47:46 -07:00
module ItemsHelper
2010-06-08 07:39:23 -07:00
module PetTypeImage
Format = 'http://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_random_standard_pet_types(Species.all) 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
2010-06-08 07:39:23 -07:00
def standard_species_images(species)
build_on_random_standard_pet_types(species) do |pet_type|
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
private
def build_on_random_standard_pet_types(species, &block)
raw(PetType.random_basic_per_species(species.map(&:id)).map(&block).join)
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.image_hash, emotion_id, size_id)
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