What are the implications of using require_dependency in Rails 3 applications?

It seems to me that I understand the difference between require and require_dependency (from How to require a reload of require_dependency and Rails related constants? ).

However, I am wondering what should happen if I use some of the different methods there (see http://hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all-subdirectories / and The best way to load a module / class from the lib folder in Rails 3? ) is to download all the files so that we:

  • no need to use require_dependency all over the application and
  • no need to restart development servers when changing files in the lib directory.

It seems that development performance will be slightly affected, which is not so important for me. How to affect productivity in a production environment? All files are usually downloaded only once, if you are still in production? Is there a better way that I don't see?

If you could include some resources where I could learn more about this, they would be greatly appreciated. Some blog entries said that this behavior has recently changed from Rails 3 to autoreloading lib / * files, and that it was controversial, but I do not see any links to these discussions. It would be helpful to consider the pros and cons. Thanks!

+6
source share
1 answer

The code loader is disabled by default during production. Therefore, if you call require_dependency at the top of the file, it will be executed only once.

The Rails 3 change you were talking about is very small. You can usually call Foo and it will be loaded from app/models/foo.rb automatically. Before it can also be loaded from lib/foo.rb (These app/models and lib directories are called startup paths.) The Rails team decided to remove lib from startup paths in the third version. You can still get it back. But it is recommended that you leave less frequently modified and project-specific files in lib. If you have something that does not apply to any of the subdirectories of the default applications, such as app / models or app / controller, you do not need to embed them in lib. You can add your own subdirectory. For example, I have app/presenters . There is a discussion on the old issue if you would like more information about this.

+4
source

Source: https://habr.com/ru/post/898032/


All Articles