ash_phoenix icon indicating copy to clipboard operation
ash_phoenix copied to clipboard

allow `mix ash_phoenix.gen.html` to generate templates with `href` values pointing to resource prefixes containing dashes

Open j-n-f opened this issue 5 months ago • 1 comments

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

AI Policy

  • [x] I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

Is your feature request related to a problem? Please describe.

I'm trying to generate a resource route set with a dash in it like <app-root>/random-thing/...

When I generate a controller + templates:

mix ash_phoenix.gen.html MyApp.Domain MyApp.Domain.RandomThing --resource-plural random_things

I will get valid code (AFAIK elixir identifiers can't have dashes, so underscore is the way to go here). The notice after the command runs describes how to add resource routing. The problem is that if I set up routes with:

# in /lib/praxis_web/router.ex
scope "/", PraxisWeb do
  pipe_through :browser

  resources "/random-things", RandomThingController
end

then the routing table won't match the generated templates.

Describe the solution you'd like

The ash_phoenix.gen.html task already has an argument to help create an identifier in controller code:

https://github.com/ash-project/ash_phoenix/blob/85fd46566e4663d8474268e8b1e93bb2f70b1b19/lib/ash_phoenix/gen/gen.ex#L13

If an optional parameter was added like --resource-plural-for-template (when present) it could override the href values in generated templates. Continuing with the example:

mix ash_phoenix.gen.html MyApp.Domain MyApp.Domain.RandomThing \
    --resource-plural random_things \
    --resource-plural-for-template random-things

Then when the dashed route prefix is added to the router there's no conflict, and the templates don't have to be updated by hand.

Describe alternatives you've considered

The obvious alternative is to accept the defaults but it should be possible to support both use cases. You might also imagine cases where (for some reason) a developer doesn't want the route prefix to match the resource name.

Maybe there would be a way to pass some argument to the resources declaration in the router so it would pass a prefix into the template which would get substituted into the href? I don't have enough expertise to say how viable that would be. I can see that the resources declaration (which is part of Phoenix) has some parameters like alias and as, but this mix task is part of ash_phoenix so it might take some co-ordination to make something like that work.

Additional context

No response

j-n-f avatar Jul 20 '25 05:07 j-n-f

I'd call it something like --resource-plural-for-routes. PR welcome!

zachdaniel avatar Jul 21 '25 01:07 zachdaniel