Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Follow the Rails default for has_many inversing (#14484)

Damian Legawieccee28a23d996
diff --git a/spree/core/app/models/spree/base.rb b/spree/core/app/models/spree/base.rb
index e1893f059d..9f0da393c7 100644
--- a/spree/core/app/models/spree/base.rb
+++ b/spree/core/app/models/spree/base.rb
@@ -66,11 +66,6 @@ class Spree::Base < ApplicationRecord
ApplicationRecord.try(:spree_base_uniqueness_scope) || []
end
- # FIXME: https://github.com/rails/rails/issues/40943
- def self.has_many_inversing
- false
- end
-
# this can overridden in subclasses to disallow deletion
def can_be_deleted?
true
diff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rb
index 5dc1ab31f5..941d86cbac 100644
--- a/spree/core/app/models/spree/cart.rb
+++ b/spree/core/app/models/spree/cart.rb
@@ -166,11 +166,15 @@ module Spree
fulfillments.delete_all
fulfillment_items.reset
- self.fulfillments = Spree::Stock::Coordinator.new(self).fulfillments.map do |fulfillment|
+ # Appended rather than assigned: setting `cart` on each proposal already
+ # files it under this cart's `fulfillments` (the association is
+ # inverse_of it), so a collection assignment sees the rows as already
+ # present, writes none of them, and the cart ends up with no proposals at
+ # all.
+ Spree::Stock::Coordinator.new(self).fulfillments.each do |fulfillment|
fulfillment.address_id = ship_address_id
fulfillment.order = nil
- fulfillment.cart = self
- fulfillment
+ fulfillments << fulfillment
end
prune_undeliverable_fulfillments!
fulfillments.reload
diff --git a/spree/core/app/models/spree/order_group.rb b/spree/core/app/models/spree/order_group.rb
index 663875ad1d..be2e852861 100644
--- a/spree/core/app/models/spree/order_group.rb
+++ b/spree/core/app/models/spree/order_group.rb
@@ -144,9 +144,15 @@ module Spree
#
# @return [BigDecimal] completed payments on this group, less their refunds
+ #
+ # Reads the persisted rows rather than filtering the loaded association: a
+ # payment still being validated is already held by `payments` (the
+ # association is inverse_of this group), and counting it here would have the
+ # group treat money it has not taken yet as settled — which is what
+ # `max_amount` then subtracts from the total.
def payment_total
@payment_total ||= begin
- settled = payments.select { |payment| payment.status == 'completed' }
+ settled = payments.completed.to_a
settled.sum(&:amount) - Spree::Refund.where(payment_id: settled.map(&:id)).sum(:amount)
end
end
diff --git a/spree/core/app/models/spree/stock_movement.rb b/spree/core/app/models/spree/stock_movement.rb
index 1da7c520fe..99d1e725b1 100644
--- a/spree/core/app/models/spree/stock_movement.rb
+++ b/spree/core/app/models/spree/stock_movement.rb
@@ -90,8 +90,14 @@ module Spree
Spree.t('stock_movement.reasons.manual_adjustment', locale: :en)
end
+ # A movement is an immutable audit row: once written, nothing may rewrite
+ # it. The guard is against an *update*, so it reads `persisted?` at the
+ # moment the save begins rather than afterwards — a parent's autosave can
+ # insert this row while its own save is still in flight (the stock level
+ # holds it through `inverse_of`), which leaves it persisted mid-insert. That
+ # row is being created, not updated, so it must still be allowed to finish.
def readonly?
- persisted?
+ persisted? && !being_created?
end
# @deprecated Use {#stock_level}; removed in 6.1.
@@ -108,6 +114,21 @@ module Spree
private
+ # Re-entrant: a parent's autosave can call this again from inside the outer
+ # save, and the inner call must not clear the outer one's guard.
+ def create_or_update(...)
+ @create_depth = @create_depth.to_i + 1
+ @being_created = true if new_record?
+ super
+ ensure
+ @create_depth -= 1
+ @being_created = false if @create_depth.zero?
+ end
+
+ def being_created?
+ @being_created.present?
+ end
+
# The single place count_on_hand and allocated_count change.
#
# A correction applies even to a variant that stopped tracking inventory —
diff --git a/spree/core/config/initializers/rails61_fixes.rb b/spree/core/config/initializers/rails61_fixes.rb
deleted file mode 100644
index 2b7c61ea19..0000000000
--- a/spree/core/config/initializers/rails61_fixes.rb
+++ /dev/null
@@ -1 +0,0 @@
-ActiveRecord::Base.has_many_inversing = false