Skip to content

Commit 9064c5b

Browse files
committed
improvement: add --resource-plural-for-routes to gen tasks
closes #393
1 parent c511b60 commit 9064c5b

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lib/ash_phoenix/gen/gen.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defmodule AshPhoenix.Gen do
1515
## Options
1616
1717
- `--resource-plural` - The plural resource name (e.g. "products")
18+
- `--resource-plural-for-routes` - Override the plural name used in route paths (e.g. "random-things"). Useful when the route prefix contains dashes.
1819
"""
1920
end
2021

lib/ash_phoenix/gen/live.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if Code.ensure_loaded?(Igniter) do
2121
opts = [
2222
interactive?: true,
2323
resource_plural: resource_plural,
24+
resource_plural_for_routes: options[:resource_plural_for_routes],
2425
phx_version: options[:phx_version]
2526
]
2627

@@ -105,18 +106,19 @@ if Code.ensure_loaded?(Igniter) do
105106

106107
defp live_route_instructions(assigns, phx_version) do
107108
module = if String.starts_with?(phx_version, "1.8"), do: "Form", else: "Index"
109+
prefix = assigns[:route_prefix]
108110

109111
[
110-
~s|live "/#{assigns[:resource_plural]}", #{assigns[:resource_alias]}Live.Index, :index\n|,
112+
~s|live "#{prefix}", #{assigns[:resource_alias]}Live.Index, :index\n|,
111113
if assigns[:create_action] do
112-
~s|live "/#{assigns[:resource_plural]}/new", #{assigns[:resource_alias]}Live.#{module}, :new\n|
114+
~s|live "#{prefix}/new", #{assigns[:resource_alias]}Live.#{module}, :new\n|
113115
end,
114116
if assigns[:update_action] do
115-
~s|live "/#{assigns[:resource_plural]}/:id/edit", #{assigns[:resource_alias]}Live.#{module}, :edit\n\n|
117+
~s|live "#{prefix}/:id/edit", #{assigns[:resource_alias]}Live.#{module}, :edit\n\n|
116118
end,
117-
~s|live "/#{assigns[:resource_plural]}/:id", #{assigns[:resource_alias]}Live.Show, :show\n|,
119+
~s|live "#{prefix}/:id", #{assigns[:resource_alias]}Live.Show, :show\n|,
118120
if assigns[:update_action] do
119-
~s|live "/#{assigns[:resource_plural]}/:id/show/edit", #{assigns[:resource_alias]}Live.Show, :edit|
121+
~s|live "#{prefix}/:id/show/edit", #{assigns[:resource_alias]}Live.Show, :edit|
120122
end
121123
]
122124
|> Enum.reject(&is_nil/1)
@@ -180,6 +182,8 @@ if Code.ensure_loaded?(Igniter) do
180182
create_action = action(resource, opts, :create)
181183
update_action = action(resource, opts, :update)
182184

185+
route_prefix = "/#{opts[:resource_plural_for_routes] || plural_name}"
186+
183187
Keyword.merge(assigns,
184188
resource_singular: short_name,
185189
resource_alias: Macro.camelize(short_name),
@@ -194,7 +198,7 @@ if Code.ensure_loaded?(Igniter) do
194198
pkey: pkey,
195199
get_by_pkey: get_by_pkey,
196200
attrs: attrs(resource),
197-
route_prefix: "/#{plural_name}"
201+
route_prefix: route_prefix
198202
)
199203
end
200204

lib/mix/tasks/ash_phoenix.gen.live.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if Code.ensure_loaded?(Igniter) do
2626
* `--domain` - Existing domain
2727
* `--resource` - Existing resource module name
2828
* `--resource-plural` - Pluralized version resource name for the route paths and templates
29+
* `--resource-plural-for-routes` - Override the plural name used in route paths (e.g. "random-things"). Useful when the route prefix contains dashes.
2930
* `--phx-version` - Phoenix version 1.7 (old) or 1.8 (new). Defaults to 1.8
3031
"""
3132

@@ -40,6 +41,7 @@ if Code.ensure_loaded?(Igniter) do
4041
resource: :string,
4142
resourceplural: :string,
4243
resource_plural: :string,
44+
resource_plural_for_routes: :string,
4345
phx_version: :string
4446
],
4547
# Default values for the options in the `schema`.

0 commit comments

Comments
 (0)