From ce4e2fd53cb6922b10509a8d519479f623f0f341 Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 21 Jan 2013 16:01:41 -0600 Subject: [PATCH] move species to database --- app/controllers/outfits_controller.rb | 14 +- app/controllers/pet_attributes_controller.rb | 2 +- app/models/color.rb | 19 +-- app/models/item.rb | 27 ++-- app/models/pet_type.rb | 35 ++-- db/migrate/20130121205607_create_colors.rb | 14 ++ db/schema.rb | 17 +- db/seeds.rb | 162 ++++++++++++++++++- 8 files changed, 230 insertions(+), 60 deletions(-) create mode 100644 db/migrate/20130121205607_create_colors.rb diff --git a/app/controllers/outfits_controller.rb b/app/controllers/outfits_controller.rb index 66ae1a8d..96fc329b 100644 --- a/app/controllers/outfits_controller.rb +++ b/app/controllers/outfits_controller.rb @@ -42,7 +42,7 @@ class OutfitsController < ApplicationController def new unless localized_fragment_exist?(:action_suffix => 'start_from_scratch_form_content') - @colors = Color.all_ordered_by_name + @colors = Color.alphabetical @species = Species.alphabetical end @@ -66,12 +66,12 @@ class OutfitsController < ApplicationController end def start - Globalize.with_locale(I18n.default_locale) do - # Start URLs are always in English, so let's make sure we search in - # English. - @species = Species.find_by_name params[:species_name] - @color = Color.find_by_name params[:color_name] - end + # Start URLs are always in English, so let's make sure we search in + # English. + I18n.locale = I18n.default_locale + + @species = Species.find_by_name params[:species_name] + @color = Color.find_by_name params[:color_name] if @species && @color redirect_to wardrobe_path(:species => @species.id, :color => @color.id) diff --git a/app/controllers/pet_attributes_controller.rb b/app/controllers/pet_attributes_controller.rb index c60c700e..b7d6a641 100644 --- a/app/controllers/pet_attributes_controller.rb +++ b/app/controllers/pet_attributes_controller.rb @@ -1,7 +1,7 @@ class PetAttributesController < ApplicationController def index render :json => { - :color => Color.all_ordered_by_name, + :color => Color.alphabetical, :species => Species.alphabetical } end diff --git a/app/models/color.rb b/app/models/color.rb index 624466a3..792dc21b 100644 --- a/app/models/color.rb +++ b/app/models/color.rb @@ -1,15 +1,16 @@ -class Color < PetAttribute - fetch_objects! +class Color < ActiveRecord::Base + translates :name - Basic = %w(blue green red yellow).map { |name| find_by_name(name) } - BasicIds = Basic.map(&:id) + scope :alphabetical, lambda { includes(:translations).order(Color::Translation.arel_table[:name]) } + scope :basic, where(:basic => true) + scope :standard, where(:standard => true) + scope :nonstandard, where(:standard => false) - def self.basic_ids - BasicIds + def as_json(options={}) + {:id => id, :name => human_name} end - def self.nonstandard_ids - @nonstandard_ids ||= File.read(Rails.root.join('config', 'nonstandard_colors.txt')). - chomp.split("\n").map { |name| Color.find_by_name(name).id } + def human_name + name.split(' ').map { |word| word.capitalize }.join(' ') end end diff --git a/app/models/item.rb b/app/models/item.rb index ddab917f..762d5211 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -20,9 +20,6 @@ class Item < ActiveRecord::Base SPECIAL_COLOR_DESCRIPTION_REGEX = /This item is only wearable by Neopets painted ([a-zA-Z]+)\.|WARNING: This [a-zA-Z]+ can be worn by ([a-zA-Z]+) [a-zA-Z]+ ONLY!/ - SPECIAL_PAINTBRUSH_COLORS_PATH = Rails.root.join('config', 'colors_with_unique_bodies.txt') - SPECIAL_PAINTBRUSH_COLORS = File.read(SPECIAL_PAINTBRUSH_COLORS_PATH).split("\n").map { |name| Color.find_by_name(name) } - cattr_reader :per_page @@per_page = 30 @@ -59,7 +56,7 @@ class Item < ActiveRecord::Base } I18n.usable_locales_with_neopets_language_code.each do |locale| - Globalize.with_locale(locale) do + I18n.with_locale(locale) do indexed_attributes[:name][locale] = self.name end end @@ -160,17 +157,21 @@ class Item < ActiveRecord::Base protected def determine_special_color - if description.include?(PAINTBRUSH_SET_DESCRIPTION) - downcased_name = name.downcase - SPECIAL_PAINTBRUSH_COLORS.each do |color| - return color if downcased_name.include?(color.name) + I18n.with_locale(I18n.default_locale) do + # Rather than go find the special description in all locales, let's just + # run this logic in English. + if description.include?(PAINTBRUSH_SET_DESCRIPTION) + downcased_name = name.downcase + Color.nonstandard.each do |color| + return color if downcased_name.include?(color.name) + end end - end - match = description.match(SPECIAL_COLOR_DESCRIPTION_REGEX) - if match - color = match[1] || match[2] - return Color.find_by_name(color.downcase) + match = description.match(SPECIAL_COLOR_DESCRIPTION_REGEX) + if match + color = match[1] || match[2] + return Color.find_by_name(color.downcase) + end end end public diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index b904a270..f34aa7ca 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -4,6 +4,7 @@ class PetType < ActiveRecord::Base IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/ belongs_to :species + belongs_to :color has_one :contribution, :as => :contributed has_many :pet_states has_many :pets @@ -14,17 +15,16 @@ class PetType < ActiveRecord::Base # Returns all pet types of a single standard color. The caller shouldn't care # which, though, in this implemention, it's always Blue. Don't depend on that. - scope :single_standard_color, where(:color_id => Color::BasicIds[0]) + scope :single_standard_color, lambda { where(:color_id => Color.standard.first) } - scope :nonstandard_colors, where(:color_id => Color.nonstandard_ids) + scope :nonstandard_colors, lambda { where(:color_id => Color.nonstandard) } def self.standard_pet_types_by_species_id - @standard_pet_types_by_species_id ||= - PetType.where(:color_id => Color::BasicIds).group_by(&:species_id) + PetType.where(:color_id => Color.basic).group_by(&:species_id) end def self.standard_body_ids - @standard_body_ids ||= [].tap do |body_ids| + [].tap do |body_ids| standard_pet_types_by_species_id.each do |species_id, pet_types| body_ids.concat(pet_types.map(&:body_id)) end @@ -52,26 +52,21 @@ class PetType < ActiveRecord::Base end 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 image_hash self['image_hash'] || basic_image_hash end def basic_image_hash - BasicHashes[species.name][color.name] + I18n.with_locale(I18n.default_locale) do + # Probably should move the basic hashes into the database someday. + # Until then, access the hash using the English color/species names. + + unless BasicHashes[species.name] && BasicHashes[species.name][color.name] + raise "basic image hash for #{species.name}, #{color.name} not found" + end + + BasicHashes[species.name][color.name] + end end def human_name diff --git a/db/migrate/20130121205607_create_colors.rb b/db/migrate/20130121205607_create_colors.rb new file mode 100644 index 00000000..fcc3eaa8 --- /dev/null +++ b/db/migrate/20130121205607_create_colors.rb @@ -0,0 +1,14 @@ +class CreateColors < ActiveRecord::Migration + def self.up + create_table :colors do |t| + t.boolean :basic + t.boolean :standard + end + Color.create_translation_table! :name => :string + end + + def self.down + drop_table :colors + Color.drop_translation_table! + end +end diff --git a/db/schema.rb b/db/schema.rb index 8836717b..6e1d4d80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130121193957) do +ActiveRecord::Schema.define(:version => 20130121205607) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -54,9 +54,20 @@ ActiveRecord::Schema.define(:version => 20130121193957) do add_index "closet_lists", ["user_id"], :name => "index_closet_lists_on_user_id" + create_table "color_translations", :force => true do |t| + t.integer "color_id" + t.string "locale" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "color_translations", ["color_id"], :name => "index_color_translations_on_color_id" + add_index "color_translations", ["locale"], :name => "index_color_translations_on_locale" + create_table "colors", :force => true do |t| - t.string "name" - t.boolean "basic", :default => false, :null => false + t.boolean "basic" + t.boolean "standard" end create_table "contributions", :force => true do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 664d8c74..87cadcf3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,155 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). -# -# Examples: -# -# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) -# Mayor.create(:name => 'Daley', :city => cities.first) +Species.create(:id => 1, :name => "acara") +Species.create(:id => 2, :name => "aisha") +Species.create(:id => 3, :name => "blumaroo") +Species.create(:id => 4, :name => "bori") +Species.create(:id => 5, :name => "bruce") +Species.create(:id => 6, :name => "buzz") +Species.create(:id => 7, :name => "chia") +Species.create(:id => 8, :name => "chomby") +Species.create(:id => 9, :name => "cybunny") +Species.create(:id => 10, :name => "draik") +Species.create(:id => 11, :name => "elephante") +Species.create(:id => 12, :name => "eyrie") +Species.create(:id => 13, :name => "flotsam") +Species.create(:id => 14, :name => "gelert") +Species.create(:id => 15, :name => "gnorbu") +Species.create(:id => 16, :name => "grarrl") +Species.create(:id => 17, :name => "grundo") +Species.create(:id => 18, :name => "hissi") +Species.create(:id => 19, :name => "ixi") +Species.create(:id => 20, :name => "jetsam") +Species.create(:id => 21, :name => "jubjub") +Species.create(:id => 22, :name => "kacheek") +Species.create(:id => 23, :name => "kau") +Species.create(:id => 24, :name => "kiko") +Species.create(:id => 25, :name => "koi") +Species.create(:id => 26, :name => "korbat") +Species.create(:id => 27, :name => "kougra") +Species.create(:id => 28, :name => "krawk") +Species.create(:id => 29, :name => "kyrii") +Species.create(:id => 30, :name => "lenny") +Species.create(:id => 31, :name => "lupe") +Species.create(:id => 32, :name => "lutari") +Species.create(:id => 33, :name => "meerca") +Species.create(:id => 34, :name => "moehog") +Species.create(:id => 35, :name => "mynci") +Species.create(:id => 36, :name => "nimmo") +Species.create(:id => 37, :name => "ogrin") +Species.create(:id => 38, :name => "peophin") +Species.create(:id => 39, :name => "poogle") +Species.create(:id => 40, :name => "pteri") +Species.create(:id => 41, :name => "quiggle") +Species.create(:id => 42, :name => "ruki") +Species.create(:id => 43, :name => "scorchio") +Species.create(:id => 44, :name => "shoyru") +Species.create(:id => 45, :name => "skeith") +Species.create(:id => 46, :name => "techo") +Species.create(:id => 47, :name => "tonu") +Species.create(:id => 48, :name => "tuskaninny") +Species.create(:id => 49, :name => "uni") +Species.create(:id => 50, :name => "usul") +Species.create(:id => 51, :name => "wocky") +Species.create(:id => 52, :name => "xweetok") +Species.create(:id => 53, :name => "yurble") +Species.create(:id => 54, :name => "zafara") + +Color.create(:id => 1, :name => "alien", :basic => false, :standard => true) +Color.create(:id => 2, :name => "apple", :basic => false, :standard => false) +Color.create(:id => 3, :name => "asparagus", :basic => false, :standard => false) +Color.create(:id => 4, :name => "aubergine", :basic => false, :standard => false) +Color.create(:id => 5, :name => "avocado", :basic => false, :standard => false) +Color.create(:id => 6, :name => "baby", :basic => false, :standard => false) +Color.create(:id => 7, :name => "biscuit", :basic => false, :standard => true) +Color.create(:id => 8, :name => "blue", :basic => true, :standard => true) +Color.create(:id => 9, :name => "blueberry", :basic => false, :standard => false) +Color.create(:id => 10, :name => "brown", :basic => false, :standard => true) +Color.create(:id => 11, :name => "camouflage", :basic => false, :standard => true) +Color.create(:id => 12, :name => "carrot", :basic => false, :standard => false) +Color.create(:id => 13, :name => "checkered", :basic => false, :standard => true) +Color.create(:id => 14, :name => "chocolate", :basic => false, :standard => true) +Color.create(:id => 15, :name => "chokato", :basic => false, :standard => false) +Color.create(:id => 16, :name => "christmas", :basic => false, :standard => true) +Color.create(:id => 17, :name => "clay", :basic => false, :standard => true) +Color.create(:id => 18, :name => "cloud", :basic => false, :standard => true) +Color.create(:id => 19, :name => "coconut", :basic => false, :standard => true) +Color.create(:id => 20, :name => "custard", :basic => false, :standard => true) +Color.create(:id => 21, :name => "darigan", :basic => false, :standard => true) +Color.create(:id => 22, :name => "desert", :basic => false, :standard => true) +Color.create(:id => 23, :name => "disco", :basic => false, :standard => true) +Color.create(:id => 24, :name => "durian", :basic => false, :standard => false) +Color.create(:id => 25, :name => "electric", :basic => false, :standard => true) +Color.create(:id => 26, :name => "faerie", :basic => false, :standard => true) +Color.create(:id => 27, :name => "fire", :basic => false, :standard => true) +Color.create(:id => 28, :name => "garlic", :basic => false, :standard => true) +Color.create(:id => 29, :name => "ghost", :basic => false, :standard => true) +Color.create(:id => 30, :name => "glowing", :basic => false, :standard => true) +Color.create(:id => 31, :name => "gold", :basic => false, :standard => true) +Color.create(:id => 32, :name => "gooseberry", :basic => false, :standard => false) +Color.create(:id => 33, :name => "grape", :basic => false, :standard => false) +Color.create(:id => 34, :name => "green", :basic => true, :standard => true) +Color.create(:id => 35, :name => "grey", :basic => false, :standard => true) +Color.create(:id => 36, :name => "halloween", :basic => false, :standard => true) +Color.create(:id => 37, :name => "ice", :basic => false, :standard => true) +Color.create(:id => 38, :name => "invisible", :basic => false, :standard => true) +Color.create(:id => 39, :name => "island", :basic => false, :standard => true) +Color.create(:id => 40, :name => "jelly", :basic => false, :standard => true) +Color.create(:id => 41, :name => "lemon", :basic => false, :standard => false) +Color.create(:id => 42, :name => "lime", :basic => false, :standard => false) +Color.create(:id => 43, :name => "mallow", :basic => false, :standard => true) +Color.create(:id => 44, :name => "maraquan", :basic => false, :standard => false) +Color.create(:id => 45, :name => "msp", :basic => false, :standard => true) +Color.create(:id => 46, :name => "mutant", :basic => false, :standard => false) +Color.create(:id => 47, :name => "orange", :basic => false, :standard => false) +Color.create(:id => 48, :name => "pea", :basic => false, :standard => false) +Color.create(:id => 49, :name => "peach", :basic => false, :standard => false) +Color.create(:id => 50, :name => "pear", :basic => false, :standard => false) +Color.create(:id => 51, :name => "pepper", :basic => false, :standard => false) +Color.create(:id => 52, :name => "pineapple", :basic => false, :standard => false) +Color.create(:id => 53, :name => "pink", :basic => false, :standard => true) +Color.create(:id => 54, :name => "pirate", :basic => false, :standard => true) +Color.create(:id => 55, :name => "plum", :basic => false, :standard => false) +Color.create(:id => 56, :name => "plushie", :basic => false, :standard => true) +Color.create(:id => 57, :name => "purple", :basic => false, :standard => true) +Color.create(:id => 58, :name => "quigukiboy", :basic => false, :standard => true) +Color.create(:id => 59, :name => "quigukigirl", :basic => false, :standard => true) +Color.create(:id => 60, :name => "rainbow", :basic => false, :standard => true) +Color.create(:id => 61, :name => "red", :basic => true, :standard => true) +Color.create(:id => 62, :name => "robot", :basic => false, :standard => true) +Color.create(:id => 63, :name => "royalboy", :basic => false, :standard => true) +Color.create(:id => 64, :name => "royalgirl", :basic => false, :standard => true) +Color.create(:id => 65, :name => "shadow", :basic => false, :standard => true) +Color.create(:id => 66, :name => "silver", :basic => false, :standard => true) +Color.create(:id => 67, :name => "sketch", :basic => false, :standard => true) +Color.create(:id => 68, :name => "skunk", :basic => false, :standard => true) +Color.create(:id => 69, :name => "snot", :basic => false, :standard => true) +Color.create(:id => 70, :name => "snow", :basic => false, :standard => false) +Color.create(:id => 71, :name => "speckled", :basic => false, :standard => true) +Color.create(:id => 72, :name => "split", :basic => false, :standard => true) +Color.create(:id => 73, :name => "sponge", :basic => false, :standard => true) +Color.create(:id => 74, :name => "spotted", :basic => false, :standard => true) +Color.create(:id => 75, :name => "starry", :basic => false, :standard => true) +Color.create(:id => 76, :name => "strawberry", :basic => false, :standard => true) +Color.create(:id => 77, :name => "striped", :basic => false, :standard => true) +Color.create(:id => 78, :name => "thornberry", :basic => false, :standard => false) +Color.create(:id => 79, :name => "tomato", :basic => false, :standard => false) +Color.create(:id => 80, :name => "tyrannian", :basic => false, :standard => true) +Color.create(:id => 81, :name => "usukiboy", :basic => false, :standard => true) +Color.create(:id => 82, :name => "usukigirl", :basic => false, :standard => true) +Color.create(:id => 83, :name => "white", :basic => false, :standard => true) +Color.create(:id => 84, :name => "yellow", :basic => true, :standard => true) +Color.create(:id => 85, :name => "zombie", :basic => false, :standard => true) +Color.create(:id => 86, :name => "onion", :basic => false, :standard => false) +Color.create(:id => 87, :name => "magma", :basic => false, :standard => true) +Color.create(:id => 88, :name => "relic", :basic => false, :standard => true) +Color.create(:id => 89, :name => "woodland", :basic => false, :standard => true) +Color.create(:id => 90, :name => "transparent", :basic => false, :standard => true) +Color.create(:id => 91, :name => "maractite", :basic => false, :standard => true) +Color.create(:id => 92, :name => "8-bit", :basic => false, :standard => true) +Color.create(:id => 93, :name => "swamp gas", :basic => false, :standard => true) +Color.create(:id => 94, :name => "water", :basic => false, :standard => true) +Color.create(:id => 95, :name => "wraith", :basic => false, :standard => true) +Color.create(:id => 96, :name => "eventide", :basic => false, :standard => true) +Color.create(:id => 97, :name => "elderlyboy", :basic => false, :standard => true) +Color.create(:id => 98, :name => "elderlygirl", :basic => false, :standard => true) +Color.create(:id => 99, :name => "stealthy", :basic => false, :standard => true) +Color.create(:id => 100, :name => "dimensional", :basic => false, :standard => true)