From c3132e87d6d5b62bfb3ec6064a2cd0b993017ac2 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 30 Apr 2018 13:49:52 +0200 Subject: [PATCH 1/3] Rename and fix simple_product to base_product This factory has been merged into the base_product factory. Besides, products no longer have on_hand attribute. --- app/models/spree/product_decorator.rb | 1 - spec/factories.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 5f2eaab5f6..06970f0a13 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -246,7 +246,6 @@ Spree::Product.class_eval do variant = self.master.dup variant.product = self variant.is_master = false - variant.on_demand = self.on_demand self.variants << variant end end diff --git a/spec/factories.rb b/spec/factories.rb index ebe8d05dd8..4731967055 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -460,7 +460,8 @@ FactoryGirl.modify do factory :product do primary_taxon { Spree::Taxon.first || FactoryGirl.create(:taxon) } end - factory :simple_product do + + factory :base_product do # Fix product factory name sequence with Kernel.rand so it is not interpreted as a Spree::Product method # Pull request: https://github.com/spree/spree/pull/1964 # When this fix has been merged into a version of Spree that we're using, this line can be removed. @@ -468,7 +469,6 @@ FactoryGirl.modify do supplier { Enterprise.is_primary_producer.first || FactoryGirl.create(:supplier_enterprise) } primary_taxon { Spree::Taxon.first || FactoryGirl.create(:taxon) } - on_hand 3 unit_value 1 unit_description '' From 6784621c1be10989894912b370c340f0bf776122 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 30 Apr 2018 13:53:23 +0200 Subject: [PATCH 2/3] Remove invalid spree config According to https://github.com/spree/spree/commit/5ef8dc67dffe8950805bb8a7c9acb62c76aa7a2f "Backorders can be configured now as per stock location or stock item". --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0679613855..4d6410ec11 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -114,7 +114,6 @@ RSpec.configure do |config| spree_config.currency = currency spree_config.shipping_instructions = true spree_config.auto_capture = true - spree_config.allow_backorders = false end Spree::Api::Config[:requires_authentication] = true From 776b5a23f6ea694ecc810eacfa182c4b2e9be034 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 30 Apr 2018 13:54:40 +0200 Subject: [PATCH 3/3] Skip methods that distributor can't use in Package This ensures that Spree's shipping methods take into account the ones the order's distributor uses. This reimplements what `#available_to_order_with_distributor_check?` used to do and it doesn't work with Spree 2.0.4. --- app/models/spree/shipping_method_decorator.rb | 6 --- app/models/stock/package.rb | 33 ++++++++++++ config/initializers/spree.rb | 2 + spec/models/stock/package_spec.rb | 51 +++++++++++++++++++ 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 app/models/stock/package.rb create mode 100644 spec/models/stock/package_spec.rb diff --git a/app/models/spree/shipping_method_decorator.rb b/app/models/spree/shipping_method_decorator.rb index a900f14936..6a512737bd 100644 --- a/app/models/spree/shipping_method_decorator.rb +++ b/app/models/spree/shipping_method_decorator.rb @@ -42,12 +42,6 @@ Spree::ShippingMethod.class_eval do ] end - def available_to_order_with_distributor_check?(order) - available_to_order_without_distributor_check?(order) && - self.distributors.include?(order.distributor) - end - alias_method_chain :available_to_order?, :distributor_check - def within_zone?(order) if order.ship_address zone && zone.include?(order.ship_address) diff --git a/app/models/stock/package.rb b/app/models/stock/package.rb new file mode 100644 index 0000000000..012e2265d4 --- /dev/null +++ b/app/models/stock/package.rb @@ -0,0 +1,33 @@ +# Extends Spree's Package implementation to skip shipping methods that are not +# valid for OFN. +# +# It requires the following configuration in config/initializers/spree.rb: +# +# Spree.config do |config| +# ... +# config.package_factory = Stock::Package +# end +# +module Stock + class Package < Spree::Stock::Package + # Skips the methods that are not used by the order's distributor + # + # @return [Array] + def shipping_methods + super.delete_if do |shipping_method| + !ships_with?(order.distributor, shipping_method) + end + end + + private + + # Checks whether the given distributor provides the specified shipping method + # + # @param distributor [Spree::Enterprise] + # @param shipping_method [Spree::ShippingMethod] + # @return [Boolean] + def ships_with?(distributor, shipping_method) + distributor.shipping_methods.include?(shipping_method) + end + end +end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 8495ec3824..77243c8ebb 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -20,6 +20,8 @@ Spree.config do |config| # Auto-capture payments. Without this option, payments must be manually captured in the paypal interface. config.auto_capture = true #config.override_actionmailer_config = false + + config.package_factory = Stock::Package end # TODO Work out why this is necessary diff --git a/spec/models/stock/package_spec.rb b/spec/models/stock/package_spec.rb new file mode 100644 index 0000000000..2c85b9b079 --- /dev/null +++ b/spec/models/stock/package_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +module Stock + describe Package do + describe '#shipping_methods' do + let(:stock_location) { double(:stock_location) } + let(:order) { build(:order) } + + subject(:package) { Package.new(stock_location, order, contents) } + + describe '#shipping_methods' do + let(:enterprise) { build(:enterprise) } + let(:other_enterprise) { build(:enterprise) } + + let(:order) { build(:order, distributor: enterprise) } + + let(:variant1) do + instance_double( + Spree::Variant, + shipping_category: shipping_method1.shipping_categories.first + ) + end + let(:variant2) do + instance_double( + Spree::Variant, + shipping_category: shipping_method2.shipping_categories.first + ) + end + let(:variant3) do + instance_double(Spree::Variant, shipping_category: nil) + end + + let(:contents) do + [ + Package::ContentItem.new(variant1, 1), + Package::ContentItem.new(variant1, 1), + Package::ContentItem.new(variant2, 1), + Package::ContentItem.new(variant3, 1) + ] + end + + let(:shipping_method1) { create(:shipping_method, distributors: [enterprise]) } + let(:shipping_method2) { create(:shipping_method, distributors: [other_enterprise]) } + + it 'does not return shipping methods not used by the package\'s order distributor' do + expect(package.shipping_methods).to eq [shipping_method1] + end + end + end + end +end