From 180598c603fb35526b8d673d9757a981ffe06438 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 22 Jul 2019 18:41:47 +0100 Subject: [PATCH] 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 --- app/controllers/spree/api/variants_controller.rb | 15 ++++++++------- app/serializers/api/variant_serializer.rb | 6 ++++-- app/views/spree/api/products/bulk_show.v1.rabl | 8 +------- app/views/spree/api/variants/bulk_index.v1.rabl | 2 -- app/views/spree/api/variants/bulk_show.v1.rabl | 7 ------- .../spree/api/variants_controller_spec.rb | 15 +-------------- 6 files changed, 14 insertions(+), 39 deletions(-) delete mode 100644 app/views/spree/api/variants/bulk_index.v1.rabl delete mode 100644 app/views/spree/api/variants/bulk_show.v1.rabl diff --git a/app/controllers/spree/api/variants_controller.rb b/app/controllers/spree/api/variants_controller.rb index fe0fa1d793..e634a3900f 100644 --- a/app/controllers/spree/api/variants_controller.rb +++ b/app/controllers/spree/api/variants_controller.rb @@ -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 diff --git a/app/serializers/api/variant_serializer.rb b/app/serializers/api/variant_serializer.rb index b7cc365120..3972c6bf4c 100644 --- a/app/serializers/api/variant_serializer.rb +++ b/app/serializers/api/variant_serializer.rb @@ -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 diff --git a/app/views/spree/api/products/bulk_show.v1.rabl b/app/views/spree/api/products/bulk_show.v1.rabl index 3baf5a5069..0786c5d844 100644 --- a/app/views/spree/api/products/bulk_show.v1.rabl +++ b/app/views/spree/api/products/bulk_show.v1.rabl @@ -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 \ No newline at end of file diff --git a/app/views/spree/api/variants/bulk_index.v1.rabl b/app/views/spree/api/variants/bulk_index.v1.rabl deleted file mode 100644 index 69f1b82f5f..0000000000 --- a/app/views/spree/api/variants/bulk_index.v1.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @variants -extends "spree/api/variants/bulk_show" diff --git a/app/views/spree/api/variants/bulk_show.v1.rabl b/app/views/spree/api/variants/bulk_show.v1.rabl deleted file mode 100644 index 8044ded0a0..0000000000 --- a/app/views/spree/api/variants/bulk_show.v1.rabl +++ /dev/null @@ -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 } diff --git a/spec/controllers/spree/api/variants_controller_spec.rb b/spec/controllers/spree/api/variants_controller_spec.rb index e32f34809a..5ffede16d0 100644 --- a/spec/controllers/spree/api/variants_controller_spec.rb +++ b/spec/controllers/spree/api/variants_controller_spec.rb @@ -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