2008-10-01 02:19:35 -04:00
|
|
|
class Photos < Application
|
2008-10-15 02:31:50 -04:00
|
|
|
before :logged_in?, :only => [ :new, :create, :destroy ]
|
|
|
|
before :administrator?, :only => [ :destroy, :approve, :moderate ]
|
2008-10-11 04:22:58 -04:00
|
|
|
before :make_photo, :only => [ :new, :create ]
|
2008-10-15 02:31:50 -04:00
|
|
|
before :fetch_photo, :only => [ :show, :destroy, :thumbnail, :approve, :flag ]
|
2008-10-11 04:22:58 -04:00
|
|
|
|
2008-10-01 02:19:35 -04:00
|
|
|
def index
|
2008-10-11 04:22:58 -04:00
|
|
|
@page = params[:page].to_i
|
|
|
|
per_page = 24
|
|
|
|
@page_count = (Photo.count(:id).to_f / per_page.to_f).ceil
|
|
|
|
@photos = Photo.find :all, :order => 'id DESC', :limit => per_page, :offset => (per_page * @page)
|
|
|
|
if request.xhr?
|
|
|
|
partial 'photos/photo_browser'
|
|
|
|
else
|
|
|
|
render
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2008-10-01 02:19:35 -04:00
|
|
|
render
|
|
|
|
end
|
|
|
|
|
2008-10-11 04:22:58 -04:00
|
|
|
def create
|
2008-10-15 02:31:50 -04:00
|
|
|
@photo.user = current_user
|
2008-10-11 04:22:58 -04:00
|
|
|
if @photo.save
|
|
|
|
flash[:notice] = 'Great success'
|
|
|
|
redirect url(:photo, @photo)
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-15 02:31:50 -04:00
|
|
|
def destroy
|
2008-10-11 18:36:58 -04:00
|
|
|
raise NotAcceptable unless request.xhr?
|
2008-10-15 02:31:50 -04:00
|
|
|
if @photo.destroy
|
|
|
|
render '', :status => 200
|
2008-10-11 04:22:58 -04:00
|
|
|
else
|
2008-10-15 02:31:50 -04:00
|
|
|
render '', :status => 401
|
2008-10-11 04:22:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def thumbnail
|
|
|
|
if @photo.exist?
|
|
|
|
w = params[:width].to_i
|
|
|
|
w = @photo.width if w == 0 or w > @photo.width
|
|
|
|
h = params[:height].to_i
|
|
|
|
h = @photo.height if h == 0 or h > @photo.height
|
|
|
|
w = h if h > w
|
|
|
|
send_data get_photo_version(w, w).to_blob, :filename => @photo.filename, :disposition => 'inline', :type => @photo.content_type
|
|
|
|
else
|
|
|
|
send_file Merb.root + '/public/images/image-missing.png', :disposition => 'inline'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-15 02:31:50 -04:00
|
|
|
def approve
|
|
|
|
raise NotAllowed unless request.xhr?
|
|
|
|
@photo.approved = true
|
|
|
|
if @photo.save
|
|
|
|
render '', :status => 200
|
|
|
|
else
|
|
|
|
render '', :status => 401
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flag
|
|
|
|
raise NotAllowed unless request.xhr?
|
|
|
|
pf = PhotoFlag.new :photo_id => @photo.id, :user_id => current_user.id
|
|
|
|
if pf.save
|
|
|
|
render '', :status => 200
|
|
|
|
else
|
|
|
|
render '', :status => 401
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def moderate
|
|
|
|
@photos = Photo.find :all, :order => "photo_flags_count DESC, id ASC", :conditions => [ 'approved = ?', false ]
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
2008-10-11 04:22:58 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def make_photo
|
|
|
|
@photo = Photo.new params[:photo]
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_photo
|
|
|
|
@photo = Photo.find params[:id]
|
|
|
|
raise NotFound unless @photo
|
|
|
|
end
|
2008-10-01 02:19:35 -04:00
|
|
|
end
|