From ff549fb62e64f9aa8000baa1d87c85555e582d93 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 9 Feb 2021 17:16:38 +0000 Subject: [PATCH] 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. --- app/controllers/spree/admin/tax_rates_controller.rb | 12 +++++++++++- .../spree/admin/tax_rates_controller_spec.rb | 13 +++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/admin/tax_rates_controller.rb b/app/controllers/spree/admin/tax_rates_controller.rb index b1846bff65..cd261ec718 100644 --- a/app/controllers/spree/admin/tax_rates_controller.rb +++ b/app/controllers/spree/admin/tax_rates_controller.rb @@ -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 diff --git a/spec/controllers/spree/admin/tax_rates_controller_spec.rb b/spec/controllers/spree/admin/tax_rates_controller_spec.rb index 8bef1f2fb1..2acc969225 100644 --- a/spec/controllers/spree/admin/tax_rates_controller_spec.rb +++ b/spec/controllers/spree/admin/tax_rates_controller_spec.rb @@ -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" }