无法覆盖狂欢模型中的方法

Unable to override a method in spree model

我正在将 rails 和 spree 升级到 Rails 4.2 和 Spree 3.1

我重写了 Payment 模型中的 purchase! 方法

方法是这样的

module Spree
  class Payment < Spree::Base
    module Processing
      def purchase!
        started_processing!
        gateway_action(source, :purchase, :complete)
      end
    end
  end
end

这就是它在 rails 3.2 中被覆盖的方式(它一直有效到现在)

Spree::Payment::Processing.class_eval do
  def purchase!
    started_processing!
    if source.class == Spree::PurchaseOrder
      gateway_action(source, :authorize, :pend)
    else
      gateway_action(source, :purchase, :complete)
    end
  end
end

但现在 Rails 4.2 和 Spree 3.1

出现以下错误
/home/deepak/workspace/Project/app/models/spree/payment/processing_decorator.rb:2:in `<module:Spree>': superclass mismatch for class Payment (TypeError)
  from /home/deepak/workspace/Project/app/models/spree/payment/processing_decorator.rb:1:in `<top (required)>'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus/load_tracking.rb:76:in `load'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus/load_tracking.rb:76:in `load'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/zeus-0.15.10/lib/zeus/load_tracking.rb:68:in `load'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
  from /home/deepak/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
  from /home/deepak/workspace/Project/config/application.rb:16:in `block (2 levels) in <class:Application>'
  from /home/deepak/workspace/Project/config/application.rb:15:in `glob'
  from /home/deepak/workspace/Project/config/application.rb:15:in `block in <class:Application>'
# /initializers/spree_overrides.rb
module Spree
  class Payment < Spree::Base
    module Processing
      def purchase!
        started_processing!
        if source.class == Spree::PurchaseOrder
          gateway_action(source, :authorize, :pend)
        else
          gateway_action(source, :purchase, :complete)
        end
      end
    end
  end
end

将它放在初始化程序中,它将覆盖 Spree 中定义的原始方法。