impress/app/controllers/outfits_controller.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

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]
def create
if user_signed_in?
outfit = Outfit.new params[:outfit]
outfit.user = current_user
if outfit.save
render :json => outfit.id
else
render :json => {:errors => outfit.errors}, :status => :bad_request
end
else
render :json => {:errors => {:user => ['not logged in']}}, :status => :forbidden
end
end
def for_current_user
@outfits = user_signed_in? ? current_user.outfits : []
render :json => @outfits
end
def destroy
authenticate_action &:destroy
end
2010-11-05 15:45:05 -07:00
def new
@colors = Color.all
@species = Species.all
@top_contributors = User.top_contributors.limit(3)
end
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
end
def update
authenticate_action { |outfit| outfit.update_attributes(params[:outfit]) }
end
private
def authenticate_action
if yield(@outfit)
render :json => true
else
render :json => false, :status => :bad_request
end
end
2010-11-13 14:26:14 -08:00
def find_authorized_outfit
raise ActiveRecord::RecordNotFound unless user_signed_in?
@outfit = current_user.outfits.find(params[:id])
end
2010-10-10 19:18:42 -07:00
end