When and Where Do I Require Files in a Rails Application

When and where do I require files in a rails application?

This is a helpful post about this issue.

In short, Rails autoloads classes in your lib directory only if they follow the proper naming conventions.

Ruby on Rails: where to put the 'require' files?

I've always used gems and put them in the gemfile, but I think this is different...

No, this is no different. Barometer is a Rubygem and putting it in your Gemfile is exactly the way to use it.

As with every library, your require should go in whichever file uses the code, for example the same file that the Barometer.new call is. You don't always need the require line depending on your Ruby environment, but it's always a good idea to get used to it

When to call a require in Rails?

If you're concerned about performance then you should require things in the context of where they are needed so that if that portion of your code is not exercised, the library is not loaded. Any subsequent calls to require have no effect as that file has already been loaded. This ends up looking like something along the lines of:

if (user.using_openid?)
require 'openid'

# ... Do OpenID stuff
end

While this is more efficient in terms of resources, it can make it very difficult to determine the dependencies of your application. Declaring these up-front makes it clear to other people maintaining the software. Keep in mind that "other people" always includes your future self when you've forgotten about some details of your application.

You're technically allowed to require anything at any time, late or early, but declaring your requirements up front is better from a design perspective. If you find that there is an element that is used only intermittently and takes an unusual amount of time or memory to load, then you should probably document that up front in your requirements file. For example:

require 'library1'
require 'library2'
require 'library3'
require 'library4'
require 'library5'

# Other libraries loaded as required:
# * slowimagelibrary
# * slowencryptionlibrary
# * openid

Arguably this is less of an issue with bundler because you can have your gems declared up front more formally and the actual require call can come later.

Rails : how to require files from lib folder in order to use them in controllers?

Requiring and including are different. Requiring is the process of loading the file and (making MyApp::UseCases::UseCase1 available), whereas include will mix a module into the current module; this has the effect of making its constants available in the current context, as well.

In an initializer, try explicitly requiring your files:

require File.join(Rails.root, "lib", "myapp", "use_cases", "use_case1.rb")
require File.join(Rails.root, "lib", "myapp", "use_cases", "use_case2.rb")

Then include the module containing your classes:

include MyApp::UseCases

This should make all the constants under UseCases available for lookup in the current scope, which in the case of an initializer will be the main scope, making those constants effectively available everywhere.



Related Topics



Leave a reply



Submit