From 8a11dc3c337bf208363e4f60867b3641ba2ac027 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Sat, 6 Jan 2018 13:50:05 +0000 Subject: [PATCH] Decouple image upload from Spree --- .../services/product_image_service.js.coffee | 2 +- .../api/product_images_controller.rb | 19 +++++++++++++++++++ .../spree/api/images_controller_decorator.rb | 15 --------------- .../update_product_image.v1.rabl | 0 config/routes.rb | 4 ++-- 5 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 app/controllers/api/product_images_controller.rb delete mode 100644 app/controllers/spree/api/images_controller_decorator.rb rename app/views/{spree/api/images => api/product_images}/update_product_image.v1.rabl (100%) diff --git a/app/assets/javascripts/admin/products/services/product_image_service.js.coffee b/app/assets/javascripts/admin/products/services/product_image_service.js.coffee index 8bad8746ec..8b0f273e8b 100644 --- a/app/assets/javascripts/admin/products/services/product_image_service.js.coffee +++ b/app/assets/javascripts/admin/products/services/product_image_service.js.coffee @@ -8,7 +8,7 @@ angular.module("ofn.admin").factory "ProductImageService", (FileUploader, SpreeA autoUpload: true configure: (product) => - @imageUploader.url = "/api/images/product/#{product.id}" + @imageUploader.url = "/api/product_images/#{product.id}" @imagePreview = product.image_url @imageUploader.onSuccessItem = (image, response) => product.thumb_url = response.thumb_url diff --git a/app/controllers/api/product_images_controller.rb b/app/controllers/api/product_images_controller.rb new file mode 100644 index 0000000000..adcf978c51 --- /dev/null +++ b/app/controllers/api/product_images_controller.rb @@ -0,0 +1,19 @@ +module Api + class ProductImagesController < Spree::Api::BaseController + respond_to :json + + def update_product_image + @product = Spree::Product.find(params[:product_id]) + authorize! :update, @product + + if @product.images.first.nil? + @image = Spree::Image.create(attachment: params[:file], viewable_id: @product.master.id, viewable_type: 'Spree::Variant') + respond_with(@image, status: 201) + else + @image = @product.images.first + @image.update_attributes(attachment: params[:file]) + respond_with(@image, status: 200) + end + end + end +end diff --git a/app/controllers/spree/api/images_controller_decorator.rb b/app/controllers/spree/api/images_controller_decorator.rb deleted file mode 100644 index 3f5f121eaa..0000000000 --- a/app/controllers/spree/api/images_controller_decorator.rb +++ /dev/null @@ -1,15 +0,0 @@ -Spree::Api::ImagesController.class_eval do - def update_product_image - @product = Spree::Product.find(params[:product_id]) - authorize! :update, @product - - if @product.images.first.nil? - @image = Spree::Image.create(attachment: params[:file], viewable_id: @product.master.id, viewable_type: 'Spree::Variant') - respond_with(@image, status: 201) - else - @image = @product.images.first - @image.update_attributes(attachment: params[:file]) - respond_with(@image, status: 200) - end - end -end diff --git a/app/views/spree/api/images/update_product_image.v1.rabl b/app/views/api/product_images/update_product_image.v1.rabl similarity index 100% rename from app/views/spree/api/images/update_product_image.v1.rabl rename to app/views/api/product_images/update_product_image.v1.rabl diff --git a/config/routes.rb b/config/routes.rb index 358daf5dcb..9d79e32300 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -210,6 +210,8 @@ Openfoodnetwork::Application.routes.draw do resource :status do get :job_queue end + + post '/product_images/:product_id', to: 'product_images#update_product_image' end namespace :open_food_network do @@ -285,8 +287,6 @@ Spree::Core::Engine.routes.prepend do resources :orders do get :managed, on: :collection end - - post '/images/product/:product_id', to: 'images#update_product_image' end namespace :admin do