Ruby on rails Spree模块装饰器
我正在为我的网上商店使用Spree Commerce。我想在结帐过程中改变一些行为,这是在SpreeGem中的Ruby on rails Spree模块装饰器,ruby-on-rails,ruby,ruby-on-rails-3,decorator,spree,Ruby On Rails,Ruby,Ruby On Rails 3,Decorator,Spree,我正在为我的网上商店使用Spree Commerce。我想在结帐过程中改变一些行为,这是在SpreeGem中的app/models/spree/order/checkout.rb中定义的。因此,我在我的应用程序的同一点上进行了checkout\u decorator.rb 问题是,我的更改没有加载。另一个问题是,模块中的所有内容都在一个方法中,defself.included(klass)方法。所以我想我必须覆盖整个文件,而不是一个方法。以下是我的装饰师的样子: checkout\u decor
app/models/spree/order/checkout.rb
中定义的。因此,我在我的应用程序的同一点上进行了checkout\u decorator.rb
问题是,我的更改没有加载。另一个问题是,模块中的所有内容都在一个方法中,defself.included(klass)
方法。所以我想我必须覆盖整个文件,而不是一个方法。以下是我的装饰师的样子:
checkout\u 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\u eval
是正确的方法吗?我试过class\u eval
,但也不起作用。我该如何解决这个问题?模块评估方法不适合您
您应该查看,以获得一些关于如何自定义签出流的好示例。这是定制签出流的推荐方法,因为您不需要复制/粘贴一大堆代码。名称空间不正确
尝试一下Spree::Order::Checkout.class\u eval dotl;dr:在Spree::Order类而不是Spree::Order::Checkout模块中覆盖所需的方法 您提到,在原始文件(spree_core-3.2.0.rc3/app/models/spree/order/checkout.rb)中,有一个方法可以包装整个模块
def self.included(klass)
klass.class_eval do
当模块包含在类中时,将调用此方法,并执行自己的class\u eval
将模块的方法添加到包含它的类的实例中
既然(spree_core-3.2.0.rc3/app/models/spree/order.rb)有这一行:
include Spree::Order::Checkout
我们可以向order类本身添加一个decorator(app/models/spree/order\u decorator.rb)