13

I am extending a class (which is in a plugin) by including a module, this is done in an initializer.

require 'qwerty/core/user'
User.send :include, Qwerty::Core::Extensions::User

However in development before every request (and after reload! is called in the console) all models are reloaded but because the initializers are not run again the module is not included. Leaving a model with 'missing parts'.

Because the model is in a plugin it does not seem wise to include code directly in the class which would be the usual approach.

For now I have simply added a before_filter which includes the module if in development environment. But I have copy/pasted and have duplicate code in the initializer and application controller.

  # Class extensions in initalizers are over-writtern each request
  def development_loading
    if RAILS_ENV == 'development'      
      User.send :include, Qwerty::Core::Extensions::User
    end
  end

Is there a better way?

As a side note the plugin is mine, so I could add code in to it, but the extensions held in the module may not always be present...

5 Answers 5

12

environment.rb

config.to_prepare do
  User.send :include, Qwerty::Core::Extensions::User
end

The code is the block is run before every request in development mode and once in production mode.

2
  • 1
    This worked for me, but in application.rb (rails 3.0.20), not environment.rb
    – tekniklr
    Oct 28, 2013 at 19:15
  • This is perfectly works in rails 4 if add this to engine.rb in rails engine. Thanks!
    – Evgeniy B
    Mar 2, 2016 at 17:10
6

In Rails 3.x you can configure a block to run every time reloads happen (in development mode, or when config.cache_classes = false). This would go in an initializer:

ActionDispatch::Callbacks.to_prepare do
    # configure stuff or initialize
end
3

Slightly more elegant solution than the one accepted since it can be put in an initializer:

require 'dispatcher'

Dispatcher.to_prepare do
  # stuff that needs to happen once per initialization
end
1

At first I was going to advise something about adding a 'development' directory to the front of your load path in development mode, so that your revisions would always get reloaded first... But then it occurred to me that you said something confusing.

The model you're trying to extend. It's in a plugin? Plugins aren't supposed to be reloaded by default in development mode, unless the app explicitly says they should in its configuration by setting Config.reload_plugins? to false.

But if, for some reason, your plugin is reloading anyway and you don't want it to, you can put this in your plugin's init.rb to explicitly say it shouldn't reload:

Dependencies.load_once_paths << lib_path

See the Rails docs on the Configuration class for more detail: http://api.rubyonrails.org/classes/Rails/Configuration.html#M002536

3
  • Yes, I have reload_plugins? set to true because I am actively developing the plugin. I want to work out a way of extending the models in the plugin from the Rails app (ie. without touching the plugin code). A little confusing yes! In my init.rb I have: if RAILS_ENV == 'development' ActiveSupport::Dependencies.load_once_paths.reject!{|x| x =~ /^#{Regexp.escape(File.dirname(FILE))}/} end
    – Kris
    Oct 9, 2009 at 11:49
  • I think my problem is because I am trying to do two things at once - work on a plugin (reload it every request) and extend the plugin from the Rails app. A bit catch 22...
    – Kris
    Oct 9, 2009 at 11:54
  • Why? Why give yourself all these constraints? You don't want to add development code to the plugin, you don't want to load it just once, and you want to reverse the idea of a plugin by extending your plugin from your app (which is philosophically backwards). What do you gain from all these special cases? Is adding some if RAILS_ENV=='development' conditions into your plugin going to break something? You could always take it out again later, or put the extensions in files that only live in a special Git branch. Quit fighting the river. Go with the flow. Let Rails be Rails. Get it done.
    – SFEley
    Oct 10, 2009 at 2:13
0

Why do you use initializers to include functionality?

Try the following instead:

require 'qwerty/core/user'
class User < ActiveRecord::Base
  include Qwerty::Core::Extensions::User
  # bla bla
end
2
  • Because he only wants that code included in development mode.
    – SFEley
    Oct 8, 2009 at 20:07
  • Because the model is in a plugin, which while it is a plugin i am developing, i am treating it like a third party plugin, so I dont want to touch the code in it. So I must extend those classes from the outside.
    – Kris
    Oct 9, 2009 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.