狂欢模块装饰

23t*_*tux 6 ruby ruby-on-rails decorator spree ruby-on-rails-3

我在网上商店使用Spree Commerce.我想在结帐过程中更改一些行为,这是在app/models/spree/order/checkout.rbspree gem中定义的.所以我checkout_decorator.rb在我的应用程序中同时提出了一个问题.

问题是,我的更改未加载.另一个问题是,模块内的所有内容都在一个方法内,即def self.included(klass)方法.所以我认为我必须覆盖整个文件,而不是只覆盖一个方法.这是我的装饰者的样子:

checkout_decorator.rb

Spree::Order::Checkout.module_eval do
  def self.included(klass)
    klass.class_eval do
      class_attribute :next_event_transitions
      class_attribute :previous_states
      class_attribute :checkout_flow
      class_attribute :checkout_steps

      def self.define_state_machine!
         # here i want to make some changes
      end

      # and the other methods are also include here
      # for readability, i don't show them here
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

checkout.rbspree gem中的原始文件如下所示:

module Spree
  class Order < ActiveRecord::Base
    module Checkout
      def self.included(klass)
        klass.class_eval do
          class_attribute :next_event_transitions
          class_attribute :previous_states
          class_attribute :checkout_flow
          class_attribute :checkout_steps

          def self.checkout_flow(&block)
            if block_given?
              @checkout_flow = block
              define_state_machine!
            else
              @checkout_flow
            end
          end

          def self.define_state_machine!
             # some code
          end

          # and other methods that are not shown here
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:为什么这不起作用?这module_eval是正确的方法吗?我试过class_eval但它也不起作用.我怎么解决这个问题?

gma*_*all 1

module_eval 方法不适合你。

您应该查看Spree 结账流程文档,了解一些有关如何自定义结账流程的好示例。这是自定义结帐流程的推荐方法,因为您不需要复制/粘贴一大堆代码。