Skip to content

Commit

Permalink
Improve error while retrieving invalid payment methods
Browse files Browse the repository at this point in the history
Show a simple error after retrieving payment methods no more supported,
suggesting to run a specific rake task to fix the issue and
 without having to delete those payment methods.
  • Loading branch information
luca-landa committed Nov 16, 2020
1 parent 646f3e3 commit 6729498
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Spree
# This class is not meant to be instantiated. Please create instances of concrete payment methods.
#
class PaymentMethod < Spree::Base
class UnsupportedPaymentMethod < StandardError; end

preference :server, :string, default: 'test'
preference :test_mode, :boolean, default: true

Expand All @@ -31,6 +33,16 @@ def self.const_missing(name)
end
end

def self.find_sti_class(type_name)
super(type_name)
rescue ActiveRecord::SubclassNotFound
raise UnsupportedPaymentMethod, "Found invalid payment type '#{type_name}'.\n"\
"This may happen after switching payment service provider, when payment methods "\
"reference old types that are not supported any more.\n"\
"If that is the case, consider running 'rake payment_method:deprecate_unsupported_payment_methods' "\
"to fix the issue."
end

validates :name, :type, presence: true

has_many :payments, class_name: "Spree::Payment", inverse_of: :payment_method
Expand Down
18 changes: 18 additions & 0 deletions core/spec/models/spree/payment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,22 @@ def gateway_class
expect(described_class::DISPLAY).to eq([:both, :front_end, :back_end])
end
end

describe "#find_sti_class" do
context "with an unexisting type" do
context "while retrieving records" do
let!(:unsupported_payment_method) { create(:payment_method, type: 'UnsupportedPaymentMethod') }
before do
unsupported_payment_method.update type: 'UnsupportedPaymentMethod'
end

it "raises an UnsupportedPaymentMethod error" do
expect { Spree::PaymentMethod.all.to_json }
.to raise_error(
Spree::PaymentMethod::UnsupportedPaymentMethod,
/Found invalid payment type 'UnsupportedPaymentMethod'/)
end
end
end
end
end

0 comments on commit 6729498

Please sign in to comment.