升级到rails 3.2.1后如何处理供应商/插件

发布于 2025-01-01 18:43:11 字数 476 浏览 2 评论 0原文

升级到rails3.2.1后,出现此警告:

You have Rails 2.3-style plugins invendor/plugins! Rails 4.0 中将删除对这些插件的支持。将它们移出并将它们捆绑到您的 Gemfile 中,或者将它们作为 lib/myplugin/* 和 config/initializers/myplugin.rb 折叠到您的应用程序中。有关更多信息,请参阅发行说明:http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released

我将插件移至供应商/插件中目录但我不知道如何编写 config/initializers/myplugin.rb 文件,并且谷歌找不到答案。

After upgrading to rails3.2.1,this warning occurs:

You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released

I move my plugins in vendor/plugins directory but i don't know how to write config/initializers/myplugin.rb file, and google can't find the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦屿孤独相伴 2025-01-08 18:43:11

我刚刚经历过这个,发现你只需要浏览每个插件来检查一些事情:

  • 它是 ruby​​gems 上的 gem 吗?如果是这样,只需将其放入 Gemfile 中并从 vendor/plugins 中删除。
  • 如果没有可用的 gem,或者 gem 已旧,请获取 vendor/plugins 中的文件夹并移动它到 lib/plugins

我遇到的一件事是,您需要手动要求所有这些插件。这是我创建并放置在 config/initializers/plugins.rb 中的初始化程序:

Dir[Rails.root.join('lib', 'plugins', '*')].each do |plugin|
  next if File.basename(plugin) == 'initializers'

  lib = File.join(plugin, 'lib')
  $LOAD_PATH.unshift lib

  begin
    require File.join(plugin, 'init.rb')
  rescue LoadError
    begin
      require File.join(lib, File.basename(plugin) + '.rb')
    rescue LoadError
      require File.join(lib, File.basename(plugin).underscore + '.rb')
    end
  end

  initializer = File.join(File.dirname(plugin), 'initializers', File.basename(plugin) + '.rb')
  require initializer if File.exists?(initializer)
end

我还遇到了某些插件所需的初始化程序的问题,因此我将这些特定的初始化程序移至 lib/plugins/initializers 文件夹。您必须将它们命名为插件的名称,因此 my_plugin 插件的初始化程序必须位于文件 lib/plugins/initializers/my_plugin.rb

希望如此有帮助!

I just went through this and found that you just have to go through each plugin to check a few things:

  • Is it a gem on rubygems? If so, just stick it in your Gemfile and delete from vendor/plugins
  • If no gem is available, or the gem is old, take the folder in vendor/plugins and move it to lib/plugins

One thing I ran across is that you then need to require all those plugins manually. Here is the initializer I created and placed in config/initializers/plugins.rb:

Dir[Rails.root.join('lib', 'plugins', '*')].each do |plugin|
  next if File.basename(plugin) == 'initializers'

  lib = File.join(plugin, 'lib')
  $LOAD_PATH.unshift lib

  begin
    require File.join(plugin, 'init.rb')
  rescue LoadError
    begin
      require File.join(lib, File.basename(plugin) + '.rb')
    rescue LoadError
      require File.join(lib, File.basename(plugin).underscore + '.rb')
    end
  end

  initializer = File.join(File.dirname(plugin), 'initializers', File.basename(plugin) + '.rb')
  require initializer if File.exists?(initializer)
end

I also had the problem of initializers I needed for some of the plugins, so I moved those particular initializers into the lib/plugins/initializers folder. You have to name them the name of the plugin, so an initializer for the my_plugin plugin would have to be in the file lib/plugins/initializers/my_plugin.rb

Hope this helps!

享受孤独 2025-01-08 18:43:11

其他两个答案都很好并且似乎有效。

但是,如果您的插件由供应商/插件/.../lib 下的单个 .rb 文件组成,并且供应商/插件/.../init.rb 只是一个

require 'pluginname'

那么您只需将单个文件复制到您的 lib 目录并将一个文件添加到 config/initializers 中,该文件需要 'yourpluginname'

这是一个使用acts_as_lated 插件的具体示例,该插件还不是一个 gem。

  1. 将vendor/plugins/acts_as_erated/lib/acts_as_erated.rb复制到lib/

  2. 使用以下内容创建文件config/initializers/acts_as_erated.rb...

    需要“acts_as_lated”

  3. 删除供应商/插件/acts_as_erated

both the other answers are good and seem to work.

However if your plugin consists of a single .rb file under vendor/plugins/.../lib and the vendor/plugins/.../init.rb is just a

require 'pluginname'

Then you can simply copy the single file to your lib directory and add a file to config/initializers that does a require 'yourpluginname'

Here is a concrete example using the acts_as_rated plugin which is not a gem yet.

  1. copy vendor/plugins/acts_as_rated/lib/acts_as_rated.rb to lib/

  2. create a file config/initializers/acts_as_rated.rb with the following...

    require 'acts_as_rated'

  3. delete vendor/plugins/acts_as_rated

旧故 2025-01-08 18:43:11

没有人提到转换为 Railtie 或 Rails::Engine。

只需将所有 ruby​​ 文件移至 gem 中 [使用捆绑 gem 来创建它以最小化摩擦]。

然后查看 Railtie 文档 [并找到您的目标 Rails 版本]:

https:// /apidock.com/rails/v3.2.13/Rails/Railtie

将旧插件 init.rb 转换为 Railtie 非常简单方式,它为您提供了通往 Rails 4+ 的途径。

有视图或控制器吗?,然后使用 Rails::Engine 代替。虽然从工作流程的角度来看,gems 中的引擎可能很复杂,但将插件转换为插件却相当简单。

帮自己一个忙,开始在 gem 中构建测试,而不是在 Rails 项目中。

No one has mentioned converting to a Railtie or Rails::Engine.

Just move all of your ruby files into a gem [use bundle gem to create it for minimal friction].

Then have a look at the Railtie docs [and find your target Rails version]:

https://apidock.com/rails/v3.2.13/Rails/Railtie

It's quite easy to just convert an old plugin init.rb to a railtie in this way, and it gives you a pathway to Rails 4+.

Got views or controllers?, then use a Rails::Engine instead. While engines in gems can be complicated from a workflow standpoint, converting a plugin to one is pretty trivial.

Do yourself a favor and start building tests right in the gem, instead of in the Rails project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文