Change where module file goes for namespaced AR models

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Change where module file goes for namespaced AR models

Matt Wire
Hello all!

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

https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/model/model_generator.rb (starting at LOC 29)

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!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Change where module file goes for namespaced AR models

Matt Jones-15

On Oct 18, 2017, at 3:56 PM, Matt Wire <[hidden email]> wrote:

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)?

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Change where module file goes for namespaced AR models

Matt Wire
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.