2
我知道我可以给模型添加新的方法,但我似乎无法覆盖现有的方法。下面是我有如何从Ruby中的模块覆盖现有的实例方法?
在我User.rb
include ExtraMethods
def is_invisible?
true unless self.active?
end
在我的模块
module ExtraMethods
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def user_extra_methods
include ExtraMethods::InstanceMethods
end
end
module InstanceMethods
def is_invisible?
true unless self.active? || self.admin?
end
end
end
ActiveRecord::Base.send(:include, ExtraMethods)
User.send(:user_extra_methods)
我希望发生的是在插件的方法来覆盖模型中的方法。任何想法或参考将是伟大的,似乎无法找到一个很好的参考。
谢谢!
J