无法将 类 从我的 gem 加载到我的 rails 应用

Unable to load classes from my gem to my rails app

我有一些通用代码想在我的两个 rails 应用程序之间共享。

我已经像这样生成了我的 gem :

bundle gem document-common

我的 gem规格看起来是这样的:

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'document_common/version'

Gem::Specification.new do |spec|
  spec.name          = "document_common"
  spec.version       = DocumentCommon::VERSION
  spec.authors       = ['Author']
  spec.email         = ['test@domain.com']

  spec.summary       = 'Common Models and Lib'
  spec.description   = 'Common Models and Lib'
  spec.homepage      = 'google.com'
  spec.license       = 'WTFPL'

  spec.files      = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.rdoc"]
  spec.test_files = Dir["spec/**/*"]
  spec.test_files.reject! { |file| file.match(/[.log|.sqlite3]$/) }

  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.11"
  spec.add_development_dependency "rake", "~> 10.0"
end

在我的 lib 文件夹中我有这个:

document_common
document_common.rb

文件夹 document_common 和 ruby 文件 document_common.rb,并且 ruby 文件具有以下内容:

require 'document_common/version'

module DocumentCommon
  # Your code goes here...
end

然后在 document_common 里面有一个 version.rb 内容是这样的:

module DocumentCommon
  VERSION = '0.1.0'
end

并且 document.rb 具有以下内容:

module DocumentCommon
  class Document < ActiveRecord::Base

  end
end

所以我将这个 gem 推送到我的 git 存储库。然后我在 rails 4 应用程序中将此 gem 信息添加到我的 Gemfile 中进行捆绑安装,但是当我引用 DocumentCommon::Document 时出现此错误:

NameError: uninitialized constant DocumentCommon::Document

但是,如果我检索有关版本的信息 DocumentCommon::VERSION,我不会收到任何错误并获得实际版本。此外,当我执行 DocumentCommon.constants 时,我得到 [:VERSION]。我在这里做错了什么?我需要做什么才能在我的主 rails 应用程序中访问 DocumentCommon::Document 模型?

您可以使用自动加载,查看设计 gem 是否确实如此:

https://github.com/plataformatec/devise/blob/master/lib/devise.rb