mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Decouple image upload from Spree
This commit is contained in:
@@ -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
|
||||
|
||||
19
app/controllers/api/product_images_controller.rb
Normal file
19
app/controllers/api/product_images_controller.rb
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user