forked from OpenNeo/impress
Because we ended up with such a big error, and it doesn't have an easy fix, I'm wrapping up today by reverting the entire set of refactors we've done lately, so modeling in production can continue while we improve this code further over time. I generated this commit by hand-picking the refactor-y commits recently, running `git revert --no-commit <hash>` in reverse order, then manually updating `pet_spec.rb` to reflect the state of the code: passing the most important behavioral tests, but no longer passing one of the kinds of annoyances I *did* fix in the new code. ```shell git revert --no-commit48c1a58df9git revert --no-commit42e7eabdd8git revert --no-commitd82c7f817agit revert --no-commit5264947608git revert --no-commit90407403bagit revert --no-commit242b85470dgit revert --no-commit9eaee4a2d4git revert --no-commit52ca41dbffgit revert --no-commitc03e7446e3git revert --no-commitf81415d327git revert --no-commit13ceec8fcc```
80 lines
2.1 KiB
Ruby
80 lines
2.1 KiB
Ruby
class PetsController < ApplicationController
|
|
rescue_from Neopets::CustomPets::PetNotFound, with: :pet_not_found
|
|
rescue_from Neopets::CustomPets::DownloadError, with: :pet_download_error
|
|
rescue_from Pet::UnexpectedDataFormat, with: :unexpected_data_format
|
|
|
|
def load
|
|
# Uncomment this to temporarily disable modeling for most users.
|
|
# return modeling_disabled unless user_signed_in? && current_user.admin?
|
|
|
|
raise Neopets::CustomPets::PetNotFound unless params[:name]
|
|
@pet = Pet.load(params[:name])
|
|
points = contribute(current_user, @pet)
|
|
|
|
respond_to do |format|
|
|
format.html do
|
|
path = destination + "?" + @pet.wardrobe_query
|
|
redirect_to path
|
|
end
|
|
|
|
format.json do
|
|
render :json => {:points => points, :query => @pet.wardrobe_query}
|
|
end
|
|
end
|
|
end
|
|
|
|
protected
|
|
|
|
def contribute(user, pet)
|
|
if user.present?
|
|
points = user.contribute! pet
|
|
else
|
|
pet.save!
|
|
points = true
|
|
end
|
|
points
|
|
end
|
|
|
|
def destination
|
|
case (params[:destination] || params[:origin])
|
|
when 'wardrobe' then wardrobe_path
|
|
else root_path
|
|
end
|
|
end
|
|
|
|
def pet_not_found
|
|
pet_load_error :long_message => t('pets.load.not_found'),
|
|
:status => :not_found
|
|
end
|
|
|
|
def pet_download_error(e)
|
|
Rails.logger.warn e.message
|
|
Rails.logger.warn e.backtrace.join("\n")
|
|
pet_load_error :long_message => t('pets.load.pet_download_error'),
|
|
:status => :gateway_timeout
|
|
end
|
|
|
|
def pet_load_error(options)
|
|
respond_to do |format|
|
|
format.html do
|
|
path = params[:origin] || root_path
|
|
path += "?name=#{params[:name]}"
|
|
redirect_to path, :alert => options[:long_message]
|
|
end
|
|
|
|
format.json do
|
|
render :json => options[:long_message], :status => options[:status]
|
|
end
|
|
end
|
|
end
|
|
|
|
def modeling_disabled
|
|
pet_load_error long_message: t('pets.load.modeling_disabled'),
|
|
status: :forbidden
|
|
end
|
|
|
|
def unexpected_data_format
|
|
pet_load_error long_message: t('pets.load.unexpected_data_format'),
|
|
status: :internal_server_error
|
|
end
|
|
end
|