mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Re-implement pre-Rails-5 versions of #implement! and #decrement! on Spree::LineItem
Rails 5 introduced some breaking changes to these built-in methods, and the new versions no longer work correctly in relation to decrementing stock with VariantOverrides.
This commit is contained in:
19
app/models/concerns/line_item_stock_changes.rb
Normal file
19
app/models/concerns/line_item_stock_changes.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Rails 5 introduced some breaking changes to these built-in methods, and the new versions
|
||||
# no longer work correctly in relation to decrementing stock with LineItems / VariantOverrides.
|
||||
# The following methods re-instate the pre-Rails-5 versions, which work as expected.
|
||||
# https://apidock.com/rails/v4.2.9/ActiveRecord/Persistence/increment%21
|
||||
# https://apidock.com/rails/v4.2.9/ActiveRecord/Persistence/decrement%21
|
||||
|
||||
module LineItemStockChanges
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def increment!(attribute, by = 1)
|
||||
increment(attribute, by).update_attribute(attribute, self[attribute])
|
||||
end
|
||||
|
||||
def decrement!(attribute, by = 1)
|
||||
decrement(attribute, by).update_attribute(attribute, self[attribute])
|
||||
end
|
||||
end
|
||||
@@ -6,6 +6,7 @@ require 'variant_units/variant_and_line_item_naming'
|
||||
module Spree
|
||||
class LineItem < ActiveRecord::Base
|
||||
include VariantUnits::VariantAndLineItemNaming
|
||||
include LineItemStockChanges
|
||||
include LineItemBasedAdjustmentHandling
|
||||
|
||||
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items
|
||||
|
||||
Reference in New Issue
Block a user