Skip to content

Commit 072edbc

Browse files
committed
Hard-Delete prices
We don't need soft delete on a model that belongs to an already soft-deleted model. It'll disappear anyway.
1 parent 2f69e2f commit 072edbc

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

core/app/models/concerns/spree/default_price.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def default_price
4141
end
4242

4343
def has_default_price?
44-
default_price.present? && !default_price.discarded?
44+
default_price.present?
4545
end
4646
end
4747
end

core/app/models/spree/price.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spree
44
class Price < Spree::Base
5-
include Spree::SoftDeletable
5+
include Spree::HardDeletable
66

77
MAXIMUM_AMOUNT = BigDecimal('99_999_999.99')
88

core/app/models/spree/variant.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class Variant < Spree::Base
5959
has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: "Spree::Image"
6060

6161
has_many :prices,
62-
-> { with_discarded },
6362
class_name: 'Spree::Price',
6463
dependent: :destroy,
6564
inverse_of: :variant,

core/app/models/spree/variant/price_selector.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def price_for_options(price_options)
3939
#
4040
# @return [Array<Spree::Price>]
4141
def sorted_prices_for(variant)
42-
variant.prices.select do |price|
43-
variant.discarded? || price.kept?
44-
end.sort_by do |price|
42+
variant.prices.sort_by do |price|
4543
[
4644
price.country_iso.nil? ? 0 : 1,
4745
price.updated_at || Time.zone.now,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class RemoveSoftDeletedPrices < ActiveRecord::Migration[7.0]
4+
def up
5+
discarded_prices = Spree::Price.where.not(deleted_at: nil)
6+
affected_variant_ids = discarded_prices.map(&:variant_id).uniq
7+
discarded_prices.delete_all
8+
Spree::Variant.where(id: affected_variant_ids).find_each(&:touch)
9+
remove_column :spree_prices, :deleted_at
10+
end
11+
12+
def down
13+
add_column :spree_prices, :deleted_at, :timestamp, null: true
14+
end
15+
end

core/spec/models/spree/variant_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,17 @@
350350
expect(variant.default_price.attributes).to eq(price.attributes)
351351
end
352352

353-
context "when the variant and the price are both soft-deleted" do
353+
context "when the variant is soft-deleted" do
354354
it "will use a deleted price as the default price" do
355355
variant = create(:variant, deleted_at: 1.day.ago)
356-
variant.prices.each { |price| price.discard }
357-
expect(variant.reload.price).to be_present
356+
expect(variant.price).to be_present
358357
end
359358
end
360359

361360
context "when the variant is not soft-deleted, but its price is" do
362361
it "will not use a deleted price as the default price" do
363362
variant = create(:variant)
364-
variant.prices.each { |price| price.discard }
363+
variant.prices.each { |price| price.destroy }
365364
expect(variant.reload.price).not_to be_present
366365
end
367366
end
@@ -397,13 +396,13 @@
397396
end
398397
end
399398

400-
context 'when default price is discarded' do
399+
context 'when default price is destroyed' do
401400
it 'returns false' do
402401
variant = create(:variant, price: 20)
403402

404-
variant.default_price.discard
403+
variant.default_price.destroy
405404

406-
expect(variant.has_default_price?).to be(false)
405+
expect(variant.reload.has_default_price?).to be(false)
407406
end
408407
end
409408
end
@@ -873,7 +872,7 @@
873872
it "should not discard default_price" do
874873
variant.discard
875874
variant.reload
876-
expect(previous_variant_price.reload).not_to be_discarded
875+
expect(previous_variant_price.reload).to be_present
877876
end
878877

879878
it "should keep its price if deleted" do

0 commit comments

Comments
 (0)