Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force my plugin to reload with each request?

As I understand it, plugins are not reloaded in Rails with each request in development mode. Which makes sense, as generally you add plugins to your app and it's the app you're developing.

But if you are developing a plugin, you have to restart the server with each change to the plugin which has a significant overhead.

Is there any way to make Rails reload your plugin during development, the way it reloads your models and controllers?

like image 872
msaspence Avatar asked Jan 17 '11 12:01

msaspence


3 Answers

I have been struggling with this for some time, too. None of the solutions work, including the autoload_paths and autoload_once_paths tricks. Furthermore, the hack with FileUpdateChecker and initializers also does not help (the checker triggers properly, but the files are not reloaded). Same for config.reload_plugins = true...

However, there is a solution. In app/controllers/application_controller.rb add one line: require_dependency 'your_file_name_here' The application controller is reloaded on every request and require_dependency makes your file to be checked for modifications and reloaded accordingly. It worked for me, Apache 2, Passenger 3 and Rails 3.0.3.

like image 65
Miki Avatar answered Nov 15 '22 18:11

Miki


I do this by using the shotgun gem.

gem install shotgun

cd /path/to/rails/app

shotgun

slower response time, but reloading all the rails code, not wasting time to write autoload_paths

like image 26
Stephan Keller Avatar answered Nov 15 '22 17:11

Stephan Keller


Easy way, develop your plugin in a folder inside the "app" folder:

  • app
    • models
    • controllers
    • helpers
    • views
    • your_plugin_here

This way, all your plugin classes will be reloaded on each request.

Another possibility is to add the path at your application.rb file:

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)

module SunspotTutorial
  class Application < Rails::Application

    config.autoload_paths += %W{ #{config.root}/plugins/your_plugin_name/lib }

    #lots of other code
  end
end

This way your plugin is going to be reloaded all the time.

like image 45
Maurício Linhares Avatar answered Nov 15 '22 19:11

Maurício Linhares