Rails /lib modules and
There are two ways that files get loaded in Rails:
- It is registered in the autoload process, and you reference a constant that corresponds to the file name. For instance, if you have
app/controllers/pages_controller.rb
and reference PagesController,app/controllers/pages_controller.rb
will automatically be loaded. This happens for a preset list of directories in the load path. This is a feature of Rails, and is not part of the normal Ruby load process. - Files are explicitly
require
d. If a file isrequire
d, Ruby looks through the entire list of paths in your load paths, and find the first case where the file yourequire
d is in the load path. You can see the entire load path by inspecting $LOAD_PATH (an alias for $:).
Since lib
is in your load path, you have two options: either name your files with the same names as the constants, so Rails will automatically pick them up when you reference the constant in question, or explicitly require the module.
I also notice that you might be confused about another thing. ApplicationController is not the root object in the system. Observe:
module MyModule def im_awesome puts "#{self} is so awesome" endendclass ApplicationController < ActionController::Base include MyModuleendclass AnotherClassendAnotherClass.new.im_awesome# NoMethodError: undefined method `im_awesome' for #<AnotherClass:0x101208ad0>
You will need to include the module into whatever class you want to use it in.
class AnotherClass include MyModuleendAnotherClass.new.im_awesome# AnotherClass is so awesome
Of course, in order to be able to include the module in the first place, you'll need to have it available (using either of the techniques above).
In Rails 3 /lib modules are not loaded automatically.
This is because the line:
# config.autoload_paths += %W(#{config.root}/extras)
inside config/application.rb is commented.
You can try to uncomment this line or, (it worked even better for me), leave this commented (for future reference) and add this two lines:
config.autoload_paths += %W(#{config.root}/lib)config.autoload_paths += Dir["#{config.root}/lib/**/"]
What worked for me, besides uncommenting config.autoload_paths (I’m on Rails 3.1.3), was to create a initializer like this:
#config/initializers/myapp_init.rbrequire 'my_module' include MyModule
This way I can call mymodule methods from anywhere and as class methods Model.mymodule_method
or as instance methods mymodel.mymodule_method
Maybe some expert may explain the implications of this. By now, use it at your own risk.
Edit: Afterwards, I think a better approuch would be:
create a initializer like this:
#config/initializers/myapp_init.rbrequire ‘my_module’
Include the module where needed, like this:
1) if you want to use it as "Class Methods" use "extend":
class Myclass < ActiveRecord::Base extend MyModule def self.method1 Myclass.my_module_method endend
2) if you want to use it as "Instance Methods" include it inside Class definition:
class Myclass < ActiveRecord::Baseinclude MyModule def method1 self.my_module_method endend
3) remember that include MyModule
refers to a file my_module.rb
in your load path that must be required first