2010-10-10 19:18:42 -07:00
|
|
|
class OutfitsController < ApplicationController
|
2010-11-13 14:26:14 -08:00
|
|
|
before_filter :find_authorized_outfit, :only => [:update, :destroy]
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-10 13:59:54 -08:00
|
|
|
def create
|
2011-02-10 14:50:47 -08:00
|
|
|
@outfit = Outfit.build_for_user(current_user, params[:outfit])
|
|
|
|
if @outfit.save
|
2012-07-17 09:15:04 -07:00
|
|
|
render :json => @outfit
|
2010-11-10 13:59:54 -08:00
|
|
|
else
|
2011-02-10 14:50:47 -08:00
|
|
|
render_outfit_errors
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
|
|
|
def index
|
|
|
|
if user_signed_in?
|
|
|
|
@outfits = current_user.outfits.wardrobe_order
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render }
|
|
|
|
format.json { render :json => @outfits }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to login_path(:return_to => request.fullpath) }
|
|
|
|
format.json { render :json => [] }
|
|
|
|
end
|
|
|
|
end
|
2010-11-11 10:43:22 -08:00
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def destroy
|
2010-11-24 17:51:01 -08:00
|
|
|
if @outfit.destroy
|
2011-07-14 10:14:06 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:success] = "Outfit #{@outfit.name} successfully deleted"
|
|
|
|
redirect_to current_user_outfits_path
|
|
|
|
}
|
|
|
|
format.json { render :json => true }
|
|
|
|
end
|
2010-11-24 17:51:01 -08:00
|
|
|
else
|
2011-07-14 10:14:06 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:alert] = "Error deleting outfit. Try again?"
|
|
|
|
redirect_to current_user_outfits_path, :status => :bad_request
|
|
|
|
}
|
|
|
|
format.json { render :json => false, :status => :bad_request }
|
|
|
|
end
|
2010-11-24 17:51:01 -08:00
|
|
|
end
|
2010-11-11 10:43:22 -08:00
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-05 15:45:05 -07:00
|
|
|
def new
|
2010-12-11 06:37:39 -08:00
|
|
|
unless fragment_exist?(:action_suffix => 'start_from_scratch_form_content')
|
|
|
|
@colors = Color.all_ordered_by_name
|
|
|
|
@species = Species.all_ordered_by_name
|
|
|
|
end
|
2012-08-01 10:34:54 -07:00
|
|
|
|
2010-12-11 06:37:39 -08:00
|
|
|
unless fragment_exist?(:action_suffix => 'top_contributors')
|
|
|
|
@top_contributors = User.top_contributors.limit(User::PreviewTopContributorsCount)
|
|
|
|
end
|
2012-08-01 10:34:54 -07:00
|
|
|
|
|
|
|
@newest_items = Item.newest.select([:id, :name, :thumbnail_url]).limit(9)
|
|
|
|
@latest_contribution = Contribution.recent.first
|
|
|
|
Contribution.preload_contributeds_and_parents([@latest_contribution])
|
2010-11-05 15:45:05 -07:00
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-12 17:58:28 -08:00
|
|
|
def show
|
2010-11-13 14:26:14 -08:00
|
|
|
@outfit = Outfit.find(params[:id])
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render }
|
|
|
|
format.json { render :json => @outfit }
|
|
|
|
end
|
2010-11-12 17:58:28 -08:00
|
|
|
end
|
2012-06-05 09:44:11 -07:00
|
|
|
|
|
|
|
def start
|
|
|
|
@species = Species.find_by_name params[:species_name]
|
|
|
|
@color = Color.find_by_name params[:color_name]
|
|
|
|
if @species && @color
|
|
|
|
redirect_to wardrobe_path(:species => @species.id, :color => @color.id)
|
|
|
|
else
|
|
|
|
not_found('species/color')
|
|
|
|
end
|
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def update
|
2010-11-24 17:51:01 -08:00
|
|
|
if @outfit.update_attributes(params[:outfit])
|
2012-07-26 20:47:22 -07:00
|
|
|
render :json => @outfit
|
2010-11-11 10:43:22 -08:00
|
|
|
else
|
2010-11-24 17:51:01 -08:00
|
|
|
render_outfit_errors
|
2010-11-11 10:43:22 -08:00
|
|
|
end
|
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-24 17:51:01 -08:00
|
|
|
private
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-13 14:26:14 -08:00
|
|
|
def find_authorized_outfit
|
2010-11-12 17:58:28 -08:00
|
|
|
raise ActiveRecord::RecordNotFound unless user_signed_in?
|
|
|
|
@outfit = current_user.outfits.find(params[:id])
|
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|
2010-11-24 17:51:01 -08:00
|
|
|
def render_outfit_errors
|
|
|
|
render :json => {:errors => @outfit.errors}, :status => :bad_request
|
|
|
|
end
|
2010-10-10 19:18:42 -07:00
|
|
|
end
|
2011-03-23 15:23:01 -07:00
|
|
|
|