diff --git a/app/controllers/outfits_controller.rb b/app/controllers/outfits_controller.rb index 844cbb45..66ae1a8d 100644 --- a/app/controllers/outfits_controller.rb +++ b/app/controllers/outfits_controller.rb @@ -43,7 +43,7 @@ class OutfitsController < ApplicationController def new unless localized_fragment_exist?(:action_suffix => 'start_from_scratch_form_content') @colors = Color.all_ordered_by_name - @species = Species.all_ordered_by_name + @species = Species.alphabetical end unless localized_fragment_exist?('outfits#new newest_items') @@ -66,8 +66,13 @@ class OutfitsController < ApplicationController end def start - @species = Species.find_by_name params[:species_name] - @color = Color.find_by_name params[:color_name] + 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 + if @species && @color redirect_to wardrobe_path(:species => @species.id, :color => @color.id) else diff --git a/app/controllers/pet_attributes_controller.rb b/app/controllers/pet_attributes_controller.rb index 731177f4..c60c700e 100644 --- a/app/controllers/pet_attributes_controller.rb +++ b/app/controllers/pet_attributes_controller.rb @@ -1,10 +1,8 @@ class PetAttributesController < ApplicationController - caches_page :index - def index render :json => { :color => Color.all_ordered_by_name, - :species => Species.all_ordered_by_name + :species => Species.alphabetical } end end diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index 47662b54..72afaf2e 100644 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -20,7 +20,7 @@ module ItemsHelper end def standard_species_search_links - build_on_pet_types(Species.all) do |pet_type| + build_on_pet_types(Species.alphabetical) do |pet_type| image = pet_type_image(pet_type, :happy, :zoom) query = "species:#{pet_type.species.name}" link_to(image, items_path(:q => query)) diff --git a/app/models/item.rb b/app/models/item.rb index 3df39b1f..ddab917f 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -193,6 +193,10 @@ class Item < ActiveRecord::Base species_ids = pet_types.map(&:species_id).uniq Species.find(species_ids) end + + def support_species?(species) + species_support_ids.blank? || species_support_ids.include?(species.id) + end def as_json(options = {}) { diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index e0b8e156..b904a270 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -3,6 +3,7 @@ class PetType < ActiveRecord::Base IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$}; IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/ + belongs_to :species has_one :contribution, :as => :contributed has_many :pet_states has_many :pets @@ -65,20 +66,6 @@ class PetType < ActiveRecord::Base @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 self['image_hash'] || basic_image_hash end diff --git a/app/models/species.rb b/app/models/species.rb index e6606067..ac3b4f4e 100644 --- a/app/models/species.rb +++ b/app/models/species.rb @@ -1,11 +1,13 @@ -class Species < PetAttribute - fetch_objects! +class Species < ActiveRecord::Base + translates :name - def self.require_by_name(name) - species = Species.find_by_name(name) - raise NotFound, "Species \"#{name.humanize}\" does not exist" unless species - species + scope :alphabetical, lambda { includes(:translations).order(Species::Translation.arel_table[:name]) } + + def as_json(options={}) + {:id => id, :name => human_name} end - class NotFound < ArgumentError;end + def human_name + name.capitalize + end end diff --git a/db/migrate/20130121193957_create_species.rb b/db/migrate/20130121193957_create_species.rb new file mode 100644 index 00000000..7384bd18 --- /dev/null +++ b/db/migrate/20130121193957_create_species.rb @@ -0,0 +1,11 @@ +class CreateSpecies < ActiveRecord::Migration + def self.up + create_table :species + Species.create_translation_table! :name => :string + end + + def self.down + drop_table :species + Species.drop_translation_table! + end +end diff --git a/db/schema.rb b/db/schema.rb index 7d8294a6..8836717b 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 => 20130111213346) do +ActiveRecord::Schema.define(:version => 20130121193957) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -217,6 +217,20 @@ ActiveRecord::Schema.define(:version => 20130111213346) do t.datetime "updated_at" end + create_table "species", :force => true do |t| + end + + create_table "species_translations", :force => true do |t| + t.integer "species_id" + t.string "locale" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "species_translations", ["locale"], :name => "index_species_translations_on_locale" + add_index "species_translations", ["species_id"], :name => "index_species_translations_on_species_id" + create_table "swf_assets", :force => true do |t| t.string "type", :limit => 7, :null => false t.integer "remote_id", :limit => 3, :null => false