impress/app/controllers/outfits_controller.rb

74 lines
1.7 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
2011-02-10 14:50:47 -08:00
@outfit = Outfit.build_for_user(current_user, params[:outfit])
if @outfit.save
render :json => @outfit.id
else
2011-02-10 14:50:47 -08:00
render_outfit_errors
end
end
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
end
def destroy
if @outfit.destroy
render :json => true
else
render :json => false, :status => :bad_request
end
end
2010-11-05 15:45:05 -07:00
def new
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
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
if @outfit.update_attributes(params[:outfit])
render :json => true
else
render_outfit_errors
end
end
private
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
def render_outfit_errors
render :json => {:errors => @outfit.errors}, :status => :bad_request
end
2010-10-10 19:18:42 -07:00
end