我为我的网上商店使用Spree Commerce。我想在结账过程中更改一些行为,这在冒险宝石里面的app/models/spree/order/checkout.rb
中定义。所以我在我的应用程序的同一点做了一个checkout_decorator.rb
。Spree模块装饰器
问题是,我的更改没有加载。另一个问题是,模块内的所有内容都在一种方法中,即方法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
从大礼包宝石原始文件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
,但它也不起作用。我该如何解决这个问题?