From b8a08c932c2fa8b9c63058913147e9d8ab0988da Mon Sep 17 00:00:00 2001 From: Benjamin Wacha Date: Tue, 28 May 2019 18:50:15 +0300 Subject: [PATCH] minor refactors --- app/controllers/moments_controller.rb | 7 ++++--- app/models/moment.rb | 2 -- app/services/cloudinary_service.rb | 6 ++++-- spec/services/cloudinary_service_spec.rb | 2 +- .../uploads/tmp/moment => spec/uploads}/moment.jpeg | Bin 5 files changed, 9 insertions(+), 8 deletions(-) rename {public/uploads/tmp/moment => spec/uploads}/moment.jpeg (100%) diff --git a/app/controllers/moments_controller.rb b/app/controllers/moments_controller.rb index edcc0f5efc..8b8df742b4 100644 --- a/app/controllers/moments_controller.rb +++ b/app/controllers/moments_controller.rb @@ -73,11 +73,12 @@ def destroy # POST /moments/1/picture # POST /moments/1/picture.json def picture - moment = set_moment(params[:moment_id]) - cloudinary_response = CloudinaryService.upload(file, 'options') + # to do: add image upload options + cloudinary_response = CloudinaryService.upload(file, options) return if cloudinary_response.nil? + moment = set_moment(params[:moment_id]) moment.picture_id = cloudinary_response['public_id'] moment.save! end @@ -92,7 +93,7 @@ def set_moment def moment_params params.require(:moment).permit( - :name, :why, :fix, :comment, :published_at, :draft, :image, + :name, :why, :fix, :comment, :published_at, :draft, category: [], mood: [], viewers: [], strategy: [] ) end diff --git a/app/models/moment.rb b/app/models/moment.rb index 6ac443b160..5d23ed1da8 100644 --- a/app/models/moment.rb +++ b/app/models/moment.rb @@ -26,8 +26,6 @@ class Moment < ApplicationRecord include CommonMethods extend FriendlyId - mount_uploader :image, AvatarUploader - friendly_id :name serialize :category, Array serialize :viewers, Array diff --git a/app/services/cloudinary_service.rb b/app/services/cloudinary_service.rb index 17d83d886f..9481334763 100644 --- a/app/services/cloudinary_service.rb +++ b/app/services/cloudinary_service.rb @@ -5,13 +5,15 @@ class CloudinaryService class << self def upload(file, options = {}) - # to do: add exception handling Cloudinary::Uploader.upload(file, options) + rescue StandardError + nil end def delete(public_id, options = {}) - # to do: add exception handling Cloudinary::Uploader.destroy(public_id, options) + rescue StandardError + nil end end end diff --git a/spec/services/cloudinary_service_spec.rb b/spec/services/cloudinary_service_spec.rb index dfd09f2af7..e0021cf4bd 100644 --- a/spec/services/cloudinary_service_spec.rb +++ b/spec/services/cloudinary_service_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true FILENAME = "moment.jpeg" -FILE_PATH = "/public/uploads/tmp/moment" +FILE_PATH = "/spec/uploads/" OPTIONS = {} describe CloudinaryService do diff --git a/public/uploads/tmp/moment/moment.jpeg b/spec/uploads/moment.jpeg similarity index 100% rename from public/uploads/tmp/moment/moment.jpeg rename to spec/uploads/moment.jpeg