Files
growstuff/app/controllers/photos_controller.rb
Skud 6ecbf749eb Avoid duplicate photos on plantings (or vice versa)
Strictly speaking this doesn't prevent you adding them if you really
try, but the validation for that was beyond what I could figure out (the
docs don't help, and all SO/blog posts are outdated and didn't
work).

However, if you do somehow manage to add dups, you will never see them
again thanks to the :uniq => true in the model.  That's good enough for
me.
2013-06-01 12:07:17 +10:00

107 lines
2.6 KiB
Ruby

class PhotosController < ApplicationController
load_and_authorize_resource
# GET /photos
# GET /photos.json
def index
@photos = Photo.paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @photos }
end
end
# GET /photos/1
# GET /photos/1.json
def show
@photo = Photo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @photo }
end
end
# GET /photos/new
# GET /photos/new.json
def new
@photo = Photo.new
@planting_id = params[:planting_id]
@flickr_auth = current_member.auth('flickr')
if @flickr_auth
@photos = current_member.flickr_photos
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @photo }
end
end
# GET /photos/1/edit
def edit
@photo = Photo.find(params[:id])
end
# POST /photos
# POST /photos.json
def create
@photo = Photo.find_by_flickr_photo_id(params[:photo][:flickr_photo_id]) ||
Photo.new(params[:photo])
@photo.owner_id = current_member.id
@photo.set_flickr_metadata
if params[:planting_id]
planting = Planting.find_by_id(params[:planting_id])
if planting
if planting.owner.id == current_member.id
@photo.plantings << planting unless @photo.plantings.include?(planting)
else
flash[:alert] = "You must own both the planting and the photo."
end
else
flash[:alert] = "Couldn't find planting to connect to photo."
end
end
respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully added.' }
format.json { render json: @photo, status: :created, location: @photo }
else
format.html { render action: "new" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
# PUT /photos/1
# PUT /photos/1.json
def update
@photo = Photo.find(params[:id])
respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /photos/1
# DELETE /photos/1.json
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
respond_to do |format|
format.html { redirect_to photos_url }
format.json { head :no_content }
end
end
end