From 66c43e220a233a3cc63deacb004493e617f7ec6d Mon Sep 17 00:00:00 2001 From: Matchu Date: Sun, 16 May 2010 16:15:21 -0400 Subject: [PATCH] move species supported images for item show into pet_type method --- app/helpers/items_helper.rb | 11 +++--- app/models/color.rb | 2 ++ app/models/item.rb | 4 +++ app/models/pet_type.rb | 34 +++++++++++++++++++ app/models/species.rb | 7 ---- app/views/items/show.html.haml | 2 +- ..._type_hashes.yml => basic_type_hashes.yml} | 0 spec/models/color_spec.rb | 4 +++ spec/models/item_spec.rb | 10 ++++++ spec/models/pet_type_spec.rb | 30 ++++++++++++++++ 10 files changed, 92 insertions(+), 12 deletions(-) rename config/{standard_type_hashes.yml => basic_type_hashes.yml} (100%) diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index aff665ae..dce2a5dc 100644 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -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 diff --git a/app/models/color.rb b/app/models/color.rb index 84f619ab..39ad1755 100644 --- a/app/models/color.rb +++ b/app/models/color.rb @@ -1,3 +1,5 @@ class Color < PetAttribute fetch_objects! + + Basic = %w(blue green red yellow).map { |name| find_by_name(name) } end diff --git a/app/models/item.rb b/app/models/item.rb index 57e4b7e1..cfb85405 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index a82a563e..9ba19fff 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -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 diff --git a/app/models/species.rb b/app/models/species.rb index 6144ca99..367e1c8d 100644 --- a/app/models/species.rb +++ b/app/models/species.rb @@ -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 diff --git a/app/views/items/show.html.haml b/app/views/items/show.html.haml index 3a2519df..4d2b9245 100644 --- a/app/views/items/show.html.haml +++ b/app/views/items/show.html.haml @@ -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! diff --git a/config/standard_type_hashes.yml b/config/basic_type_hashes.yml similarity index 100% rename from config/standard_type_hashes.yml rename to config/basic_type_hashes.yml diff --git a/spec/models/color_spec.rb b/spec/models/color_spec.rb index 7c083198..1712ce9c 100644 --- a/spec/models/color_spec.rb +++ b/spec/models/color_spec.rb @@ -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 diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index af0d439d..2efa99d6 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -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' diff --git a/spec/models/pet_type_spec.rb b/spec/models/pet_type_spec.rb index 5b974bbb..014f7281 100644 --- a/spec/models/pet_type_spec.rb +++ b/spec/models/pet_type_spec.rb @@ -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