Adding optional manual override of line item stock level check

This commit is contained in:
Rob Harrington
2016-12-10 15:30:55 +11:00
parent 00ee5a63df
commit 8e0f2fcb35
2 changed files with 30 additions and 0 deletions

View File

@@ -7,8 +7,12 @@ Spree::LineItem.class_eval do
# Redefining here to add the inverse_of option
belongs_to :order, :class_name => "Spree::Order", inverse_of: :line_items
# Allows manual skipping of stock_availability check
attr_accessor :skip_stock_check
attr_accessible :max_quantity, :final_weight_volume, :price
attr_accessible :final_weight_volume, :price, :as => :api
attr_accessible :skip_stock_check
before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed?
after_save :update_units
@@ -139,6 +143,13 @@ Spree::LineItem.class_eval do
private
# Override of Spree validation method
# Added check for in-memory :skip_stock_check attribute
def stock_availability
return if skip_stock_check || sufficient_stock?
errors.add(:quantity, I18n.t('validation.exceeds_available_stock'))
end
def scoper
return @scoper unless @scoper.nil?
@scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)

View File

@@ -535,5 +535,24 @@ module Spree
}.to change(Spree::OptionValue, :count).by(0)
end
end
describe "checking stock availability" do
let(:line_item) { LineItem.new }
context "when skip_stock_check is not set" do
it "checks stock" do
expect(line_item).to receive(:sufficient_stock?) { true }
line_item.send(:stock_availability)
end
end
context "when skip_stock_check is set to true" do
before { line_item.skip_stock_check = true }
it "does not check stock" do
expect(line_item).to_not receive(:sufficient_stock?)
line_item.send(:stock_availability)
end
end
end
end
end