Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Allow Spree::StockLocation and Spree::ShippingMethod to also be found…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
onedanshow authored and Jeff Dutil committed Sep 16, 2014
1 parent 026c754 commit 2431c56
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/spree/wombat/handler/add_shipment_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/spree/wombat/handler/set_inventory_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions lib/spree/wombat/handler/update_shipment_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/spree/wombat/handler/add_shipment_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 28 additions & 14 deletions spec/lib/spree/wombat/handler/set_inventory_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
42 changes: 40 additions & 2 deletions spec/lib/spree/wombat/handler/update_shipment_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2431c56

Please sign in to comment.