From b2916e28f3390ac3dff804bb0f8a890d30223d84 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Fri, 31 May 2013 11:48:24 +0100 Subject: [PATCH] Don't recreate photos, but update metadata. --- app/controllers/photos_controller.rb | 5 +++-- app/views/photos/show.html.haml | 2 -- spec/controllers/photos_controller_spec.rb | 11 +++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 960fd36bc..f20d8ed77 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -46,13 +46,14 @@ class PhotosController < ApplicationController # POST /photos # POST /photos.json def create - @photo = Photo.new(params[:photo]) + @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 respond_to do |format| if @photo.save - format.html { redirect_to @photo, notice: 'Photo was successfully created.' } + 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" } diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index 912ca5113..6306f9994 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -1,5 +1,3 @@ -%p#notice= notice - = image_tag(@photo.fullsize_url, :alt => 'A plant', :class => 'img-rounded') %p diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index c61c7e244..4355c5bd9 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -91,6 +91,17 @@ describe PhotosController do end end + describe "for the second time" do + it "does not add a photo twice" do + expect { + post :create, {:photo => { :flickr_photo_id => 1 } } + }.to change(Photo, :count).by(1) + expect { + post :create, {:photo => { :flickr_photo_id => 1 } } + }.to change(Photo, :count).by(0) + end + end + describe "with invalid params" do it "assigns a newly created but unsaved photo as @photo" do # Trigger the behavior that occurs when invalid params are submitted