From e9b5551c0f7bf2bda9d29c2747802c8662f003ef Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 1 Aug 2019 11:08:16 +0100 Subject: [PATCH] Adpat shipment controller to move out of Spree namespace --- app/controllers/api/shipments_controller.rb | 156 +++++++++--------- .../api/shipments_controller_spec.rb | 2 +- 2 files changed, 78 insertions(+), 80 deletions(-) diff --git a/app/controllers/api/shipments_controller.rb b/app/controllers/api/shipments_controller.rb index bf396287d3..4c9bb047eb 100644 --- a/app/controllers/api/shipments_controller.rb +++ b/app/controllers/api/shipments_controller.rb @@ -1,107 +1,105 @@ require 'open_food_network/scope_variant_to_hub' -module Spree - module Api - class ShipmentsController < ::Api::BaseController - respond_to :json +module Api + class ShipmentsController < Api::BaseController + respond_to :json - before_filter :find_order - before_filter :find_and_update_shipment, only: [:ship, :ready, :add, :remove] + before_filter :find_order + before_filter :find_and_update_shipment, only: [:ship, :ready, :add, :remove] - def create - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i - @shipment = get_or_create_shipment(params[:stock_location_id]) + def create + variant = scoped_variant(params[:variant_id]) + quantity = params[:quantity].to_i + @shipment = get_or_create_shipment(params[:stock_location_id]) - @order.contents.add(variant, quantity, nil, @shipment) + @order.contents.add(variant, quantity, nil, @shipment) - @shipment.refresh_rates - @shipment.save! + @shipment.refresh_rates + @shipment.save! - render json: @shipment.reload, serializer: ::Api::ShipmentSerializer, status: :ok + render json: @shipment.reload, serializer: Api::ShipmentSerializer, status: :ok + end + + def update + authorize! :read, Spree::Shipment + @shipment = @order.shipments.find_by_number!(params[:id]) + params[:shipment] ||= [] + unlock = params[:shipment].delete(:unlock) + + if unlock == 'yes' + @shipment.adjustment.open end - def update - authorize! :read, Shipment - @shipment = @order.shipments.find_by_number!(params[:id]) - params[:shipment] ||= [] - unlock = params[:shipment].delete(:unlock) + @shipment.update_attributes(params[:shipment]) - if unlock == 'yes' - @shipment.adjustment.open + if unlock == 'yes' + @shipment.adjustment.close + end + + render json: @shipment.reload, serializer: Api::ShipmentSerializer, status: :ok + end + + def ready + authorize! :read, Spree::Shipment + unless @shipment.ready? + if @shipment.can_ready? + @shipment.ready! + else + render(json: { error: I18n.t(:cannot_ready, scope: "spree.api.shipment") }, + status: :unprocessable_entity) && return end - - @shipment.update_attributes(params[:shipment]) - - if unlock == 'yes' - @shipment.adjustment.close - end - - render json: @shipment.reload, serializer: ::Api::ShipmentSerializer, status: :ok end + render json: @shipment, serializer: Api::ShipmentSerializer, status: :ok + end - def ready - authorize! :read, Shipment - unless @shipment.ready? - if @shipment.can_ready? - @shipment.ready! - else - render(json: { error: I18n.t(:cannot_ready, scope: "spree.api.shipment") }, - status: :unprocessable_entity) && return - end - end - render json: @shipment, serializer: ::Api::ShipmentSerializer, status: :ok + def ship + authorize! :read, Spree::Shipment + unless @shipment.shipped? + @shipment.ship! end + render json: @shipment, serializer: Api::ShipmentSerializer, status: :ok + end - def ship - authorize! :read, Shipment - unless @shipment.shipped? - @shipment.ship! - end - render json: @shipment, serializer: ::Api::ShipmentSerializer, status: :ok - end + def add + variant = scoped_variant(params[:variant_id]) + quantity = params[:quantity].to_i - def add - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i + @order.contents.add(variant, quantity, nil, @shipment) - @order.contents.add(variant, quantity, nil, @shipment) + render json: @shipment, serializer: Api::ShipmentSerializer, status: :ok + end - render json: @shipment, serializer: ::Api::ShipmentSerializer, status: :ok - end + def remove + variant = scoped_variant(params[:variant_id]) + quantity = params[:quantity].to_i - def remove - variant = scoped_variant(params[:variant_id]) - quantity = params[:quantity].to_i + @order.contents.remove(variant, quantity, @shipment) + @shipment.reload if @shipment.persisted? - @order.contents.remove(variant, quantity, @shipment) - @shipment.reload if @shipment.persisted? + render json: @shipment, serializer: Api::ShipmentSerializer, status: :ok + end - render json: @shipment, serializer: ::Api::ShipmentSerializer, status: :ok - end + private - private + def find_order + @order = Spree::Order.find_by_number!(params[:order_id]) + authorize! :read, @order + end - def find_order - @order = Spree::Order.find_by_number!(params[:order_id]) - authorize! :read, @order - end + def find_and_update_shipment + @shipment = @order.shipments.find_by_number!(params[:id]) + @shipment.update_attributes(params[:shipment]) + @shipment.reload + end - def find_and_update_shipment - @shipment = @order.shipments.find_by_number!(params[:id]) - @shipment.update_attributes(params[:shipment]) - @shipment.reload - end + def scoped_variant(variant_id) + variant = Spree::Variant.find(variant_id) + OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) + variant + end - def scoped_variant(variant_id) - variant = Spree::Variant.find(variant_id) - OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) - variant - end - - def get_or_create_shipment(stock_location_id) - @order.shipment || @order.shipments.create(stock_location_id: stock_location_id) - end + def get_or_create_shipment(stock_location_id) + @order.shipment || @order.shipments.create(stock_location_id: stock_location_id) end end end diff --git a/spec/controllers/api/shipments_controller_spec.rb b/spec/controllers/api/shipments_controller_spec.rb index 6bde1ff395..9e838617e3 100644 --- a/spec/controllers/api/shipments_controller_spec.rb +++ b/spec/controllers/api/shipments_controller_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Spree::Api::ShipmentsController, type: :controller do +describe Api::ShipmentsController, type: :controller do render_views let!(:shipment) { create(:shipment) }