Move Stock::Package to OrderManagement::Stock::Package

This commit is contained in:
Luis Ramos
2020-07-02 14:37:00 +01:00
parent 1b28592f58
commit 943cb7bf05
6 changed files with 111 additions and 105 deletions

View File

@@ -1,45 +0,0 @@
# 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
# Returns all existing shipping categories.
# It does not filter by the shipping categories of the products in the order.
# It allows checkout of products with categories that are not the shipping methods categories
# It disables the matching of product shipping category with shipping method's category
#
# @return [Array<Spree::ShippingCategory>]
def shipping_categories
Spree::ShippingCategory.all
end
# Skips the methods that are not used by the order's distributor
#
# @return [Array<Spree::ShippingMethod>]
def shipping_methods
available_shipping_methods = super.to_a
available_shipping_methods.keep_if do |shipping_method|
ships_with?(order.distributor.shipping_methods.to_a, shipping_method)
end
end
private
# Checks whether the given distributor provides the specified shipping method
#
# @param shipping_methods [Array<Spree::ShippingMethod>]
# @param shipping_method [Spree::ShippingMethod]
# @return [Boolean]
def ships_with?(shipping_methods, shipping_method)
shipping_methods.include?(shipping_method)
end
end
end

View File

@@ -30,7 +30,7 @@ Spree.config do |config|
config.auto_capture = true
#config.override_actionmailer_config = false
config.package_factory = Stock::Package
config.package_factory = OrderManagement::Stock::Package
config.order_updater_decorator = OrderUpdater
# S3 settings

View File

@@ -0,0 +1,49 @@
# frozen_string_literal: true
# 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 = OrderManagement::Stock::Package
# end
#
module OrderManagement
module Stock
class Package < Spree::Stock::Package
# Returns all existing shipping categories.
# It does not filter by the shipping categories of the products in the order: it allows
# checkout of products with categories that are not the shipping method's categories
# It disables the matching of product shipping category with shipping method's category
#
# @return [Array<Spree::ShippingCategory>]
def shipping_categories
Spree::ShippingCategory.all
end
# Skips the methods that are not used by the order's distributor
#
# @return [Array<Spree::ShippingMethod>]
def shipping_methods
available_shipping_methods = super.to_a
available_shipping_methods.keep_if do |shipping_method|
ships_with?(order.distributor.shipping_methods.to_a, shipping_method)
end
end
private
# Checks whether the given distributor provides the specified shipping method
#
# @param shipping_methods [Array<Spree::ShippingMethod>]
# @param shipping_method [Spree::ShippingMethod]
# @return [Boolean]
def ships_with?(shipping_methods, shipping_method)
shipping_methods.include?(shipping_method)
end
end
end
end

View File

@@ -0,0 +1,60 @@
require 'spec_helper'
module OrderManagement
module Stock
describe Package do
let(:stock_location) { double(:stock_location) }
subject(:package) { Package.new(stock_location, order, contents) }
let(:enterprise) { create(:enterprise) }
let(:other_enterprise) { create(: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]) }
describe '#shipping_methods' do
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
describe '#shipping_categories' do
it "returns shipping categories that are not shipping categories of the order's products" do
package
other_shipping_category = Spree::ShippingCategory.create(name: "Custom")
expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first,
other_shipping_category]
end
end
end
end
end

View File

@@ -14,7 +14,7 @@ describe Spree::Calculator::FlatPercentItemTotal do
it_behaves_like "a model using the LocalizedNumber module", [:preferred_flat_percent]
end
it "computes amount correctly for a given Stock::Package" do
it "computes amount correctly for a given OrderManagement::Stock::Package" do
order = double(:order, line_items: [line_item] )
package = double(:package, order: order)

View File

@@ -1,58 +0,0 @@
require 'spec_helper'
module Stock
describe Package do
let(:stock_location) { double(:stock_location) }
subject(:package) { Package.new(stock_location, order, contents) }
let(:enterprise) { create(:enterprise) }
let(:other_enterprise) { create(: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]) }
describe '#shipping_methods' do
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
describe '#shipping_categories' do
it "returns shipping categories that are not shipping categories of the order's products" do
package
other_shipping_category = Spree::ShippingCategory.create(name: "Custom")
expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first,
other_shipping_category]
end
end
end
end