From c7648d67bd4318245a6c8f1b2e82cb694bd59e5a Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 28 Aug 2018 14:58:57 +1000 Subject: [PATCH] Add Variant stock API of Spree 1 --- app/models/spree/variant_decorator.rb | 2 + lib/open_food_network/variant_stock.rb | 80 ++++++++++++++++++++++++++ spec/factories.rb | 11 ++++ 3 files changed, 93 insertions(+) create mode 100644 lib/open_food_network/variant_stock.rb diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 2947efc282..3a97048c7c 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -1,5 +1,6 @@ require 'open_food_network/enterprise_fee_calculator' require 'open_food_network/variant_and_line_item_naming' +require 'open_food_network/variant_stock' require 'open_food_network/products_cache' Spree::Variant.class_eval do @@ -9,6 +10,7 @@ Spree::Variant.class_eval do # removing the Spree method to prevent error. remove_method :options_text if instance_methods(false).include? :options_text include OpenFoodNetwork::VariantAndLineItemNaming + include OpenFoodNetwork::VariantStock has_many :exchange_variants has_many :exchanges, through: :exchange_variants diff --git a/lib/open_food_network/variant_stock.rb b/lib/open_food_network/variant_stock.rb new file mode 100644 index 0000000000..fba38ebd57 --- /dev/null +++ b/lib/open_food_network/variant_stock.rb @@ -0,0 +1,80 @@ +require 'active_support/concern' + +# These methods were available in Spree 1, but were removed in Spree 2. +# We would still like to use them. Therefore we use only a single stock location +# (default stock location) and use it to track the `count_on_hand` value that +# was previously a database column on variants. +# +# We may decide to deprecate these methods after we designed the Network feature. +module OpenFoodNetwork + module VariantStock + extend ActiveSupport::Concern + + included do + after_save :save_stock + end + + def on_hand + if on_demand + Float::INFINITY + else + total_on_hand + end + end + + def count_on_hand + total_on_hand + end + + def on_hand=(new_level) + error = 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false' + raise error unless Spree::Config.track_inventory_levels + + self.count_on_hand = new_level + end + + def count_on_hand=(new_level) + raise_error_if_no_stock_item_available + overwrite_stock_levels new_level + end + + def on_demand + stock_items.any?(&:backorderable?) + end + + def on_demand=(new_value) + raise_error_if_no_stock_item_available + + # There should be only one at the default stock location. + stock_items.each do |item| + item.backorderable = new_value + end + end + + private + + def save_stock + stock_items.each(&:save) + end + + def raise_error_if_no_stock_item_available + message = 'You need to save the variant to create a stock item before you can set stock levels.' + raise message if stock_items.empty? + end + + # Backwards compatible setting of stock levels in Spree 2.0. + # It would be better to use `Spree::StockItem.adjust_count_on_hand` which + # takes a value to add to the current stock level and uses proper locking. + # But this should work the same as in Spree 1.3. + def overwrite_stock_levels(new_level) + stock_items.first.send :count_on_hand=, new_level + + # There shouldn't be any other stock items, because we should have only one + # stock location. But in case there are, the total should be new_level, + # so all others need to be zero. + stock_items[1..-1].each do |item| + item.send :count_on_hand=, 0 + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index e387f519e8..71dbc0dcde 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -483,8 +483,19 @@ FactoryBot.modify do end factory :variant do + transient do + on_demand { false } + on_hand { 5 } + end + unit_value 1 unit_description '' + + after(:create) do |variant, evaluator| + variant.on_demand = evaluator.on_demand + variant.count_on_hand = evaluator.on_hand + variant.save + end end factory :shipping_method do