mercredi 16 mars 2016

Eagerloading files in directories inside the app folder in Rails 3. How is it done?

I read this:

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

(e.g. app/models/concerns/, app/models/products/)

Ask: do I want to namespace modules and classes inside my new dir? For example in app/models/products/ you would need to wrap your class in module Products.

If the answer is yes, do nothing. It will just work.

If the answer is no, add config.autoload_paths += %W( #{config.root}/app/models/products )

I want to know how Rails does this. How does it:

  1. Load the file inside a folder (named folder_nest) in the app folder only if the contents of that file are wrapped in a module named after that folder (folder_nest module). How does this happen?

There must be some logic that says: "if the thing inside of app is a file, eager load it. If it's a folder, only load the contents of said folder if it's wrapped in a module named after said folder."

Anyone know where this logic is? How do I read Rails source code?

Also, is config/initializers eagerly loaded? Where is that logic?

Aucun commentaire:

Enregistrer un commentaire