Convert spree/api/variants_controller to AMS by changing base_controller, using render json instad of respond with, deleting rabl templates and adapting specs

Delete unused pagination spec
This commit is contained in:
luisramos0
2019-07-22 18:41:47 +01:00
parent 69a5527e24
commit 180598c603
6 changed files with 14 additions and 39 deletions

View File

@@ -1,19 +1,20 @@
module Spree
module Api
class VariantsController < Spree::Api::BaseController
class VariantsController < ::Api::BaseController
respond_to :json
skip_authorization_check only: :index
before_filter :product
def index
@variants = scope.includes(:option_values).ransack(params[:q]).result.
page(params[:page]).per(params[:per_page])
respond_with(@variants)
render json: @variants, each_serializer: ::Api::VariantSerializer
end
def show
@variant = scope.includes(:option_values).find(params[:id])
respond_with(@variant)
render json: @variant, serializer: ::Api::VariantSerializer
end
def new; end
@@ -22,7 +23,7 @@ module Spree
authorize! :create, Variant
@variant = scope.new(params[:variant])
if @variant.save
respond_with(@variant, status: 201, default_template: :show)
render json: @variant, serializer: ::Api::VariantSerializer, status: 201
else
invalid_resource!(@variant)
end
@@ -32,7 +33,7 @@ module Spree
authorize! :update, Variant
@variant = scope.find(params[:id])
if @variant.update_attributes(params[:variant])
respond_with(@variant, status: 200, default_template: :show)
render json: @variant, serializer: ::Api::VariantSerializer, status: 200
else
invalid_resource!(@product)
end
@@ -43,14 +44,14 @@ module Spree
authorize! :delete, @variant
VariantDeleter.new.delete(@variant)
respond_with @variant, status: 204
render json: @variant, serializer: ::Api::VariantSerializer, status: 204
end
def destroy
authorize! :delete, Variant
@variant = scope.find(params[:id])
@variant.destroy
respond_with(@variant, status: 204)
render json: @variant, serializer: ::Api::VariantSerializer, status: 204
end
private

View File

@@ -1,6 +1,8 @@
class Api::VariantSerializer < ActiveModel::Serializer
attributes :id, :is_master, :on_hand, :name_to_display, :unit_to_display, :unit_value
attributes :options_text, :on_demand, :price, :fees, :price_with_fees, :product_name
attributes :id, :is_master, :product_name
attributes :options_text, :unit_value, :unit_description, :unit_to_display
attributes :display_as, :display_name, :name_to_display
attributes :price, :on_demand, :on_hand, :fees, :price_with_fees
attributes :tag_list
delegate :price, to: :object

View File

@@ -18,10 +18,4 @@ node( :producer_id, &:supplier_id )
node( :category_id, &:primary_taxon_id )
node( :supplier ) do |p|
partial 'api/enterprises/bulk_show', object: p.supplier
end
node( :variants ) do |p|
partial 'spree/api/variants/bulk_index', object: p.variants.reorder('spree_variants.id ASC')
end
node( :master ) do |p|
partial 'spree/api/variants/bulk_show', object: p.master
end
end

View File

@@ -1,2 +0,0 @@
collection @variants
extends "spree/api/variants/bulk_show"

View File

@@ -1,7 +0,0 @@
object @variant
attributes :id, :options_text, :unit_value, :unit_description, :on_demand, :display_as, :display_name
# Infinity is not a valid JSON object, but Rails encodes it anyway
node( :on_hand ) { |v| v.on_hand.nil? ? 0 : ( v.on_hand.to_f.finite? ? v.on_hand : t(:on_demand) ) }
node( :price ) { |v| v.price.nil? ? 0.to_f : v.price }

View File

@@ -29,7 +29,7 @@ module Spree
end
it "retrieves a list of variants with appropriate attributes" do
spree_get :index, template: 'bulk_index', format: :json
spree_get :index, format: :json
keys = json_response.first.keys.map(&:to_sym)
expect(attributes.all?{ |attr| keys.include? attr }).to eq(true)
@@ -108,19 +108,6 @@ module Spree
end
end
context "pagination" do
it "can select the next page of variants" do
second_variant = create(:variant)
api_get :index, page: 2, per_page: 1
keys = json_response["variants"].first.keys.map(&:to_sym)
expect(standard_attributes.all?{ |attr| keys.include? attr }).to eq(true)
expect(json_response["total_count"]).to eq(14)
expect(json_response["current_page"]).to eq(2)
expect(json_response["pages"]).to eq(14)
end
end
it "can see a single variant" do
api_get :show, id: variant.to_param