-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2783 from aldesantis/simple-coordinator-extension
Stock location sorters
- Loading branch information
Showing
12 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Stock | ||
module LocationSorter | ||
# Stock location sorters are used to determine the order in which | ||
# inventory units will be allocated when packaging a shipment. | ||
# | ||
# This allows you, for example, to allocate inventory from the default | ||
# stock location first. | ||
# | ||
# @abstract To implement your own location sorter, subclass and | ||
# implement {#sort}. | ||
class Base | ||
# @!attribute [r] stock_locations | ||
# @return [Enumerable<Spree::StockLocation>] | ||
# a collection of locations to sort | ||
attr_reader :stock_locations | ||
|
||
# Initializes the stock location sorter. | ||
# | ||
# @param stock_locations [Enumerable<Spree::StockLocation>] | ||
# a collection of locations to sort | ||
def initialize(stock_locations) | ||
@stock_locations = stock_locations | ||
end | ||
|
||
# Sorts the stock locations. | ||
# | ||
# @return [Enumerable<Spree::StockLocation>] | ||
# a collection of sorted stock locations | ||
def sort | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
core/app/models/spree/stock/location_sorter/default_first.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Stock | ||
module LocationSorter | ||
# This stock location sorter will give priority to the default stock | ||
# location. | ||
class DefaultFirst < Spree::Stock::LocationSorter::Base | ||
def sort | ||
stock_locations.order_default | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Stock | ||
module LocationSorter | ||
# This stock location sorter will leave the stock locations unsorted. | ||
class Unsorted < Spree::Stock::LocationSorter::Base | ||
def sort | ||
stock_locations | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
core/spec/models/spree/stock/location_sorter/default_first_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Spree | ||
module Stock | ||
module LocationSorter | ||
RSpec.describe DefaultFirst, type: :model do | ||
subject { described_class.new(stock_locations) } | ||
|
||
let(:stock_locations) { OpenStruct.new(order_default: sorted_stock_locations) } | ||
let(:sorted_stock_locations) { instance_double('Spree::StockLocation::ActiveRecord_Relation') } | ||
|
||
it 'returns the default stock location first' do | ||
expect(subject.sort).to eq(sorted_stock_locations) | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
core/spec/models/spree/stock/location_sorter/unsorted_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Spree | ||
module Stock | ||
module LocationSorter | ||
RSpec.describe Unsorted, type: :model do | ||
subject { described_class.new(stock_locations) } | ||
|
||
let(:stock_locations) { instance_double('Spree::StockLocation::ActiveRecord_Relation') } | ||
|
||
it 'returns the original stock locations unsorted' do | ||
expect(subject.sort).to eq(stock_locations) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
guides/source/developers/shipments/stock-location-sorters.html.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Stock location sorters | ||
|
||
This article explains the purpose, interface and correct usage of custom stock location sorters. | ||
|
||
Your app's stock location sorter defines the order in which stock locations are used to allocate | ||
inventory when creating packages for an order. The sorter is called by `Spree::Stock::SimpleCoordinator` | ||
when allocating inventory for an order. | ||
|
||
## Pre-configured sorters | ||
|
||
Currently, we only have two sorters, which you should use unless you need custom logic: | ||
|
||
- [Unsorted](https://github.com/solidusio/solidus/blob/master/core/app/models/spree/stock/sorter/unsorted.rb), | ||
which allocates inventory from stock locations as they are returned from the DB. | ||
- [Default first](https://github.com/solidusio/solidus/blob/master/core/app/models/spree/stock/sorter/default_first.rb), | ||
which allocates inventory from the default stock location first. | ||
|
||
## Custom sorter API | ||
|
||
A custom sorter should inherit from `Spree::Stock::LocationSorter::Base` and implement a `sort` method | ||
which accepts a `Spree::StockLocation::ActiveRecord_Relation` and returns an enumerable of stock | ||
locations. Note that the return value does not have to be an AR relation. | ||
|
||
Here's an example that sorts stock locations by a custom `priority` attribute: | ||
|
||
```ruby | ||
class Spree::Stock::LocationSorter::Priority < Spree::Stock::LocationSorter::Base | ||
def sort | ||
stock_locations.order(priority: :asc) | ||
end | ||
end | ||
``` | ||
|
||
### Switching the sorter | ||
|
||
Once you have created the logic for the new sorter, you need to register it so that it's used by | ||
`Spree::Stock::SimpleCoordinator`. | ||
|
||
For example, you can register it in your `/config/application.rb` initializer: | ||
|
||
```ruby | ||
# /config/application.rb | ||
module MyStore | ||
class Application < Rails::Application | ||
# ... | ||
|
||
initializer 'spree.register.stock_location_sorter' do |app| | ||
app.config.spree.stock.location_sorter_class = 'Spree::Stock::LocationSorter::Priority' | ||
end | ||
end | ||
end | ||
``` |