Change where module file goes for namespaced AR models

24 views
Skip to first unread message

Matt Wire

unread,
Oct 18, 2017, 4:04:44 PM10/18/17
to Ruby on Rails: Core
Hello all!

Because Rails apps get cluttered up rather quickly, I am proposing a change to this Rails generator method:


def create_module_file
 
return if regular_class_path.empty?
 
template "module.rb", File.join("app/models", "#{class_path.join('/')}.rb") if behavior == :invoke
end
 
 

TO

Enter co
def create_module_file
 
return if regular_class_path.empty?
 
template "module.rb", File.join("app/models", class_path, "#{class_path.join('/')}.rb") if behavior == :invoke
end
de here...


Some more background and argument:

when you do something like:
rails g model admin/dashboard
rails g model user/dashboard user/profile

You'll get nicely namespaced AR models and tables. The AR models are placed into a sub-directory within the models directory and a module is placed into the root model directory. The module only contains the table_name_prefix. So why not put that module into the sub-directory with the model(s)?

Thanks for your time!

Matt Jones

unread,
Oct 19, 2017, 12:49:23 PM10/19/17
to rubyonra...@googlegroups.com
For concreteness, are you suggesting that the first command there generate the file at `app/models/admin/admin.rb`? AFAIK that’s not going to work with the existing autoloader - it won’t look there by default, and it would expect a file located there to define a class or module named `Admin::Admin` if it did.

—Matt Jones

Matt Wire

unread,
Oct 20, 2017, 10:44:57 AM10/20/17
to Ruby on Rails: Core
You are right about the autoloader. I have been putting those files in `app/modules` and wasn't thinking about the autoloader when I changed my suggestion to suggest using the subdirectory inside `app/models`. I made that change before posting this new topic because I felt that suggesting a whole new dir wouldn't be well received.

This is only a problem when you're dealing with large monoliths, and because that is a result of poor app design, this suggestion is probably not warranted after all.

Reply all
Reply to author
Forward
0 new messages