How to deal with vendor/plugins after upgrading to

2019-03-08 14:37发布

问题:

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.

回答1:

I just wrote a blog post on this: How to Convert Simple Rails 2.3 Style Plugins for Rails 3.2



回答2:

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!



回答3:

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



回答4:

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.