From cad7e257185e162dc17ea18f168b84657296735e Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 7 May 2019 17:22:01 +0200 Subject: [PATCH 1/2] Validate store credit amount is not used before discarding We already had this but with the last version of discard the validation is no more performing. I think it's something related to this commit https://github.com/jhawthorn/discard/commit/dfdd984b37bf3d646b276d68e47f7f998c6aaad9 which is included in the last release. Before the release, calling discarded? on the line: validate :validate_no_amount_used, if: :discarded? was returning true, even if the record change wasn't yet persisted. We don't need that line anymore since now records can't be discarded without passing the after_discard validation so we should not have any of those. Anyway, even if that happens we don't want to block other updates on those records. --- core/app/models/spree/store_credit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb index df6f88399e1..200a741d984 100644 --- a/core/app/models/spree/store_credit.rb +++ b/core/app/models/spree/store_credit.rb @@ -39,7 +39,7 @@ class Spree::StoreCredit < Spree::PaymentSource before_validation :associate_credit_type before_validation :validate_category_unchanged, on: :update before_destroy :validate_no_amount_used - validate :validate_no_amount_used, if: :discarded? + before_discard :validate_no_amount_used attr_accessor :action, :action_amount, :action_originator, :action_authorization_code, :store_credit_reason From 892e5dc84de35c81edeb5d0eaf3c3d13f9c1565b Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 7 May 2019 17:36:46 +0200 Subject: [PATCH 2/2] Ensure callback chain is aborted when store credit is not valid This is needed to avoid discard to set deleted_at anyway. Now that we are not running this method as a validation anymore we need to stop the callback chain by throwing abort, see: https://api.rubyonrails.org/classes/ActiveModel/Callbacks.html --- core/app/models/spree/store_credit.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb index 200a741d984..970dacdd44b 100644 --- a/core/app/models/spree/store_credit.rb +++ b/core/app/models/spree/store_credit.rb @@ -268,6 +268,7 @@ def validate_category_unchanged def validate_no_amount_used if amount_used > 0 errors.add(:amount_used, 'is greater than zero. Can not delete store credit') + throw :abort end end