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.

Skip `update_columns` call for `Adjustmant#update!` when values didn'… (#13401)

Damian Legawiec10032adb0853
diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb
index a2d1abf5ae..2ef36ca365 100644
--- a/core/app/models/spree/adjustment.rb
+++ b/core/app/models/spree/adjustment.rb
@@ -122,13 +122,19 @@ module Spree
src = cached_source
return amount if closed? || src.blank?
- amount = src.compute_amount(target)
- attributes = { amount: amount, updated_at: Time.current }
- attributes[:eligible] = src.promotion.eligible?(target) if promotion?
+ new_amount = src.compute_amount(target)
+ new_eligible = promotion? ? src.promotion.eligible?(target) : eligible
- update_columns(attributes)
+ changed_attributes = {}
+ changed_attributes[:amount] = new_amount if new_amount != amount
+ changed_attributes[:eligible] = new_eligible if new_eligible != eligible
- amount
+ if changed_attributes.any?
+ changed_attributes[:updated_at] = Time.current
+ update_columns(changed_attributes)
+ end
+
+ new_amount
end
private
diff --git a/core/spec/models/spree/adjustment_spec.rb b/core/spec/models/spree/adjustment_spec.rb
index 6f4e300349..918729ddc9 100644
--- a/core/spec/models/spree/adjustment_spec.rb
+++ b/core/spec/models/spree/adjustment_spec.rb
@@ -218,6 +218,17 @@ describe Spree::Adjustment, type: :model do
end
end
end
+
+ context 'when amount and eligible are the same' do
+ it "does not update the adjustment" do
+ expect(adjustment).to_not receive(:update_column)
+ adjustment.amount = 10
+ adjustment.eligible = true
+ subject
+ expect(adjustment.amount).to eq(10)
+ expect(adjustment.eligible).to eq(true)
+ end
+ end
end
end