Adapt api products and variants controllers to new namespace outside of Spree

This commit is contained in:
luisramos0
2019-08-01 18:34:19 +01:00
parent aa3c1aa0fe
commit 4aa6c673ff
2 changed files with 25 additions and 25 deletions

View File

@@ -1,23 +1,23 @@
require 'open_food_network/permissions'
module Api
class ProductsController < ::Api::BaseController
class ProductsController < Api::BaseController
respond_to :json
skip_authorization_check only: [:show, :bulk_products, :overridable]
def show
@product = find_product(params[:id])
render json: @product, serializer: ::Api::Admin::ProductSerializer
render json: @product, serializer: Api::Admin::ProductSerializer
end
def create
authorize! :create, Product
authorize! :create, Spree::Product
params[:product][:available_on] ||= Time.zone.now
@product = Product.new(params[:product])
@product = Spree::Product.new(params[:product])
begin
if @product.save
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 201
render json: @product, serializer: Api::Admin::ProductSerializer, status: 201
else
invalid_resource!(@product)
end
@@ -28,21 +28,21 @@ module Api
end
def update
authorize! :update, Product
authorize! :update, Spree::Product
@product = find_product(params[:id])
if @product.update_attributes(params[:product])
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 200
render json: @product, serializer: Api::Admin::ProductSerializer, status: 200
else
invalid_resource!(@product)
end
end
def destroy
authorize! :delete, Product
authorize! :delete, Spree::Product
@product = find_product(params[:id])
@product.update_attribute(:deleted_at, Time.zone.now)
@product.variants_including_master.update_all(deleted_at: Time.zone.now)
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 204
render json: @product, serializer: Api::Admin::ProductSerializer, status: 204
end
# TODO: This should be named 'managed'. Is the action above used? Maybe we should remove it.
@@ -70,7 +70,7 @@ module Api
@product = find_product(params[:product_id])
authorize! :delete, @product
@product.destroy
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 204
render json: @product, serializer: Api::Admin::ProductSerializer, status: 204
end
# POST /api/products/:product_id/clone
@@ -82,12 +82,12 @@ module Api
@product = original_product.duplicate
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 201
render json: @product, serializer: Api::Admin::ProductSerializer, status: 201
end
private
# Copied and modified from Spree::Api::BaseController to allow
# Copied and modified from SpreeApi::BaseController to allow
# enterprise users to access inactive products
def product_scope
# This line modified
@@ -115,7 +115,7 @@ module Api
def render_paged_products(products)
serializer = ActiveModel::ArraySerializer.new(
products,
each_serializer: ::Api::Admin::ProductSerializer
each_serializer: Api::Admin::ProductSerializer
)
render text: { products: serializer, pages: products.num_pages }.to_json

View File

@@ -1,5 +1,5 @@
module Api
class VariantsController < ::Api::BaseController
class VariantsController < Api::BaseController
respond_to :json
skip_authorization_check only: [:index, :show]
@@ -7,29 +7,29 @@ module Api
def index
@variants = scope.includes(:option_values).ransack(params[:q]).result
render json: @variants, each_serializer: ::Api::VariantSerializer
render json: @variants, each_serializer: Api::VariantSerializer
end
def show
@variant = scope.includes(:option_values).find(params[:id])
render json: @variant, serializer: ::Api::VariantSerializer
render json: @variant, serializer: Api::VariantSerializer
end
def create
authorize! :create, Variant
authorize! :create, Spree::Variant
@variant = scope.new(params[:variant])
if @variant.save
render json: @variant, serializer: ::Api::VariantSerializer, status: 201
render json: @variant, serializer: Api::VariantSerializer, status: 201
else
invalid_resource!(@variant)
end
end
def update
authorize! :update, Variant
authorize! :update, Spree::Variant
@variant = scope.find(params[:id])
if @variant.update_attributes(params[:variant])
render json: @variant, serializer: ::Api::VariantSerializer, status: 200
render json: @variant, serializer: Api::VariantSerializer, status: 200
else
invalid_resource!(@product)
end
@@ -40,14 +40,14 @@ module Api
authorize! :delete, @variant
VariantDeleter.new.delete(@variant)
render json: @variant, serializer: ::Api::VariantSerializer, status: 204
render json: @variant, serializer: Api::VariantSerializer, status: 204
end
def destroy
authorize! :delete, Variant
authorize! :delete, Spree::Variant
@variant = scope.find(params[:id])
@variant.destroy
render json: @variant, serializer: ::Api::VariantSerializer, status: 204
render json: @variant, serializer: Api::VariantSerializer, status: 204
end
private
@@ -64,10 +64,10 @@ module Api
variants = @product.variants_including_master.with_deleted
end
else
variants = Variant.scoped
variants = Spree::Variant.scoped
if current_api_user.has_spree_role?("admin")
unless params[:show_deleted]
variants = Variant.active
variants = Spree::Variant.active
end
else
variants = variants.active