Skip to content

Commit

Permalink
Add comment to clarify intent
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Nov 20, 2019
1 parent 01d3dbc commit 13e4517
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/extensions/require_nested.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ def require_nested(name)
require_dependency filename
end

# If the nested constant has a top-level constant with the same name, then both
# must be defined at the same time, otherwise the Rails autoloader can get
# confused.
#
# For example, suppose we have the following
#
# # app/models/thing_grouper.rb
# class ThingGrouper < ApplicationRecord
# require_nested :Thing
# end
#
# # app/models/thing_grouper/thing.rb
# class ThingGrouper::Thing; end
#
# # app/models/thing.rb
# class Thing < ApplicationRecord; end
#
# When the require_nested call is made, we will define `ThingGrouper`'s nested
# `Thing`, but at the same time we must also define `::Thing` in order to allow
# the Rails autloader to work correctly. We do this by using a special-case call
# to `require_nested` on `Object`, if a top-level `thing.rb` file exists.
if ActiveSupport::Dependencies.search_for_file(name.to_s.underscore) && self != Object
Object.require_nested name
end
Expand Down

0 comments on commit 13e4517

Please sign in to comment.