From ed5351d23ed072f1a73f33c9574da51fdd3485af Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 2 Mar 2020 19:02:07 +0100 Subject: [PATCH] Fix duplicate validations in payment methods and shipping methods Updates the syntax to follow the recommended usage in the code comments of the ActiveModel #validates_with method. Fixes: 12) Spree::PaymentMethod raises errors when required fields are missing Failure/Error: expect(pm.errors.to_a).to eq(["Name can't be blank", "At least one hub must be selected"]) expected: ["Name can't be blank", "At least one hub must be selected"] got: ["Name can't be blank", "At least one hub must be selected", "At least one hub must be selected"] (compared using ==) # ./spec/models/spree/payment_method_spec.rb:16:in `block (2 levels) in ' --- app/models/spree/payment_method_decorator.rb | 8 +++++++- app/models/spree/shipping_method_decorator.rb | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 4cbb7e85d9..ebe5a68ad2 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -10,7 +10,7 @@ Spree::PaymentMethod.class_eval do after_initialize :init - validates_with DistributorsValidator + validate :distributor_validation # -- Scopes scope :managed_by, lambda { |user| @@ -73,4 +73,10 @@ Spree::PaymentMethod.class_eval do name[i..-1] end end + + private + + def distributor_validation + validates_with DistributorsValidator + end end diff --git a/app/models/spree/shipping_method_decorator.rb b/app/models/spree/shipping_method_decorator.rb index 837fd45ee6..55f79c946b 100644 --- a/app/models/spree/shipping_method_decorator.rb +++ b/app/models/spree/shipping_method_decorator.rb @@ -6,7 +6,7 @@ Spree::ShippingMethod.class_eval do after_save :touch_distributors - validates_with DistributorsValidator + validate :distributor_validation scope :managed_by, lambda { |user| if user.has_spree_role?('admin') @@ -80,4 +80,8 @@ Spree::ShippingMethod.class_eval do def touch_distributors distributors.each(&:touch) end + + def distributor_validation + validates_with DistributorsValidator + end end