Skip to content

Commit

Permalink
Merge pull request solidusio#591 from brendandeere/pluggable_stock_es…
Browse files Browse the repository at this point in the history
…timator

Allow the Stock::Estimator to be customized
  • Loading branch information
jhawthorn committed Jan 7, 2016
2 parents 33edbd1 + 695e5f9 commit eb761fb
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/app/models/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
require "spree/core/search/base"
require "spree/core/search/variant"
require 'spree/core/stock_configuration'

module Spree
class AppConfiguration < Preferences::Configuration
Expand Down Expand Up @@ -311,6 +312,10 @@ def static_model_preferences
@static_model_preferences ||= Spree::Preferences::StaticModelPreferences.new
end

def stock
Spree::StockConfiguration
end

# all the following can be deprecated when store prefs are no longer supported
# @private
DEPRECATED_STORE_PREFERENCES = {
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def refresh_rates
# StockEstimator.new assigment below will replace the current shipping_method
original_shipping_method_id = shipping_method.try!(:id)

new_rates = Stock::Estimator.new(order).shipping_rates(to_package)
new_rates = Spree::Config.stock.estimator_class.new(order).shipping_rates(to_package)

# If one of the new rates matches the previously selected shipping
# method, select that instead of the default provided by the estimator.
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/stock/coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def prioritize_packages(packages)
end

def estimate_packages(packages)
estimator = Estimator.new(order)
estimator = Spree::Config.stock.estimator_class.new(order)
packages.each do |package|
package.shipping_rates = estimator.shipping_rates(package)
end
Expand Down
11 changes: 11 additions & 0 deletions core/lib/spree/core/stock_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Spree
module StockConfiguration
mattr_accessor :estimator_class do
'::Spree::Stock::Estimator'
end

def self.estimator_class
@@estimator_class.constantize
end
end
end
16 changes: 16 additions & 0 deletions core/spec/lib/spree/core/stock_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

RSpec.describe Spree::StockConfiguration do
before(:all) { @estimator_class = described_class.estimator_class.to_s }
after(:all) { described_class.estimator_class = @estimator_class }

describe '.estimator_class' do
subject { described_class.estimator_class }
let(:foo) { Struct.new :foo }

before { described_class.estimator_class = 'Foo' }
before { Foo = foo }

it { is_expected.to eq foo }
end
end
4 changes: 4 additions & 0 deletions core/spec/models/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
expect(prefs.variant_search_class).to eq Spree::Core::Search::Variant
end

describe '#stock' do
subject { prefs.stock }
it { is_expected.to eq Spree::StockConfiguration }
end
end
5 changes: 5 additions & 0 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@
expect(shipment.refresh_rates).to eq([])
end

it 'uses the pluggable estimator class' do
expect(Spree::StockConfiguration).to receive(:estimator_class).and_call_original
shipment.refresh_rates
end

context 'to_package' do
let(:inventory_units) do
[build(:inventory_unit, line_item: line_item, variant: variant, state: 'on_hand'),
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/stock/coordinator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module Stock
expect(subject).to receive(:estimate_packages).ordered
subject.packages
end

it 'uses the pluggable estimator class' do
expect(Spree::StockConfiguration).to receive(:estimator_class).and_call_original
subject.packages
end
end

describe "#shipments" do
Expand Down

0 comments on commit eb761fb

Please sign in to comment.