Skip to content
This repository
Browse code

Correct inconsistencies in rails lib files guide

  • Loading branch information...
commit 966e69c41a11270a3cc3b8b0ca290f6909ae67f7 1 parent d91055e
Ryan Bigg authored

Showing 1 changed file with 11 additions and 3 deletions. Show diff stats Hide diff stats

  1. +11 3 rails-lib-files.md
14 rails-lib-files.md
Source Rendered
@@ -2,7 +2,9 @@
2 2
3 3 In Rails's directory structure as far back as I can recall, there's always been a `lib` folder. This folder is for files that don't belong in the `app` folder (not controllers, helpers, mailers, models, observers or views), such as modules that are included into other areas of the application. The `lib` directory is for our code that won't 'fit' in the `app` directory.
4 4
5   -In Rails 3, this directory was removed from the autoload path which has lead to some frustration amongst people. We'll cover what autoloading does in the second part of this rather short and informative guide.
  5 +You can also see this directory in plugins and engines, as well as in all cool Ruby gems.
  6 +
  7 +In Rails 3, this directory was removed from the autoload path which has lead to some frustration amongst people when their files aren't being automatically loaded. We'll cover what autoloading does in the second part of this rather short and informative guide.
6 8
7 9 ## Using `lib` correctly
8 10
@@ -14,7 +16,7 @@ To require this file, we can do this in our application:
14 16
15 17 Then we've got access to the WildcardSearch module where and when we need it.
16 18
17   -## Autoloading
  19 +## Autoloading with Rails
18 20
19 21 Now with autoloading in Rails, we don't need to even require these files to access the constants defined in them. There's a configuration option called `load_paths` for Rails 3 applications which lives in `config/application.rb`, but is commented out by default. We can uncomment this setting and configure it to specify the `lib` directory:
20 22
@@ -24,4 +26,10 @@ With this setting specified, we don't need to `require` the files in this direct
24 26
25 27 So how does this work? Well, Rails will take the constant name such as `WildcardSearch`, convert it to a string, then call `underscore` before searching for this file in all the `autoload_paths` that are specified. If it finds it, then it will then call `require` on this file and thereby define the constant and if it can't find this file then it will raise an `uninitialized constant` error. If the file is named incorrectly (such as `WildCardSearch.rb` instead), then Rails will be unable to find the file it's looking for, which will cause the constant to not be loaded.
26 28
27   -NOTE: If you have a lot of code in `lib` that is not required in your application (e.g. tasks) then you should not add it to `autoload_paths` because they are eager loaded when the application starts and may lead to excessive memory usage. Eager loading of all the files in `autoload paths` is done for thread safety as `require` is inherently unsafe.
  29 +Because this file is called `wildcard_search.rb`, it will be required in the search for the `WildcardSearch` constant. If this file doesn't define this constant then you will see this kind of error:
  30 +
  31 + Expected lib/wildcard_search.rb to define WildcardSearch
  32 +
  33 +In which case this must be corrected so that Rails's autoloading is happy.
  34 +
  35 +If you want to see more information, check out the `ActiveSupport::Autoload` module which provides this functionality in Rails.

0 comments on commit 966e69c

Please sign in to comment.
Something went wrong with that request. Please try again.