Skip to content

Commit

Permalink
Use discard gem api
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMartini committed Feb 26, 2021
1 parent f8eaaf1 commit 2628619
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spree/admin/sale_prices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create

def destroy
@sale_price = Spree::SalePrice.find(params[:id])
@sale_price.destroy
@sale_price.discard
render_js_for_destroy
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ def self.prepended(base)
base.has_many :sale_prices, dependent: :destroy
base.has_many :active_sale_prices, -> { merge(::Spree::SalePrice.active) }, class_name: '::Spree::SalePrice'
base.after_save :update_calculated_sale_prices
base.after_discard do
sale_prices.discard_all
end
end

def update_calculated_sale_prices
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/sale_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class SalePrice < ActiveRecord::Base
include Spree::SoftDeletable

belongs_to :price, class_name: "Spree::Price", touch: true
belongs_to :price_with_deleted, -> { with_deleted }, class_name: "Spree::Price", foreign_key: :price_id
belongs_to :price_with_deleted, -> { with_discarded }, class_name: "Spree::Price", foreign_key: :price_id

delegate :currency, :currency=, to: :price, allow_nil: true

Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/sale_prices/_prices_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</thead>
<tbody>
<% prices.each do |price| %>
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.deleted? %>">
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.discarded? %>">
<td><%= check_box_tag 'price_ids[]', price.id %></td>
<td><%= price.variant.descriptive_name %></td>
<td><%= price.display_country %></td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/sale_prices/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</thead>
<tbody>
<% sale_prices.each do |price| %>
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.deleted? %>">
<tr id="<%= spree_dom_id price %>" data-hook="prices_row" class="<%= "deleted" if price.discarded? %>">
<td>
<% if price.start_at&.future? %>
<span class="pill pill-warning"><%=t 'spree.sale_price_scheduled' %></span>
Expand Down
2 changes: 1 addition & 1 deletion spec/models/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
before { price.put_on_sale 10 }

it 'destroys all sale prices when it is destroyed' do
expect { price.destroy }
expect { price.discard }
.to change { Spree::SalePrice.all.size }
.from(1).to(0)
end
Expand Down
12 changes: 6 additions & 6 deletions spec/models/sale_price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
let(:price) { sale_price.price }

before do
price.destroy
price.discard
sale_price.reload
end

Expand All @@ -81,18 +81,18 @@
context 'when the price has been soft-deleted' do
before do
sale = create :sale_price
sale.price.destroy
sale.price.discard
end

it 'preloads the variant via SQL also for soft-deleted records' do
records = Spree::SalePrice.with_deleted.includes(:variant)
records = Spree::SalePrice.with_discarded.includes(:variant)
expect(records.first.variant).to be_present
end
end
end

context 'touching associated product when destroyed' do
subject { -> { sale_price.reload.destroy } }
subject { -> { sale_price.reload.discard } }
let!(:product) { sale_price.product }
let(:sale_price) { Timecop.travel(1.day.ago) { create(:sale_price) } }

Expand All @@ -107,15 +107,15 @@
end

context 'when associated variant has been destroyed' do
before { sale_price.variant.destroy }
before { sale_price.variant.discard }

it 'does not touch product' do
expect(subject).not_to change { product.reload.updated_at }
end
end

context 'when associated price has been destroyed' do
before { sale_price.price.destroy }
before { sale_price.price.discard }

it 'does not touch product' do
expect(subject).not_to change { product.reload.updated_at }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
end

it 'destroys all sale prices when it is destroyed' do
expect { @variant.destroy }
expect { @variant.discard }
.to change { Spree::SalePrice.all.size }
.from(3).to(0)
end
Expand Down

0 comments on commit 2628619

Please sign in to comment.