use I18n.with_locale wherever possible, since it catches errors properly

In particular, pet#load was handling locale-switching itself, but wasn't
switching back to original locale on error. We could've used a rescue
block, but, when I18n.with_locale is so cool, may as well use it fully.
This commit is contained in:
Emi Matchu 2013-01-25 15:09:48 -06:00
parent 85d52c23c3
commit 573e8a6459
2 changed files with 41 additions and 45 deletions

View file

@ -43,10 +43,9 @@ module ApplicationHelper
end end
def canonical_path(resource) def canonical_path(resource)
original_locale = I18n.locale I18n.with_locale(I18n.default_locale) do
I18n.locale = I18n.default_locale content_for :meta, tag(:link, :rel => 'canonical', :href => url_for(resource))
content_for :meta, tag(:link, :rel => 'canonical', :href => url_for(resource)) end
I18n.locale = original_locale
end end
def contact_email def contact_email

View file

@ -20,49 +20,46 @@ class Pet < ActiveRecord::Base
options[:item_scope] ||= Item.scoped options[:item_scope] ||= Item.scoped
options[:locale] ||= I18n.default_locale options[:locale] ||= I18n.default_locale
original_locale = I18n.locale I18n.with_locale(options[:locale]) do
I18n.locale = options[:locale] require 'ostruct'
begin
require 'ostruct' neopets_language_code = I18n.neopets_language_code_for(options[:locale])
begin envelope = PET_VIEWER.request([name, 0]).post(
neopets_language_code = I18n.neopets_language_code_for(options[:locale]) :timeout => 2,
envelope = PET_VIEWER.request([name, 0]).post( :headers => {
:timeout => 2, 'Cookie' => "lang=#{neopets_language_code}"
:headers => { }
'Cookie' => "lang=#{neopets_language_code}"
}
)
rescue RocketAMF::RemoteGateway::AMFError => e
if e.message == PET_NOT_FOUND_REMOTE_ERROR
raise PetNotFound, "Pet #{name.inspect} does not exist"
end
raise DownloadError, e.message
rescue RocketAMF::RemoteGateway::ConnectionError => e
raise DownloadError, e.message
end
contents = OpenStruct.new(envelope.messages[0].data.body)
pet_data = OpenStruct.new(contents.custom_pet)
# in case this is running in a thread, explicitly grab an ActiveRecord
# connection, to avoid connection conflicts
Pet.connection_pool.with_connection do
self.pet_type = PetType.find_or_initialize_by_species_id_and_color_id(
pet_data.species_id.to_i,
pet_data.color_id.to_i
) )
self.pet_type.body_id = pet_data.body_id rescue RocketAMF::RemoteGateway::AMFError => e
self.pet_type.origin_pet = self if e.message == PET_NOT_FOUND_REMOTE_ERROR
biology = pet_data.biology_by_zone raise PetNotFound, "Pet #{name.inspect} does not exist"
biology[0] = nil # remove effects if present end
@pet_state = self.pet_type.add_pet_state_from_biology! biology raise DownloadError, e.message
@pet_state.label_by_pet(self, pet_data.owner) rescue RocketAMF::RemoteGateway::ConnectionError => e
@items = Item.collection_from_pet_type_and_registries(self.pet_type, raise DownloadError, e.message
contents.object_info_registry, contents.object_asset_registry, end
options[:item_scope]) contents = OpenStruct.new(envelope.messages[0].data.body)
pet_data = OpenStruct.new(contents.custom_pet)
# in case this is running in a thread, explicitly grab an ActiveRecord
# connection, to avoid connection conflicts
Pet.connection_pool.with_connection do
self.pet_type = PetType.find_or_initialize_by_species_id_and_color_id(
pet_data.species_id.to_i,
pet_data.color_id.to_i
)
self.pet_type.body_id = pet_data.body_id
self.pet_type.origin_pet = self
biology = pet_data.biology_by_zone
biology[0] = nil # remove effects if present
@pet_state = self.pet_type.add_pet_state_from_biology! biology
@pet_state.label_by_pet(self, pet_data.owner)
@items = Item.collection_from_pet_type_and_registries(self.pet_type,
contents.object_info_registry, contents.object_asset_registry,
options[:item_scope])
end
end end
I18n.locale = original_locale
true true
end end