Created
January 17, 2012 22:35
-
-
Save burlesona/1629457 to your computer and use it in GitHub Desktop.
Photos Controller - 1-17-2012
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhotosController < ApplicationController | |
before_filter :authenticate_user!, :except => [:index, :show] | |
impressionist :actions=>[:show] | |
respond_to :html, :json | |
def index | |
@search = Photo.search(params[:search]) | |
@search.meta_sort ||= 'created_at.desc' | |
@photos = @search.page(params[:page]).per(20) | |
respond_with @photos | |
end | |
def show | |
@photo = Photo.find(params[:id]) | |
respond_with @photo | |
end | |
def new | |
end | |
def create | |
@photo = current_user.photos.create(:image => params[:file]) | |
render :json => PhotoResponder.new( @photo ) | |
end | |
def edit | |
@photo = Photo.unscoped.find(params[:id]) | |
@contexts = Context.all | |
@focus = Focus.all | |
session[:return_to] = request.referer | |
respond_with @photo | |
end | |
def update | |
@photo = Photo.unscoped.find(params[:id]) | |
@photo.update_attributes(params[:photo]) | |
if @photo.save | |
@photo.status == 'complete' ? notice = "Photo completed and moved from queue." : notice = "Photo updated." | |
redirect_to session[:return_to], :notice => notice | |
respond_with @photo | |
else | |
@contexts = Context.all | |
@focus = Focus.all | |
render 'edit' | |
end | |
end | |
def destroy | |
@photo = Photo.unscoped.find(params[:id]) | |
authorize! :destroy, @photo | |
if @photo.destroy | |
redirect_to :back, :notice => 'Photo deleted.' | |
else | |
redirect_to :back, :alert => 'Error. Your photo was not deleted. If this problem persists please contact support.' | |
end | |
end | |
# Custom Actions | |
def update_states | |
@country = params[:country_code] | |
end | |
def tag | |
@photo = Photo.find(params[:id]) | |
@tag = Tag.find_or_create_by_name(params[:tag]) | |
authorize! :tag, @photo | |
Tagging.create(:photo => @photo, :tag => @tag) | |
end | |
def download | |
@photo = Photo.find(params[:id]) | |
if params[:size] == 'presentation' | |
authorize! :enlarge, @photo | |
@photo.increment!(:presentation_downloads) | |
redirect_to @photo.image_url(:full) | |
elsif params[:size] == 'full' | |
authorize! :download, @photo | |
@photo.increment!(:full_downloads) | |
redirect_to @photo.image_url | |
else | |
redirect_to :back, :alert => 'Invalid request.' | |
end | |
end | |
def toggle_featured | |
authorize! :manage, :site | |
@photo = Photo.find(params[:id]) | |
if params[:featured] == 'true' | |
@photo.featured = true | |
@photo.save | |
else | |
@photo.featured = false | |
@photo.save | |
end | |
redirect_to :back | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment