Extract VariantOverride fetching into ScopeVariantToHub#scope

This commit is contained in:
Rohan Mitchell
2015-06-18 14:45:40 +10:00
parent 7cc2bc4fde
commit dd2f6d6430
3 changed files with 20 additions and 9 deletions

View File

@@ -25,12 +25,21 @@ class VariantOverride < ActiveRecord::Base
if vo.nil?
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level for a variant without a VariantOverride."
elsif vo.count_on_hand.blank?
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified."
else
vo.decrement! :count_on_hand, quantity
vo.decrement_stock! quantity
end
end
def stock_overridden?
count_on_hand.present?
end
def decrement_stock!(quantity)
if stock_overridden?
decrement! :count_on_hand, quantity
else
Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified."
end
end

View File

@@ -7,12 +7,13 @@ module OpenFoodNetwork
def scope(variant)
variant.send :extend, OpenFoodNetwork::ScopeVariantToHub::ScopeVariantToHub
variant.instance_variable_set :@hub, @hub
variant.instance_variable_set :@variant_override, VariantOverride.send(:for, @hub, variant)
end
module ScopeVariantToHub
def price
VariantOverride.price_for(@hub, self) || super
@variant_override.andand.price || super
end
def price_in(currency)
@@ -20,12 +21,12 @@ module OpenFoodNetwork
end
def count_on_hand
VariantOverride.count_on_hand_for(@hub, self) || super
@variant_override.andand.count_on_hand || super
end
def decrement!(attribute, by=1)
if attribute == :count_on_hand && VariantOverride.stock_overridden?(@hub, self)
VariantOverride.decrement_stock! @hub, self, by
if attribute == :count_on_hand && @variant_override.andand.stock_overridden?
@variant_override.decrement_stock! by
else
super
end

View File

@@ -50,6 +50,7 @@ module Spree
variant = double(:variant)
quantity = 123
Spree::Variant.stub(:find).and_return(variant)
VariantOverride.stub(:for).and_return(nil)
op.should_receive(:check_stock_levels).with(variant, quantity).and_return(true)
op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)