Skip to content

Commit 1086995

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 1086995

File tree

13 files changed

+56
-42
lines changed

13 files changed

+56
-42
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: 4 additions & 4 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

@@ -21,7 +21,7 @@
2121
subject
2222
expect(assigns(:search)).to be_a(Ransack::Search)
2323
expect(assigns(:variant_prices)).to be_empty
24-
expect(assigns(:master_prices)).to eq(product.prices.kept.for_master)
24+
expect(assigns(:master_prices)).to eq(product.prices.for_master)
2525
expect(assigns(:master_prices)).to_not include(deleted_master_price)
2626
expect(assigns(:product)).to eq(product)
2727
end
@@ -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

@@ -40,7 +40,7 @@
4040
it 'assigns usable instance variables' do
4141
subject
4242
expect(assigns(:search)).to be_a(Ransack::Search)
43-
expect(assigns(:variant_prices)).to eq(product.prices.kept.for_variant)
43+
expect(assigns(:variant_prices)).to eq(product.prices.for_variant)
4444
expect(assigns(:master_prices)).to eq(product.prices.for_master)
4545
expect(assigns(:variant_prices)).to include(variant.default_price)
4646
expect(assigns(:variant_prices)).to_not include(deleted_variant_price)

backend/spec/features/admin/products/edit/products_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@
6161

6262
click_link 'Prices'
6363

64-
within "#spree_price_#{product.master.default_price.id}" do
65-
accept_alert do
66-
click_icon :trash
64+
# The deprecation warning will disappear in Solidus 5
65+
Spree.deprecator.silence do
66+
within "#spree_price_#{product.master.default_price.id}" do
67+
accept_alert do
68+
click_icon :trash
69+
end
6770
end
6871
end
6972
expect(page).to have_content("Price has been successfully removed")

backend/spec/features/admin/products/pricing_spec.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,27 @@
168168

169169
it "will delete the non-default price" do
170170
subject
171-
within "#spree_price_#{other_price.id}" do
172-
accept_alert do
173-
click_icon :trash
171+
172+
# The deprecation warning will disappear in Solidus 5
173+
Spree.deprecator.silence do
174+
within "#spree_price_#{other_price.id}" do
175+
accept_alert do
176+
click_icon :trash
177+
end
174178
end
175179
end
176180
expect(page).to have_content("Price has been successfully removed")
177181
end
178182

179183
it "does not break when default price is deleted" do
180184
subject
181-
within "#spree_price_#{variant.default_price.id}" do
182-
accept_alert do
183-
click_icon :trash
185+
186+
# The deprecation warning will disappear in Solidus 5
187+
Spree.deprecator.silence do
188+
within "#spree_price_#{variant.default_price.id}" do
189+
accept_alert do
190+
click_icon :trash
191+
end
184192
end
185193
end
186194
expect(page).to have_content("Price has been successfully removed")

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,

0 commit comments

Comments
 (0)