How to deal with phx.gen.*, when I don't need Models/Schema, in a --no-ecto webapp?

Question re. phx.gen.*

From the phx docs: The first argument is the context module followed by the schema module and its plural name (used as the schema table name). How do you deal with the spec (requirement?) above, when you don’t want/need to use a schema, nor having the need for models (--no-schema, --no-model).

Can’t get my head about the “overhead” introduced by this: ...followed by the schema module and its plural name, in the scenario described above.

Here’s what I do, in this case:

$ mix phx.gen.html NoNeed Media uploads --no-model --no-schema --no-context

But it feels unnecessary.

How are you guys using this generator (and others) in schema/model-less situations? Editing the files to remove the references to Ecto & Co, git clean .., etc?

The advice I am receiving most often, is: don’t use the generators.

Thoughts?

1 Like

I give this advice 2 thumbs up.

I never even really got into generators in rails. They are great for demos and live coding, but that’s not what my employer pays me for. I can understand if you were an independent contractor who somehow got a lot of greenfield apps how they’d be professionally useful, but otherwise, I don’t understand the level of reliance some people have on them.

1 Like

I would add just some words to their advices: don’t use the generators, when it doesn’t makes sense to use them.

Well yeah, while generators are a good way to learn Phoenix, I guess, once you learned they tend to be more and more useless.

An example is the project I’m working now: on the beginning of the project, generators were really helpful. But as the app begins growing, with some feature like multi-tenancy, authorization, and some complex screens which did not have any to do with a simple CRUD, like dashboards, generators began to be a pain in the butt.

1 Like

Greg - I agree with what you said. But there is obvious effort put in by the Phoenix team, to provide these generators, hence my curiosity and will to understand the rationales behind the need to specify a schema when you don’t need it. Still have the feeling I am missing something obvious in the generators’ design. And no, I am not using the generators to write Phoenix apps, just curious about their design and what others are thinking about these. So thank you for feedback. BTW, I honestly don’t think an employer would judge the fact you used or not a generator, if he/she trusts in your work; this should be your decision.

I think there’s a lot of checkmarks needed in any modern framework when you have code that is somewhat boilerplate or you’re trying to provide an easy set of tooling for newcomers and experienced users alike. Generators fall into this category, much the same way something like create-react-app does in the React community, and yeoman before that. (Do people still use that? I kind of leapfrogged over that time in the chronicles of JS history…) Its just kind of expected that they’re going to be present. I can’t really speak for the phoenix core team, but I suspect that they were an early development in the phoenix codebase and momentum has carried them through to today, which isn’t to downplay the significance of having them.

But like all such things, you get to a point where they are more of an impedance to your desired coding style than an asset. If your instincts lead you to organize your code that way, use them. If they’re leading you a different way, don’t use them. It really is that simple. Elixir doesn’t really give a damn either way—its going to compile your code regardless. (Or, if you’re like me, its going to find a variety of reasons NOT to compile your code!) You’re not missing out on a whole bunch of wondrous enlightenment by forgoing their use. Like Greg, I stopped using Rails generators way longer ago than I care to admit, and never really started using them in Phoenix. So I’m a bit mystified at the amount of attention such a little detail gets and how such strong opinions about their impact get formed.

1 Like

You’re able to add default args for the generators in your config file:

config :your_app, :generators, 
    context: false,
    schema: false,
    migration: false

See: https://github.com/phoenixframework/phoenix/blob/v1.3.0/lib/mix/tasks/phx.gen.html.ex#L79

I’m not 100% that the options I used in my example are correct, I’m extrapolating from the docs. It shows the option as migration: false and the corresponding flag --no-migration, so maybe schema and context are the same.

More generally, something to consider is making your own generators that are tailored to your specification. I think it is just a matter of cp deps/phoenix/lib/mix/tasks/phx.gen.html.ex lib/your_app/mix/tasks/your_app.gen.html.ex and then making the adjustments. Though keep in mind that the existing generators may rely on private apis that could change, as per this comment by Jose: Customizing `mix phoenix.gen.*` tasks

3 Likes

The generators are aimed at the majority cases. You not having a schema are in the minority. Just yesterday I watched Chris McCord’s talk at ElixirConfEU earlier this year, and he explained the overall rationale pretty well.

Oh, i didn’t mean it that way. Most of my paid work is modifying existing code to add new features. I so rarely need everything the generators supply, I forget the syntax to leave out parts. Instead I just create a new file (for example) in my controllers directory and start coding.

3 Likes