mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Convert spree/api/products_controller from rabl to ams
This commit is contained in:
@@ -11,15 +11,14 @@ module Spree
|
||||
else
|
||||
@products = product_scope.ransack(params[:q]).result
|
||||
end
|
||||
|
||||
@products = @products.page(params[:page]).per(params[:per_page])
|
||||
|
||||
respond_with(@products)
|
||||
render json: @products, each_serializer: ::Api::Admin::ProductSerializer
|
||||
end
|
||||
|
||||
def show
|
||||
@product = find_product(params[:id])
|
||||
respond_with(@product)
|
||||
render json: @product, serializer: ::Api::Admin::ProductSerializer
|
||||
end
|
||||
|
||||
def new; end
|
||||
@@ -30,7 +29,7 @@ module Spree
|
||||
@product = Product.new(params[:product])
|
||||
begin
|
||||
if @product.save
|
||||
respond_with(@product, status: 201, default_template: :show)
|
||||
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 201
|
||||
else
|
||||
invalid_resource!(@product)
|
||||
end
|
||||
@@ -44,7 +43,7 @@ module Spree
|
||||
authorize! :update, Product
|
||||
@product = find_product(params[:id])
|
||||
if @product.update_attributes(params[:product])
|
||||
respond_with(@product, status: 200, default_template: :show)
|
||||
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 200
|
||||
else
|
||||
invalid_resource!(@product)
|
||||
end
|
||||
@@ -55,7 +54,7 @@ module Spree
|
||||
@product = find_product(params[:id])
|
||||
@product.update_attribute(:deleted_at, Time.zone.now)
|
||||
@product.variants_including_master.update_all(deleted_at: Time.zone.now)
|
||||
respond_with(@product, 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.
|
||||
@@ -83,7 +82,7 @@ module Spree
|
||||
@product = find_product(params[:product_id])
|
||||
authorize! :delete, @product
|
||||
@product.destroy
|
||||
respond_with(@product, status: 204)
|
||||
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 204
|
||||
end
|
||||
|
||||
# POST /api/products/:product_id/clone
|
||||
@@ -95,7 +94,7 @@ module Spree
|
||||
|
||||
@product = original_product.duplicate
|
||||
|
||||
respond_with(@product, status: 201, default_template: :show)
|
||||
render json: @product, serializer: ::Api::Admin::ProductSerializer, status: 201
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
collection @products.order('id ASC')
|
||||
extends "spree/api/products/bulk_show"
|
||||
@@ -1,21 +0,0 @@
|
||||
object @product
|
||||
|
||||
# TODO: This is used by bulk product edit when a product is cloned.
|
||||
# But the list of products is serialized by Api::Admin::ProductSerializer.
|
||||
# This should probably be unified.
|
||||
|
||||
attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name, :on_demand, :inherits_properties
|
||||
attributes :on_hand, :price, :available_on, :permalink_live, :tax_category_id
|
||||
|
||||
# Infinity is not a valid JSON object, but Rails encodes it anyway
|
||||
node( :taxon_ids ) { |p| p.taxons.map(&:id).join(",") }
|
||||
node( :on_hand ) { |p| p.on_hand.nil? ? 0 : p.on_hand.to_f.finite? ? p.on_hand : t(:on_demand) }
|
||||
node( :price ) { |p| p.price.nil? ? '0.0' : p.price }
|
||||
|
||||
node( :available_on ) { |p| p.available_on.blank? ? "" : p.available_on.strftime("%F %T") }
|
||||
node( :permalink_live, &:permalink )
|
||||
node( :producer_id, &:supplier_id )
|
||||
node( :category_id, &:primary_taxon_id )
|
||||
node( :supplier ) do |p|
|
||||
partial 'api/enterprises/bulk_show', object: p.supplier
|
||||
end
|
||||
Reference in New Issue
Block a user