检查agains你的代码...
的模块结构可能看起来像这样(从我的项目,那肯定工作的一个考虑):
# lib/taggable.rb
require 'active_support/concern'
module Taggable
extend ActiveSupport::Concern
module ClassMethods
def taggable
include TaggableMethods # includes the instance methods specified in the TaggableMethods module
# class methods, validations and other class stuff...
end
end
module TaggableMethods
# instance methods...
end
end
现在缺少的是,你应该告诉Rails从lib
目录加载模块:
# config/application.rb
module AppName
class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)
# rest ommited...
现在模块应该包括在内。
# model.rb
class Model
taggable
end
这是基本插件的工作原理。在您的问题中提到的教程的作者编写了一个插件,该插件仅针对从ActiveRecord::Base
继承的模型,因为他正在使用其特定方法(例如update_column
)。
如果你的模块不依赖于ActiveRecord方法,则不需要扩展它(该模块也可以被Mongoid模型使用)。但是,这绝对是不的正确方法:
class ActiveRecord::Base
include HasDefault
end
如果你真的需要延长的ActiveRecord,做这种方式:
ActiveRecord::Base.extend ModuleName
当然还有很多其他的方式来编写插件依赖根据您的需求,以各种轨道gems
为灵感。
你的哪个模块包含ActiveSupport :: Concern? – Yanhao
扩展ActiveSupport :: Concern的是基于我的项目中的/ lib库。 – Ruslan