Add JSON api for products clone

This commit is contained in:
enricostano
2017-04-05 14:23:18 +02:00
committed by Rob Harrington
parent 1136cb40a9
commit 513330cfff
4 changed files with 50 additions and 4 deletions

View File

@@ -8,9 +8,6 @@ Spree::Admin::ProductsController.class_eval do
before_filter :load_spree_api_key, :only => [:bulk_edit, :variant_overrides]
before_filter :strip_new_properties, only: [:create, :update]
respond_to :json, :only => :clone
respond_override create: { html: {
success: lambda {
if params[:button] == "add_another"
@@ -22,7 +19,6 @@ Spree::Admin::ProductsController.class_eval do
failure: lambda {
render :new
} } }
#respond_override :clone => { :json => {:success => lambda { redirect_to bulk_index_admin_products_url+"?q[id_eq]=#{@new.id}" } } }
def product_distributions
end

View File

@@ -37,6 +37,17 @@ Spree::Api::ProductsController.class_eval do
respond_with(@product, :status => 204)
end
# POST /api/products/:product_id/clone
#
def clone
authorize! :create, Spree::Product
original_product = find_product(params[:product_id])
authorize! :update, original_product
@product = original_product.duplicate
respond_with(@product, status: 201, default_template: :show)
end
private

View File

@@ -237,6 +237,7 @@ Spree::Core::Engine.routes.prepend do
get :overridable
end
delete :soft_delete
post :clone
resources :variants do
delete :soft_delete

View File

@@ -107,5 +107,43 @@ module Spree
product1.deleted_at.should_not be_nil
end
end
describe '#clone' do
before do
spree_post :clone, product_id: product1.id, format: :json
end
context 'as a normal user' do
sign_in_as_user!
it 'denies access' do
assert_unauthorized!
end
end
context 'as an enterprise user' do
sign_in_as_enterprise_user! [:supplier]
it 'responds with a successful response' do
expect(response.status).to eq(201)
end
it 'clones the product' do
expect(json_response['name']).to eq("COPY OF #{product1.name}")
end
end
context 'as an administrator' do
sign_in_as_admin!
it 'responds with a successful response' do
expect(response.status).to eq(201)
end
it 'clones the product' do
expect(json_response['name']).to eq("COPY OF #{product1.name}")
end
end
end
end
end