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

53 lines
1.3 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
DESTINATIONS = {
'needed_items' => '?',
'root' => '#',
'wardrobe' => '#'
}
def load
2010-11-05 17:09:03 -07:00
# TODO: include point value with JSON once contributions implemented
2010-11-05 15:45:05 -07:00
raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(params[:name])
2010-11-06 16:07:15 -07:00
if user_signed_in?
points = current_user.contribute! @pet
else
@pet.save
points = true
end
2010-11-05 17:09:03 -07:00
respond_to do |format|
format.html do
2010-11-06 10:07:12 -07:00
destination = params[:destination] || params[:origin]
2010-11-05 17:09:03 -07:00
destination = 'root' unless DESTINATIONS[destination]
query_joiner = DESTINATIONS[destination]
path = send("#{destination}_path") + query_joiner + @pet.wardrobe_query
redirect_to path
end
format.json do
2010-11-06 16:07:15 -07:00
render :json => points
2010-11-05 17:09:03 -07:00
end
end
2010-11-05 15:45:05 -07:00
end
protected
def pet_not_found
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 => 'Could not find any pet by that name. Did you spell it correctly?'
end
format.json do
render :text => 'Pet not found', :status => :not_found
end
end
end
end