mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Adding optional manual override of line item stock level check
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user