On Wed, Apr 16, 2014 at 4:59 PM, Stephen Paul Weber <
[email protected]> wrote:
On Tuesday, 15 April 2014 20:17:29 UTC-5, Xavier Noria wrote:
>>
>> I don't think automatic namespacing is the way to go, but out of
>> curiosity if user.rb has
>>
>> class User < AR::Base
>> end
>>
>> and UsersController references User and triggers loading that file, how
>> would you put User under a namespace? What about the name of the class?
>> What about the nesting by which the constants in User are resolved?
>>
>
> Say the Rails app is named Foo and so the module Foo is the one in
> config/application. Well then when the autoloader sees a request for
> constant User for the first time, it would look for a files named user.rb
> with a class in them named User and load them as module Foo; load
> 'user.rb'; end --
>
This doesn't do what you want: load and require evaluate code resetting the
nesting, that's why you can require a file wherever you want, inside a
method or whatever, without altering the context of the code in the file.
You can at most pass a flag to get an anonymous module created by the
interpreter injected.
The only way to do that that I can think of is to manually remove the
constant from Object and add it to the app module (in which case the name
of the class would not reflect the namespace), or else use some method from
the eval family passing File.read('user.rb').
Any of the options are terrible in my view, expectations are broken all
over the place.
Let me add a remark here: I personally dislike the word "magic" for Rails
features. I believe it has been abused. Everything is deterministic and
known, there is nothing magical. has_many has a dozen options and you can
control everything. What Rails provides are carefully chosen defaults and
conventions that work together in a really integrated way. Everything
designed so that we work as less as possible in the most common case.
End of tangent!
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.