1
0
Fork 0
forked from OpenNeo/impress

move species supported images for item show into pet_type method

This commit is contained in:
Emi Matchu 2010-05-16 16:15:21 -04:00
parent f6dce65dfb
commit 66c43e220a
10 changed files with 92 additions and 12 deletions

View file

@ -1,11 +1,14 @@
module ItemsHelper
StandardSpeciesImageFormat = 'http://pets.neopets.com/cp/%s/1/1.png'
def standard_species_images
colors = Species::StandardColors
raw(Species.all.inject('') do |html, species|
def standard_species_images(species_list)
colors = Color::Basic
pet_type = PetType.new
raw(species_list.inject('') do |html, species|
color = colors[rand(colors.size)]
src = sprintf(StandardSpeciesImageFormat, species.hash_for_color(color))
pet_type.species = species
pet_type.color = color
src = sprintf(StandardSpeciesImageFormat, pet_type.image_hash)
html + image_tag(src, 'data-color' => color, 'data-species' => species.name)
end)
end

View file

@ -1,3 +1,5 @@
class Color < PetAttribute
fetch_objects!
Basic = %w(blue green red yellow).map { |name| find_by_name(name) }
end

View file

@ -22,6 +22,10 @@ class Item < ActiveRecord::Base
write_attribute('species_support_ids', replacement)
end
def supported_species
@supported_species ||= species_support_ids.empty? ? Species.all : species_support_ids.map { |id| Species.find(id) }
end
def self.search(query)
raise ArgumentError, "Please provide a search query" unless query
query = query.strip

View file

@ -1,5 +1,39 @@
class PetType < ActiveRecord::Base
BasicHashes = YAML::load_file(Rails.root.join('config', 'basic_type_hashes.yml'))
def as_json(options={})
{:id => id, :body_id => body_id}
end
def color_id=(new_color_id)
@color = nil
write_attribute('color_id', new_color_id)
end
def color=(new_color)
@color = new_color
write_attribute('color_id', @color.id)
end
def color
@color ||= Color.find(color_id)
end
def species_id=(new_species_id)
@species = nil
write_attribute('species_id', new_species_id)
end
def species=(new_species)
@species = new_species
write_attribute('species_id', @species.id)
end
def species
@species ||= Species.find(species_id)
end
def image_hash
BasicHashes[species.name][color.name]
end
end

View file

@ -1,10 +1,3 @@
class Species < PetAttribute
fetch_objects!
StandardColors = %w(blue green yellow red)
StandardHashes = YAML::load_file(Rails.root.join('config', 'standard_type_hashes.yml'))
def hash_for_color(color)
StandardHashes[name][color]
end
end

View file

@ -7,7 +7,7 @@
%h3 Preview
#item-preview
#item-preview-species= standard_species_images
#item-preview-species= standard_species_images(@item.supported_species)
#item-preview-swf
Javascript and Flash are required to preview wearables. Sorry!

View file

@ -15,4 +15,8 @@ describe Color do
Color.find_by_name('Alien').id.should == 1
Color.find_by_name('alien').id.should == 1
end
specify "class should have list of basic colors" do
Color::Basic.map { |c| c.name }.should == ['blue', 'green', 'red', 'yellow']
end
end

View file

@ -13,6 +13,16 @@ describe Item do
@item.species_support_ids.should == [4, 5, 6]
end
specify "should provide list of supported species objects" do
@item.species_support_ids = [1, 2, 3]
@item.supported_species.map { |s| s.name }.should == ['acara', 'aisha', 'blumaroo']
end
specify "should provide all species if no support ids" do
@item.species_support_ids = ''
@item.supported_species.count.should be > 0
end
specify "should have many swf_assets through parent_swf_asset_relationships" do
3.times do |n|
swf_asset = Factory.create :swf_asset, :id => n, :url => "http://images.neopets.com/#{n}.swf", :type => 'object'

View file

@ -6,5 +6,35 @@ describe PetType do
pet_type = PetType.create :color_id => 2, :species_id => 3, :body_id => 4
pet_type.as_json.should == {:id => 1, :body_id => 4}
end
specify "should allow setting species object" do
pet_type = PetType.new
pet_type.species = Species.find(1)
pet_type.species_id.should == 1
pet_type.species.id.should == 1
pet_type.species.name.should == 'acara'
end
specify "should allow setting color object" do
pet_type = PetType.new
pet_type.color = Color.find(1)
pet_type.color_id.should == 1
pet_type.color.id.should == 1
pet_type.color.name.should == 'alien'
end
specify "should return image hash if a basic color" do
blue = Color.find_by_name('blue')
acara = Species.find_by_name('acara')
pet_type = PetType.new :color => blue, :species => acara
pet_type.image_hash.should == 'mnbztxxn'
end
specify "should return nil if not a basic color" do
asparagus = Color.find_by_name('asparagus')
acara = Species.find_by_name('acara')
pet_type = PetType.new :color => asparagus, :species => acara
pet_type.image_hash.should be nil
end
end
end