[Rspec-devel] Using rspec with rails plugins, engines, etc.

Daniel Siemssen daniel at dr-siemssen.de
Wed Aug 30 07:01:43 EDT 2006

On 29.08.2006 19:40, Jay Levitt wrote:
> Daniel Siemssen wrote:
>> Seems like the new dependencies code in edge rails / rails 1.2 will
>> solve that if you use it's naming convention so that your libs get
>> autoloaded. I don't think that it's worth to put too much time in
>> producing something that achieves the same.
> Makes sense.  Can you point me to any discussions of those conventions? 
>   I keep looking for info on how the new dependencies code is coming, 
> but all I see is reports of broken plugins!
> Jay
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
Rails dependencies mechanism uses const_missing to load missing classes
and modules. It tries to find the classes/modules within these
directories (in this order):
 * /test/mocks/#{RAILS_ENV}
 * /app/controllers/*
 * /app/models/[_a-z]*
 * /components/[_a-z]*
 * /app
 * /app/models
 * /app/controllers
 * /app/helpers
 * /app/services
 * /app/apis
 * /components
 * /config
 * /lib
 * /vendor/plugins/*/lib/
Add more directories via config.autoload_paths in environment.rb.

If you use the the constant MyModule::MyClass, then rails searches for a
file my_class.rb in the directory my_module
("MyModule::MyClass".underscore). If you have a
/components/my_module/my_class.rb and a /lib/my_module/my_class.rb, then
only the first file gets loaded/required by rails. When rails loads a
file it will also be added to a set of already loaded files, so that
rails can check whether a file is already loaded or not or unload all
already loaded files (Dependencies.clear).

This results in the following conventions:
 * module name is equivalent to directory name
 * class name is equivalent to file name
 * classes and modules are always in MixedCase (as all classes and
modules in ruby are) and will be translated to underscore (e.g. MyClass
to my_class)
(* every file should only contain the class that corresponds to it's
file name)

As you see conventions haven't changed. But now all classes that are
loaded automatically can be reloaded and not only those that descend
from a class that includes the Reloadable module.

Take a look at activesupport/lib/active_support/dependencies.rb if you
want to know more and please correct me if I'm wrong.


More information about the Rspec-devel mailing list