我正在为我的在线商店使用 Spree Commerce。我想在结帐过程中更改一些行为,这些行为在 spree gem 内的 app/models/spree/order/checkout.rb
中定义。所以我在我的应用程序中的同一点创建了一个 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
来自 spree gem 的原始文件 checkout.rb
如下所示:
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
所以我的问题是:为什么这不起作用? module_eval
是执行此操作的正确方法吗?我尝试了 class_eval
但它也不起作用。我该如何解决这个问题?
最佳答案
module_eval 方法对您不起作用。
你应该看看 Spree Checkout Flow Documentation有关如何自定义结帐流程的一些很好的示例。这是自定义结帐流程的推荐方法,因为您不需要复制/粘贴一大堆代码。
关于ruby-on-rails - Spree 模块装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810506/