1
0
Fork 0
forked from OpenNeo/impress
impress/app/controllers/pets_controller.rb

74 lines
2 KiB
Ruby
Raw Normal View History

2010-10-10 19:18:42 -07:00
class PetsController < ApplicationController
2010-11-05 15:45:05 -07:00
rescue_from Pet::PetNotFound, :with => :pet_not_found
rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :asset_download_error
rescue_from Pet::DownloadError, :with => :pet_download_error
2010-11-05 15:45:05 -07:00
cache_sweeper :user_sweeper
2010-11-05 15:45:05 -07:00
def load
2011-01-27 13:35:46 -08:00
if params[:name] == '!'
redirect_to roulette_path
2010-11-06 16:07:15 -07:00
else
2011-01-27 13:35:46 -08:00
raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(params[:name])
if user_signed_in?
points = current_user.contribute! @pet
else
@pet.save
points = true
2010-11-05 17:09:03 -07:00
end
2011-01-27 13:35:46 -08:00
respond_to do |format|
format.html do
2012-08-06 18:15:31 -07:00
path = destination + @pet.wardrobe_query
2011-01-27 13:35:46 -08:00
redirect_to path
end
format.json do
2012-08-06 18:15:31 -07:00
render :json => {:points => points, :query => @pet.wardrobe_query}
2011-01-27 13:35:46 -08:00
end
2010-11-05 17:09:03 -07:00
end
end
2010-11-05 15:45:05 -07:00
end
protected
2012-08-06 18:15:31 -07:00
def destination
case (params[:destination] || params[:origin])
when 'wardrobe' then wardrobe_path + '#'
when 'needed_items' then needed_items_path + '?'
else root_path + '#'
end
end
2010-11-05 15:45:05 -07:00
def pet_not_found
2013-01-09 20:05:07 -08:00
pet_load_error :long_message => t('pets.load.not_found'),
:status => :not_found
end
def asset_download_error(e)
Rails.logger.warn e.message
2013-01-09 20:05:07 -08:00
pet_load_error :long_message => t('pets.load.asset_download_error'),
:status => :gateway_timeout
end
def pet_download_error(e)
Rails.logger.warn e.message
2013-01-09 20:05:07 -08:00
pet_load_error :long_message => t('pets.load.pet_download_error'),
:status => :gateway_timeout
end
def pet_load_error(options)
2010-11-05 17:09:03 -07:00
respond_to do |format|
format.html do
path = params[:origin] || root_path
path += "?name=#{params[:name]}"
redirect_to path, :alert => options[:long_message]
2010-11-05 17:09:03 -07:00
end
format.json do
2012-08-06 18:15:31 -07:00
render :text => options[:long_message], :status => options[:status]
2010-11-05 17:09:03 -07:00
end
end
end
end