Replies: 4 comments 9 replies
-
I don't see how it could be done sensibly, because Mojolicious does not prescribe any relevance to the naming (or even existence) of such classes - you don't direct the router to them, they have no built in relationship to helpers, etc. On another note, you can add such a namespace to the new https://metacpan.org/pod/Mojolicious#preload_namespaces attribute, but this only preloads it so that its memory is shared by prefork workers, you still need to require modules when using them. |
Beta Was this translation helpful? Give feedback.
-
I wrote a module that might be interesting for you. It was me searching for a solution to a similar problem you were having. I'm not convinced that I nailed it but it does the job in the few places I've actually wanted it. https://metacpan.org/pod/Mojo::TypeModel Note that the examples refer to an application called CarPark which is only on my github |
Beta Was this translation helpful? Give feedback.
-
There is a https://metacpan.org/pod/Mojolicious::Plugin::Model that might suit your needs |
Beta Was this translation helpful? Give feedback.
-
At least for Minion better task class and class loading features are very possible. I just don't know yet how to do it. |
Beta Was this translation helpful? Give feedback.
-
As discussed in Mojo IRC, personally I feel like application class (i.e. file lib/MyApp.pm) gets a little verbose when you have several Models and or several Tasks in your project.
So TL;DR, is it worth to add a feature for treating Model and Task namespaces as Controller namespaces (in the sense that they not need to be specifically loaded by the application)?
As an example if I have a project with 3 models and 2 tasks, the lib directory will probably look like:
And inside MyApp.pm, I will probably have something like this: (this is following what I understand are best practices following Mojolicious documentation and examples)
(The Model:: modules may actually need or not to have an app attribute
(has 'app')
, this is just an example)If you think that this is too verbose (like I do), a possible refactoring of it is:
Instead of the "use" and helper loads lines for every Model:: module and the dedicated plugin line for tasks.
But the real question is that if it is worth to add a feature to preload code for these two namespaces that could be included in core. That would mean that you have to refactor (or start writing) your Models as Plugins, and the "use" statements and the helper loading code will not be needed.
Beta Was this translation helpful? Give feedback.
All reactions