Skip to content

Commit 609f65e

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 609f65e

File tree

8 files changed

+31
-20
lines changed

8 files changed

+31
-20
lines changed

backend/app/views/spree/admin/prices/_master_variant_table.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
</tr>
2121
</thead>
2222
<% master_prices.each do |price| %>
23-
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.discarded? %>">
23+
<tr id="<%= spree_dom_id price %>" data-hook="prices_row">
2424
<td><%= price.display_country %></td>
2525
<td><%= price.currency %></td>
2626
<td><%= price.money.to_html %></td>
2727
<td class="actions">
2828
<% if can?(:edit, price) %>
29-
<%= link_to_edit(price, no_text: true) unless price.discarded? %>
29+
<%= link_to_edit(price, no_text: true) %>
3030
<% end %>
3131
<% if can?(:destroy, price) %>
3232
&nbsp;
33-
<%= link_to_delete(price, no_text: true) unless price.discarded? %>
33+
<%= link_to_delete(price, no_text: true) %>
3434
<% end %>
3535
</td>
3636
</tr>

backend/app/views/spree/admin/prices/_table.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
</thead>
1515
<tbody>
1616
<% variant_prices.each do |price| %>
17-
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.discarded? %>">
17+
<tr id="<%= spree_dom_id price %>" data-hook="prices_row">
1818
<td><%= price.variant.descriptive_name %></td>
1919
<td><%= price.display_country %></td>
2020
<td><%= price.currency %></td>
2121
<td><%= price.money.to_html %></td>
2222
<td class="actions">
2323
<% if can?(:edit, price) %>
24-
<%= link_to_edit(price, no_text: true) unless price.discarded? %>
24+
<%= link_to_edit(price, no_text: true) %>
2525
<% end %>
2626
<% if can?(:destroy, price) %>
2727
&nbsp;
28-
<%= link_to_delete(price, no_text: true) unless price.discarded? %>
28+
<%= link_to_delete(price, no_text: true) %>
2929
<% end %>
3030
</td>
3131
</tr>

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)