Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoload_paths not aware of namespace?

In app/services, I have some classes, as Notification::Finder and Notification::Builder.

They are placed as app/services/notification/builder.rb and app/services/notification/finder.rb.

There is also the Notification class as a model, at app/models/notification.rb

The autoload_path is configurated as in config.autoload_paths += %W(#{config.root}/app/services)

When I try to load Finder, it works:

Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Finder
=> Notification::Finder

But when I try the Builder, I get a problem with the rails autoloading:

Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Builder
=> ActiveRecord::Associations::Builder

It just ignores the namespace I've used when the constant name (Builder) has already been defined by other namespace, and gets the ActiveRecord::Associations::Builder instead.

Is this the expected behavior, or a rails bug?

Going more detailed, the const_missing method at activesupport/dependencies.rb receives a const_name 'Builder', and nesting.inspect => 'nil'.

Curious that when I use constantize, it resolves as expected:

Loading development environment (Rails 3.2.9)
[1] pry(main)> 'Notification::Builder'.constantize
=> Notification::Builder

( Rails issue at github: https://github.com/rails/rails/issues/8726 )

like image 955
Victor Rodrigues Avatar asked Oct 21 '22 20:10

Victor Rodrigues


1 Answers

ActiveRecord::Associations::Builder is a module in Rails. If you have a Notification::Builder, you can ask it its class:

>> Notification::Builder
=> ActiveRecord::Associations::Builder
>> Notification::Builder.class
=> Module
>> Notification::Builder.ancestors
=> [ActiveRecord::Associations::Builder]

Is this expected behavior?

Yes

OK, so... what choices do you have?

  • You can use a different term than Builder. Like Factory. or Notification::NotificationBuilder

More info:
* http://www.rubydoc.info/docs/rails/3.1.1/ActiveRecord/Associations/Builder/Association
* http://apidock.com/rails/ActiveRecord/Associations/Builder

like image 139
Jesse Wolgamott Avatar answered Oct 24 '22 10:10

Jesse Wolgamott