From c5c587fab1eb4f94b8e0d594056fcb378f115f7f Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 27 Jul 2015 13:23:46 -0400 Subject: [PATCH] stop crashing when trying to translate item names --- app/models/item.rb | 9 ++++++--- app/models/pet.rb | 17 ++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index a7f9067c..8581b971 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -389,12 +389,15 @@ class Item < ActiveRecord::Base !species_support_ids.empty? end - def origin_registry_info=(info) - Rails.logger.debug("info! #{info}") + def add_origin_registry_info(info, locale) # bear in mind that numbers from registries are floats species_support_strs = info['species_support'] || [] self.species_support_ids = species_support_strs.map(&:to_i) + + self.name_translations = {locale => info['name']} + attribute_names.each do |attribute| + next if attribute == 'name' value = info[attribute.to_sym] if value value = value.to_i if value.is_a? Float @@ -507,7 +510,7 @@ class Item < ActiveRecord::Base item.id = item_id items[item_id] = item end - item.origin_registry_info = info_registry[item.id.to_s] + item.add_origin_registry_info info_registry[item.id.to_s], I18n.default_locale item.current_body_id = pet_type.body_id # Build and update the SWF diff --git a/app/models/pet.rb b/app/models/pet.rb index 5c90ccdf..d7aa704d 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -45,9 +45,10 @@ class Pet < ActiveRecord::Base options[:item_scope]) end - def fetch_viewer_data(timeout=4) + def fetch_viewer_data(timeout=4, locale=nil) + locale ||= I18n.default_locale begin - neopets_language_code = I18n.compatible_neopets_language_code_for(I18n.locale) + neopets_language_code = I18n.compatible_neopets_language_code_for(locale) envelope = PET_VIEWER.request([name, 0]).post( :timeout => timeout, :headers => { @@ -107,7 +108,7 @@ class Pet < ActiveRecord::Base # Fetch registry data in parallel registries = Parallel.map(candidates.keys, :in_threads => 8) do |locale| - viewer_data = I18n.with_locale(locale) { fetch_viewer_data } + viewer_data = fetch_viewer_data(4, locale) # TODO: ew, don't copy-paste the default timeout [locale, viewer_data[:object_info_registry]] end @@ -126,12 +127,10 @@ class Pet < ActiveRecord::Base # Apply translations, and figure out what items are currently being worn current_items = Set.new registries.each do |locale, registry| - I18n.with_locale(locale) do - registry.each do |item_id, item_info| - item = items_by_id[item_id.to_i] - item.origin_registry_info = item_info - current_items << item - end + registry.each do |item_id, item_info| + item = items_by_id[item_id.to_i] + item.add_origin_registry_info(item_info, locale) + current_items << item end end