Also soft-delete tax rates when changing included_in_price

We need to do this when the included_in_price boolean changes as well, for the same reasons.
This commit is contained in:
Matt-Yorkley
2021-02-09 17:16:38 +00:00
parent a811fb3783
commit ff549fb62e
2 changed files with 20 additions and 5 deletions

View File

@@ -6,13 +6,23 @@ module Spree
delegate :transition_rate!, :updated_rate, to: :updater
def update
return super unless amount_changed? && associated_adjustments?
return super unless requires_transition?
transition_tax_rate
end
private
def requires_transition?
(included_changed? || amount_changed?) && associated_adjustments?
end
def included_changed?
ActiveRecord::Type::Boolean.new.type_cast_from_user(
permitted_resource_params[:included_in_price]
) != @tax_rate.included_in_price
end
def amount_changed?
BigDecimal(permitted_resource_params[:amount]) != @tax_rate.amount
end

View File

@@ -8,7 +8,8 @@ module Spree
include AuthenticationHelper
let!(:tax_rate) {
create(:tax_rate, name: "Original Rate", amount: 0.1, calculator: build(:calculator))
create(:tax_rate, name: "Original Rate", amount: 0.1, included_in_price: false,
calculator: build(:calculator))
}
describe "#update" do
@@ -17,10 +18,14 @@ module Spree
context "when the tax rate has associated adjustments" do
let!(:adjustment) { create(:adjustment, originator: tax_rate) }
context "when the amount is not changed" do
context "when the amount and included flag are not changed" do
let(:params) {
{ name: "Updated Rate", amount: "0.1", included_in_price: "0" }
}
it "updates the record" do
expect {
spree_put :update, id: tax_rate.id, tax_rate: { name: "Updated Rate", amount: "0.1" }
spree_put :update, id: tax_rate.id, tax_rate: params
}.to_not change{ Spree::TaxRate.with_deleted.count }
expect(response).to redirect_to spree.admin_tax_rates_url
@@ -29,7 +34,7 @@ module Spree
end
end
context "when the amount is changed" do
context "when the amount or included flag are changed" do
it "duplicates the record and soft-deletes the duplicate" do
expect {
spree_put :update, id: tax_rate.id, tax_rate: { name: "Changed Rate", amount: "0.5" }