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.

Let a full band ladder be replaced

Damian Legawiec91cee09f937f
diff --git a/spree/core/app/models/spree/price_adjustment_tier.rb b/spree/core/app/models/spree/price_adjustment_tier.rb
index 4105e8926d..8064bac3bd 100644
--- a/spree/core/app/models/spree/price_adjustment_tier.rb
+++ b/spree/core/app/models/spree/price_adjustment_tier.rb
@@ -47,11 +47,15 @@ module Spree
# answer to a question already answered — refused by the numericality
# bound above rather than silently shadowing the column.
def tiers_within_cap
- return if price_list_id.blank?
-
- siblings = self.class.where(price_list_id: price_list_id)
- siblings = siblings.where.not(id: id) if persisted?
- return if siblings.count < MAXIMUM_TIERS_PER_LIST
+ return if price_list.nil?
+
+ # Counted over the ladder the save will leave behind, not the rows
+ # standing now: replacing a full ladder marks the old bands for
+ # destruction and builds the new ones, so counting the database would
+ # see both halves and refuse a swap that ends the size it began
+ # (docs/plans/6.0-volume-pricing.md).
+ siblings = price_list.price_adjustment_tiers.reject { |tier| tier.marked_for_destruction? || tier == self }
+ return if siblings.size < MAXIMUM_TIERS_PER_LIST
errors.add(:base, :too_many_tiers, count: MAXIMUM_TIERS_PER_LIST)
end
diff --git a/spree/core/spec/models/spree/price_adjustment_tier_spec.rb b/spree/core/spec/models/spree/price_adjustment_tier_spec.rb
index 4a5d76fe32..80e3b57c74 100644
--- a/spree/core/spec/models/spree/price_adjustment_tier_spec.rb
+++ b/spree/core/spec/models/spree/price_adjustment_tier_spec.rb
@@ -46,6 +46,29 @@ describe Spree::PriceAdjustmentTier, type: :model do
expect(build(:price_adjustment_tier, price_list: price_list, min_quantity: 10, percentage: 0)).not_to be_valid
end
+ # Replacing a full ladder marks the old bands for destruction and builds
+ # the new ones, so a cap counting the database would see both halves.
+ it 'lets a full ladder be replaced with one the same size' do
+ (2..(described_class::MAXIMUM_TIERS_PER_LIST + 1)).each do |quantity|
+ create(:price_adjustment_tier, price_list: price_list, min_quantity: quantity)
+ end
+
+ replacement = (100..(99 + described_class::MAXIMUM_TIERS_PER_LIST)).map do |quantity|
+ { min_quantity: quantity, percentage: '-7' }
+ end
+
+ expect(price_list.update(price_adjustment_tiers: replacement)).to be(true)
+ expect(price_list.reload.price_adjustment_tiers.count).to eq(described_class::MAXIMUM_TIERS_PER_LIST)
+ end
+
+ it 'still refuses a replacement one past the cap' do
+ replacement = (100..(100 + described_class::MAXIMUM_TIERS_PER_LIST)).map do |quantity|
+ { min_quantity: quantity, percentage: '-7' }
+ end
+
+ expect(price_list.update(price_adjustment_tiers: replacement)).to be(false)
+ end
+
it 'refuses a ladder past the cap' do
(2..(described_class::MAXIMUM_TIERS_PER_LIST + 1)).each do |quantity|
create(:price_adjustment_tier, price_list: price_list, min_quantity: quantity)