Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails reloading classes and 'has been removed from the module tree but is still active!' ArgumentError

I wrote custom instrumentation in my Rails application. I enable it in config/initializers/instrumentation.rbfile like this:

ActiveSupport.on_load(:action_controller) do
  include FooBar::ControllerRuntime
end

But this leads me to errors A copy of FooBar::ControllerRuntime has been removed from the module tree but is still active!. I figure it out that I can resolve it in two ways:

  • Adding path where may 'FooBar::ControllerRuntimeis defined toconfig.autoload_one_paths`
  • Defining :to_prepare callback in ActionController::Railtie

Second solution looks like this:

config.to_prepare do
  ActionController.include FooBar::ControllerRuntime
end

This long introduction leads to question: which way is better? With first I am disabling reloading of classes which lay in the same path as my FooBar::ControllerRuntime. With second I don't feel it is good to messup with ActionController::Railtie. Right know ActionController::Railtie doesn't have defined to_prepare but what happend if in next release it will have?

like image 835
Sebastian Avatar asked May 12 '14 08:05

Sebastian


Video Answer


1 Answers

The first approach looks cleaner -

Adding path where may 'FooBar::ControllerRuntimeis defined toconfig.autoload_one_paths`

Reasons -

1) If you really wanna do some monkey patches in file like lib/extensions.rb, you may manually require it:

in config/initializers/require.rb:

require "#{Rails.root}/lib/extensions"

2) Follows proper naming conventions as you will have to list down the class and module .

I wouldn't suggest auto-loading for production application though , but if it's the last option than you can certainly try it out .

Good read here on the same - http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/

like image 190
Caffeine Coder Avatar answered Oct 08 '22 23:10

Caffeine Coder