This project is archived and is in readonly mode.
Initializer should load files in parent directories before subdirectories to minimize dependency pain
Reported by Tom Ward | August 19th, 2008 @ 12:12 PM | in 2.x
The problem
I have the following files:
app/models/zoo.rb
class Zoo < ActiveRecord::Base
include HippoManangement
end
app/models/zoo/hippo_management.rb
module Zoo::HippoManagement
end
What happens
On starting the application up in some modes (but not all), I get a dependencies error, similar to 'Expected app/models/zoo/hippo_management.rb to define Zoo::HippoManagement'. This is cause by require_dependency being called on app/models/zoo/hippo_management.rb before app/models/zoo.rb.
The code calling require_dependency is in Rails::Initializer#load_application_classes:
def load_application_classes
if configuration.cache_classes
configuration.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").each do |file|
require_dependency file.sub(matcher, '\1')
end
end
end
end
The problem is that the Dir.glob enumerates app/models/zoo/hippo_mangement.rb before app/models/zoo.rb.
The solution?
One way to get around this would be to sort the globbed files before requiring them. Another (far more involved) solution would be to fix the dependencies code so it doesn't fall over in this situation.
Comments and changes to this ticket
-
Tom Ward August 19th, 2008 @ 01:29 PM
- Tag changed from dependencies, eager_loading to dependencies, eager_loading, patch
I've written a patch (with tests) which sorts the globbed files. Having thought about this in some depth, I'm convinced that eager loading should load parents before children.
In my particular situation (using modules to partition behaviour) I'm aware there are alternative solutions (such as Rick's concerned_with) but I believe using modules is not only simpler but more flexible. In any case, eager loading breaks the concerned_with pattern as it forces all concerns to be loaded, whether declared or not.
-
Repository August 22nd, 2008 @ 01:09 PM
- State changed from new to resolved
(from [89d1c77dd012f087c091e0f23874c582ea4e3703]) Initializer to sort files before eager loading. [#859 state:resolved]
Changed Rails::Initializer to sort files before eager loading them. This ensures that any files in a parent directory will be loaded before files in a subdirectory of the 'same' name. i.e. zoo.rb will be loaded before zoo/reptile_house.rb
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 859 Initializer should load files in parent directories before subdirectories to minimize dependency pain (from [89d1c77dd012f087c091e0f23874c582ea4e3703]) Initial...