Skip to content

Commit 9d36218

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 9d36218

File tree

11 files changed

+34
-31
lines changed

11 files changed

+34
-31
lines changed

backend/app/controllers/spree/admin/prices_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PricesController < ResourceController
88
def index
99
params[:q] ||= {}
1010

11-
@search = @product.prices.kept.accessible_by(current_ability, :index).ransack(params[:q])
11+
@search = @product.prices.accessible_by(current_ability, :index).ransack(params[:q])
1212
@master_prices = @search.result
1313
.currently_valid
1414
.for_master

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>

backend/spec/controllers/spree/admin/prices_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
context "when only given a product" do
1212
let(:product) { create(:product) }
1313

14-
let!(:deleted_master_price) { create(:price, variant: product.master).tap(&:discard!) }
14+
let!(:deleted_master_price) { create(:price, variant: product.master).tap(&:destroy!) }
1515

1616
subject { get :index, params: { product_id: product.slug } }
1717

@@ -31,7 +31,7 @@
3131
let(:variant) { create(:variant) }
3232
let(:product) { variant.product }
3333

34-
let!(:deleted_variant_price) { create(:price, variant:).tap(&:discard!) }
34+
let!(:deleted_variant_price) { create(:price, variant:).tap(&:destroy!) }
3535

3636
subject { get :index, params: { product_id: product.slug, variant_id: variant.id } }
3737

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/product/scopes_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,6 @@
166166
end
167167
end
168168

169-
context "with soft-deleted master price" do
170-
before { product.master.prices.discard_all }
171-
172-
it "doesn't include the product" do
173-
expect(Spree::Product.available).to match_array([])
174-
end
175-
end
176-
177169
context "with multiple prices" do
178170
let!(:second_price) { create(:price, variant: product.master) }
179171

0 commit comments

Comments
 (0)