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:
Matt-Yorkley
2021-01-23 15:14:11 +00:00
parent dfadcbd2e0
commit 219c982db7
2 changed files with 20 additions and 0 deletions

View 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

View File

@@ -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