通过gem扩展模型的类方法

时间:2015-07-23 21:11:11

标签: ruby gem

我正在尝试编写一个允许我在Rails模型上调用类方法的gem。到目前为止,我有以下内容:

mygem /模型/ active_record_extension.rb

stop()

我要求此文件如下:

module ActiveRecordExtension
  extend ActiveSupport::Concern

  module ClassMethods
    def foo
      "foo"
    end
  end
end
# Include the extension 
ActiveRecord::Base.send(:include, ActiveRecordExtension)

我的gemspec文件包含必要的文件:

require "mygem/version"
require "mygem/models/active_record_extension"
require "mygem/railtie" if defined? Rails

module MyGem
  # Code goes here...
end

我已尝试过各种变体但由于某种原因我似乎无法在给定模型上调用 spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.files = Dir["{lib}/**/*.rb"] spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.9" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "rails", "~> 4.2" 。我目前得到一个NameError:未初始化的常量ActiveRecordExtension :: ActiveSupport。我错了吗?

谢谢!

0 个答案:

没有答案