From b42c9896bc62e17be267e682f280f39877108e53 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 4 Jul 2019 01:47:55 +0200 Subject: [PATCH] Migrate pending profile photos and fix upload to unpend profile photos fixes #8043 closes #8044 --- app/controllers/photos_controller.rb | 7 +------ db/migrate/20190703231700_fix_pending_profile_photos.rb | 9 +++++++++ spec/controllers/photos_controller_spec.rb | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20190703231700_fix_pending_profile_photos.rb diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 1f8a37182..52d44732f 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -147,12 +147,7 @@ class PhotosController < ApplicationController current_user.dispatch_post(@photo, to: photo_params[:aspect_ids]) end - if photo_params[:set_profile_photo] - profile_params = {:image_url => @photo.url(:thumb_large), - :image_url_medium => @photo.url(:thumb_medium), - :image_url_small => @photo.url(:thumb_small)} - current_user.update_profile(profile_params) - end + current_user.update_profile(photo: @photo) if photo_params[:set_profile_photo] respond_to do |format| format.json{ render(:layout => false , :json => {"success" => true, "data" => @photo}.to_json )} diff --git a/db/migrate/20190703231700_fix_pending_profile_photos.rb b/db/migrate/20190703231700_fix_pending_profile_photos.rb new file mode 100644 index 000000000..51b3a1512 --- /dev/null +++ b/db/migrate/20190703231700_fix_pending_profile_photos.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class FixPendingProfilePhotos < ActiveRecord::Migration[5.1] + def up + Photo.where(pending: true).each do |photo| + photo.update(pending: false) if Profile.where(image_url: photo.url(:thumb_large)).exists? + end + end +end diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index 424eeb235..f9f17762e 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -44,7 +44,7 @@ describe PhotosController, :type => :controller do describe '#create' do before do allow(@controller).to receive(:file_handler).and_return(uploaded_photo) - @params = {:photo => {:user_file => uploaded_photo, :aspect_ids => "all"} } + @params = {photo: {user_file: uploaded_photo, aspect_ids: "all", pending: true}} end it "creates a photo" do @@ -67,11 +67,13 @@ describe PhotosController, :type => :controller do expect(Photo.last.author).to eq(alice.person) end - it 'can set the photo as the profile photo' do + it "can set the photo as the profile photo and unpends the photo" do old_url = alice.person.profile.image_url @params[:photo][:set_profile_photo] = true post :create, params: @params - expect(alice.reload.person.profile.image_url).not_to eq(old_url) + new_url = alice.reload.person.profile.image_url + expect(new_url).not_to eq(old_url) + expect(Photo.find_by(remote_photo_name: new_url.rpartition("_").last).pending).to be_falsey end end