这里的交互有点复杂,请耐心等待。 我正在使用Spree。 Spree在其一些模型中使用了delegate_belongs_to,包括“Spree :: Variant”。 'delegate_belongs_to:product,:available_on(...)'在原始类体中被调用。我如何重写一个宝石添加到ActiveRecord :: Base的类方法(在我的装饰器中)
我希望变体能够拥有自己的available_on日期。 delegate_belongs_to被注入本身就像这样:
module DelegateBelongsTo
extend ActiveSupport::Concern
module ClassMethods
#...
def delegate_belongs_to(association, *attrs)
#...
end
end
end
ActiveRecord::Base.send :include, DelegateBelongsTo
我宁愿不重写整个variant类删除此一个参数。这是我最近的尝试之一:
Spree::Variant.class_eval do
class << self
alias_method :original_dbt, :delegate_belongs_to
def delegate_belongs_to(association, *attrs)
attrs.delete [:available_on]
original_dbt(association, attrs)
end
end
attr_accessible :available_on
#...
end
我已经尝试了一些这方面的变化。我不确定是否它是因为它在class_eval中,如果执行顺序有问题,或者是什么,但我似乎无法重写此方法。我在这里无法理解什么?
谢谢。
我正在研究迄今为止提供的解决方案。一个注意,在轨道控制台'Spree :: Product.method(:delegate_belongs_to).source_location '给了我原来的位置,'Spree :: Variant.method(:delegate_belongs_to).source_location'给了我在我的装饰器中的定义线。方法中的断点虽然没有做任何事情。 – 2012-07-19 21:04:42