可安装的发动机和模块。如何在引擎中创建lib文件?

时间:2013-06-20 20:47:50

标签: ruby-on-rails-3.2 rails-engines

我正在为我的公司用来执行各种任务的应用程序制作引擎。我的引擎需要使用github_api gem来查看和更新​​存储库。虽然我可以使用控制器(或模型)中的代码访问gem,我想将这些方法放在lib文件夹中的模块中,因为我还需要连接到另一个api而不是想要使用不应该存在的代码来混乱我的控制器或模型。

但是,似乎我的引擎不喜欢我的模块。它甚至不想加载文件,即使我在engine.rb文件中指定它也是如此。我将文件放在引擎的lib / modules文件夹中,并在其中指出路径here

我的错误如下: 未初始化的常量MyEngine :: WelcomeController :: Githubapi

就像它只是不加载我的文件。当我尝试要求它时,它会给我错误并且不会启动服务器。

engine.rb

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    config.autoload_paths << File.expand_path("../lib/modules/", __FILE__)
  end
end

githubapi.rb

module Githubapi

  # Lists the repository branch names for dropdown-list
  # param[String] repo_name the repository name as GitHub sees it
  def get_repo_branches(repo_name)
    branch_names = Array.new
    git_connection = Github.new :oauth_token => ENV['GITHUB_TOKEN']
    branches = git_connection.repos.branches('owner_name',repo_name)
    branches.each do |branch|
      branch_names << branch.name
    end
    branch_names.sort_by!{ |m| m.downcase }
    return branch_names
  end

end

调用控制器中的方法传递给视图总是会导致错误

@branches = Githubapi.get_repo_branches('my_repo')

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用“add_dependency”不会引起引擎的gemspec吗?

Gem::Specification.new do |s|
[....] stuff [....]
  s.add_dependency "github_api"
[....] stuff [....]
end

这就是你发动机使用其他宝石的信号。