From 894b99d809682b3b079caa374b3be05f280d9cca Mon Sep 17 00:00:00 2001 From: Daniel Dixon Date: Thu, 14 Aug 2014 01:03:33 -0500 Subject: [PATCH] Allow Spree::StockLocation and Spree::ShippingMethod to also be found by admin name. Fix for 'undefined method with_indifferent_access for nil:NilClass' when not passing an address. Add specs to finds stock location by name or admin name Add specs to finds shipping method by name or admin name Add specs to find stock location by name or admin name Add specs to find stock location by name or admin name Add specs to find shipping method by name or admin name Fixes #33 --- .../wombat/handler/add_shipment_handler.rb | 4 +- .../wombat/handler/set_inventory_handler.rb | 2 +- .../wombat/handler/update_shipment_handler.rb | 6 +-- .../handler/add_shipment_handler_spec.rb | 40 ++++++++++++++++++ .../handler/set_inventory_handler_spec.rb | 42 ++++++++++++------- .../handler/update_shipment_handler_spec.rb | 42 ++++++++++++++++++- 6 files changed, 114 insertions(+), 22 deletions(-) diff --git a/lib/spree/wombat/handler/add_shipment_handler.rb b/lib/spree/wombat/handler/add_shipment_handler.rb index 93ee82a..e84e8cd 100644 --- a/lib/spree/wombat/handler/add_shipment_handler.rb +++ b/lib/spree/wombat/handler/add_shipment_handler.rb @@ -36,12 +36,12 @@ def process email = shipment.delete(:email) stock_location_name = shipment.delete(:stock_location) - stock_location = Spree::StockLocation.find_by_name(stock_location_name) + stock_location = Spree::StockLocation.find_by_name(stock_location_name) || Spree::StockLocation.find_by_admin_name(stock_location_name) return response("Can't find a StockLocation with name #{stock_location_name}!", 500) unless stock_location shipment[:stock_location_id] = stock_location.id shipping_method_name = shipment.delete(:shipping_method) - shipping_method = Spree::ShippingMethod.find_by_name(shipping_method_name) + shipping_method = Spree::ShippingMethod.find_by_name(shipping_method_name) || Spree::ShippingMethod.find_by_admin_name(shipping_method_name) return response("Can't find a ShippingMethod with name #{shipping_method_name}!", 500) unless shipping_method # build the inventory units diff --git a/lib/spree/wombat/handler/set_inventory_handler.rb b/lib/spree/wombat/handler/set_inventory_handler.rb index 96452b9..382042d 100644 --- a/lib/spree/wombat/handler/set_inventory_handler.rb +++ b/lib/spree/wombat/handler/set_inventory_handler.rb @@ -6,7 +6,7 @@ class SetInventoryHandler < Base def process stock_location_name = @payload[:inventory][:location] - stock_location = Spree::StockLocation.find_by_name(stock_location_name) + stock_location = Spree::StockLocation.find_by_name(stock_location_name) || Spree::StockLocation.find_by_admin_name(stock_location_name) return response("Stock location with name #{stock_location_name} was not found", 500) unless stock_location sku = @payload[:inventory][:product_id] diff --git a/lib/spree/wombat/handler/update_shipment_handler.rb b/lib/spree/wombat/handler/update_shipment_handler.rb index 78da479..1152854 100644 --- a/lib/spree/wombat/handler/update_shipment_handler.rb +++ b/lib/spree/wombat/handler/update_shipment_handler.rb @@ -39,16 +39,16 @@ def process email = shipment_hsh.delete(:email) stock_location_name = shipment_hsh.delete(:stock_location) - stock_location = Spree::StockLocation.find_by_name(stock_location_name) + stock_location = Spree::StockLocation.find_by_name(stock_location_name) || Spree::StockLocation.find_by_admin_name(stock_location_name) return response("Can't find a StockLocation with name #{stock_location_name}!", 500) unless stock_location shipment_hsh[:stock_location_id] = stock_location.id shipping_method_name = shipment_hsh.delete(:shipping_method) - shipping_method = Spree::ShippingMethod.find_by_name(shipping_method_name) + shipping_method = Spree::ShippingMethod.find_by_name(shipping_method_name) || Spree::ShippingMethod.find_by_admin_name(shipping_method_name) return response("Can't find a ShippingMethod with name #{shipping_method_name}!", 500) unless shipping_method shipment_attributes = shipment_hsh.slice *Spree::Shipment.attribute_names - shipment_attributes["address_attributes"] = address_attributes + shipment_attributes["address_attributes"] = address_attributes if address_attributes missing_variants = [] missing_line_items = [] diff --git a/spec/lib/spree/wombat/handler/add_shipment_handler_spec.rb b/spec/lib/spree/wombat/handler/add_shipment_handler_spec.rb index f5d88a5..47efa1b 100644 --- a/spec/lib/spree/wombat/handler/add_shipment_handler_spec.rb +++ b/spec/lib/spree/wombat/handler/add_shipment_handler_spec.rb @@ -55,6 +55,46 @@ module Wombat end end + context "finds stock location" do + context "when a stock location with name equal to stock_location in the message exists" do + it "find stock location by name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(order.reload.shipments.last.stock_location).to eql stock_location + end + end + + context "when a stock location with admin name equal to stock_location in the message exists" do + let!(:stock_location) { create(:stock_location, name: 'a stock location name', admin_name: 'default')} + + it "find stock location by admin name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(order.reload.shipments.last.stock_location).to eql stock_location + end + end + end + + context "finds shipping method" do + context "when a shipping method with name equal to shipping_method in the message exists" do + it "find a shipping method by name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(order.reload.shipments.last.shipping_method).to eql shipping_method + end + end + + context "when a shipping method with admin name equal to shipping_method in the message exists" do + let!(:shipping_method) { create(:shipping_method, name: 'a shipping method name', admin_name: 'UPS Ground (USD)')} + + it "find a shipping method by admin name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(order.reload.shipments.last.shipping_method).to eql shipping_method + end + end + end + context "finds state" do context "by abbr" do diff --git a/spec/lib/spree/wombat/handler/set_inventory_handler_spec.rb b/spec/lib/spree/wombat/handler/set_inventory_handler_spec.rb index d0d9e50..e851905 100644 --- a/spec/lib/spree/wombat/handler/set_inventory_handler_spec.rb +++ b/spec/lib/spree/wombat/handler/set_inventory_handler_spec.rb @@ -1,5 +1,24 @@ require 'spec_helper' +shared_examples "updates inventory level and responds with proper message" do + let!(:stock_item) do + item = variant.stock_items.first + item.stock_location = stock_location + item.save + item + end + + it "will set the inventory to the supplied amount" do + expect{handler.process}.to change{stock_item.reload.count_on_hand}.from(0).to(93) + end + + it "returns a Hub::Responder with a proper message" do + responder = handler.process + expect(responder.summary).to eql "Set inventory for SPREE-T-SHIRT at us_warehouse from 0 to 93" + expect(responder.code).to eql 200 + end +end + module Spree module Wombat describe Handler::SetInventoryHandler do @@ -9,23 +28,18 @@ module Wombat describe "process" do context "with stock item present" do - let!(:stock_location) { create(:stock_location, name: 'us_warehouse')} - let!(:variant) { create(:variant, :sku => 'SPREE-T-SHIRT') } - let!(:stock_item) do - item = variant.stock_items.first - item.stock_location = stock_location - item.save - item - end + let(:variant) { create(:variant, :sku => 'SPREE-T-SHIRT') } + + context "and the stock location name is equal to location in the message" do + let!(:stock_location) { create(:stock_location, name: 'us_warehouse')} - it "will set the inventory to the supplied amount" do - expect{handler.process}.to change{stock_item.reload.count_on_hand}.from(0).to(93) + include_examples "updates inventory level and responds with proper message" end - it "returns a Hub::Responder with a proper message" do - responder = handler.process - expect(responder.summary).to eql "Set inventory for SPREE-T-SHIRT at us_warehouse from 0 to 93" - expect(responder.code).to eql 200 + context "and the stock location admin name is equal to location in the message" do + let!(:stock_location) { create(:stock_location, name: 'a stock location name', admin_name: 'us_warehouse')} + + include_examples "updates inventory level and responds with proper message" end end diff --git a/spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb b/spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb index 10316f2..10e0a2f 100644 --- a/spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb +++ b/spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb @@ -79,10 +79,48 @@ module Wombat expect(shipment.reload.state).to eql 'pending' end end - end - end + context "finds stock location" do + context "when a stock location with name equal to stock_location in the message exists" do + it "find stock location by name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(shipment.reload.stock_location).to eql stock_location + end + end + + context "when a stock location with admin name equal to stock_location in the message exists" do + let!(:stock_location) { create(:stock_location, name: 'a stock location name', admin_name: 'default')} + + it "find stock location by admin name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(shipment.reload.stock_location).to eql stock_location + end + end + end + context "finds shipping method" do + context "when a shipping method with name equal to shipping_method in the message exists" do + it "find a shipping method by name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(shipment.reload.shipping_methods.first).to eql shipping_method + end + end + + context "when a shipping method with admin name equal to shipping_method in the message exists" do + let!(:shipping_method) { create(:shipping_method, name: 'a shipping method name', admin_name: 'UPS Ground (USD)')} + + it "find a shipping method by admin name" do + responder = handler.process + expect(responder.code).to eql 200 + expect(shipment.reload.shipping_methods.first).to eql shipping_method + end + end + end + end + end end end end