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]
|
2010-11-12 17:58:28 -08:00
|
|
|
|
2010-11-10 13:59:54 -08:00
|
|
|
def create
|
|
|
|
if user_signed_in?
|
2010-11-24 17:51:01 -08:00
|
|
|
@outfit = Outfit.new params[:outfit]
|
|
|
|
@outfit.user = current_user
|
|
|
|
if @outfit.save
|
|
|
|
render :json => @outfit.id
|
2010-11-10 13:59:54 -08:00
|
|
|
else
|
2010-11-24 17:51:01 -08:00
|
|
|
render_outfit_errors
|
2010-11-10 13:59:54 -08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
render :json => {:errors => {:user => ['not logged in']}}, :status => :forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def for_current_user
|
|
|
|
@outfits = user_signed_in? ? current_user.outfits : []
|
|
|
|
render :json => @outfits
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2010-11-24 17:51:01 -08:00
|
|
|
if @outfit.destroy
|
|
|
|
render :json => true
|
|
|
|
else
|
|
|
|
render :json => false, :status => :bad_request
|
|
|
|
end
|
2010-11-11 10:43:22 -08:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
unless fragment_exist?(:action_suffix => 'top_contributors')
|
|
|
|
@top_contributors = User.top_contributors.limit(User::PreviewTopContributorsCount)
|
|
|
|
end
|
2010-11-05 15:45:05 -07:00
|
|
|
end
|
2010-11-11 10:43:22 -08: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
|
|
|
|
|
2010-11-11 10:43:22 -08:00
|
|
|
def update
|
2010-11-24 17:51:01 -08:00
|
|
|
if @outfit.update_attributes(params[:outfit])
|
2010-11-11 10:43:22 -08:00
|
|
|
render :json => true
|
|
|
|
else
|
2010-11-24 17:51:01 -08:00
|
|
|
render_outfit_errors
|
2010-11-11 10:43:22 -08:00
|
|
|
end
|
|
|
|
end
|
2010-11-12 17:58:28 -08:00
|
|
|
|
2010-11-24 17:51:01 -08:00
|
|
|
private
|
|
|
|
|
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
|
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
|