mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Move Base splitter from main app models to order management engine services
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module Stock
|
||||
module Splitter
|
||||
class Base
|
||||
attr_reader :packer, :next_splitter
|
||||
|
||||
def initialize(packer, next_splitter = nil)
|
||||
@packer = packer
|
||||
@next_splitter = next_splitter
|
||||
end
|
||||
delegate :stock_location, :order, to: :packer
|
||||
|
||||
def split(packages)
|
||||
return_next(packages)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def return_next(packages)
|
||||
next_splitter ? next_splitter.split(packages) : packages
|
||||
end
|
||||
|
||||
def build_package(contents = [])
|
||||
@packer.package_factory.new(stock_location, order, contents)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -83,7 +83,7 @@ module Openfoodnetwork
|
||||
# we must use this splitter and no other
|
||||
initializer "spree.register.stock_splitters" do |app|
|
||||
app.config.spree.stock_splitters = [
|
||||
Spree::Stock::Splitter::Base
|
||||
OrderManagement::Stock::BasicSplitter
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module OrderManagement
|
||||
module Stock
|
||||
class BasicSplitter
|
||||
attr_reader :packer, :next_splitter
|
||||
|
||||
def initialize(packer, next_splitter = nil)
|
||||
@packer = packer
|
||||
@next_splitter = next_splitter
|
||||
end
|
||||
delegate :stock_location, :order, to: :packer
|
||||
|
||||
def split(packages)
|
||||
return_next(packages)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def return_next(packages)
|
||||
next_splitter ? next_splitter.split(packages) : packages
|
||||
end
|
||||
|
||||
def build_package(contents = [])
|
||||
@packer.package_factory.new(stock_location, order, contents)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module OrderManagement
|
||||
module Stock
|
||||
describe BasicSplitter do
|
||||
let(:packer) { build(:stock_packer) }
|
||||
|
||||
it 'continues to splitter chain' do
|
||||
splitter1 = BasicSplitter.new(packer)
|
||||
splitter2 = BasicSplitter.new(packer, splitter1)
|
||||
packages = []
|
||||
|
||||
splitter1.should_receive(:split).with(packages)
|
||||
splitter2.split(packages)
|
||||
end
|
||||
|
||||
it 'builds package using package factory' do
|
||||
# Basic extension of Base splitter used to test build_package method
|
||||
class ::RealSplitter < BasicSplitter
|
||||
def split(_packages)
|
||||
build_package
|
||||
end
|
||||
end
|
||||
|
||||
# Custom package used to test setting package factory
|
||||
class ::CustomPackage
|
||||
def initialize(stock_location, order, splitters); end
|
||||
end
|
||||
allow(Spree::Config).to receive(:package_factory) { CustomPackage }
|
||||
|
||||
expect(::RealSplitter.new(packer).split(nil).class).to eq CustomPackage
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@ require 'spec_helper'
|
||||
describe Openfoodnetwork::Application, 'configuration' do
|
||||
let(:config) { described_class.config }
|
||||
|
||||
it "sets Spree::Stock::Splitter::Base as the only stock splitter" do
|
||||
expect(config.spree.stock_splitters).to eq [Spree::Stock::Splitter::Base]
|
||||
it "sets OrderManagement::Stock::BasicSplitter as the only stock splitter" do
|
||||
expect(config.spree.stock_splitters).to eq [OrderManagement::Stock::BasicSplitter]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module Stock
|
||||
module Splitter
|
||||
describe Base do
|
||||
let(:packer) { build(:stock_packer) }
|
||||
|
||||
it 'continues to splitter chain' do
|
||||
splitter1 = Base.new(packer)
|
||||
splitter2 = Base.new(packer, splitter1)
|
||||
packages = []
|
||||
|
||||
splitter1.should_receive(:split).with(packages)
|
||||
splitter2.split(packages)
|
||||
end
|
||||
|
||||
it 'builds package using package factory' do
|
||||
# Basic extension of Base splitter used to test build_package method
|
||||
class ::BasicSplitter < Base
|
||||
def split(_packages)
|
||||
build_package
|
||||
end
|
||||
end
|
||||
|
||||
# Custom package used to test setting package factory
|
||||
class ::CustomPackage
|
||||
def initialize(stock_location, order, splitters); end
|
||||
end
|
||||
allow(Spree::Config).to receive(:package_factory) { CustomPackage }
|
||||
|
||||
expect(::BasicSplitter.new(packer).split(nil).class).to eq CustomPackage
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user