Decouple image upload from Spree

This commit is contained in:
Matt-Yorkley
2018-01-06 13:50:05 +00:00
parent 23deb37876
commit 8a11dc3c33
5 changed files with 22 additions and 18 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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