Skip to content

Commit

Permalink
Use return_to on form back link
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumm committed Aug 15, 2024
1 parent 6c9365d commit 3a88f10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 5 additions & 5 deletions priv/templates/phx.gen.live/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
</:actions>
</.simple_form>
<.back navigate={~p"<%= schema.route_prefix %>"}>Back to <%= schema.plural %></.back>
<.back navigate={return_path(@return_to.key, @<%= schema.singular %>)}>Back to <%%= @return_to.name %></.back>
</div>
"""
end
Expand All @@ -36,8 +36,8 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp return_to("show"), do: "show"
defp return_to(_), do: "index"
defp return_to("show"), do: %{key: "show", name: "<%= schema.singular %>"}
defp return_to(_), do: %{key: "index", name: "<%= schema.plural %>"}

defp apply_action(socket, :edit, %{"id" => id}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
Expand Down Expand Up @@ -73,7 +73,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
{:noreply,
socket
|> put_flash(:info, "<%= schema.human_singular %> updated successfully")
|> push_navigate(to: return_path(socket.assigns.return_to, <%= schema.singular %>))}
|> push_navigate(to: return_path(socket.assigns.return_to.key, <%= schema.singular %>))}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
Expand All @@ -86,7 +86,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
{:noreply,
socket
|> put_flash(:info, "<%= schema.human_singular %> created successfully")
|> push_navigate(to: return_path(socket.assigns.return_to, <%= schema.singular %>))}
|> push_navigate(to: return_path(socket.assigns.return_to.key, <%= schema.singular %>))}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
Expand Down
12 changes: 9 additions & 3 deletions priv/templates/phx.gen.live/live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> render_click()
|> follow_redirect(conn, ~p"<%= schema.route_prefix %>/new")

assert render(form_live) =~ "New <%= schema.human_singular %>"
html = render(form_live)
assert html =~ "New <%= schema.human_singular %>"
assert html =~ "Back to <%= schema.plural %>"

assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand All @@ -58,7 +60,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> render_click()
|> follow_redirect(conn, ~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}/edit")
assert render(form_live) =~ "Edit <%= schema.human_singular %>"
html = render(form_live)
assert html =~ "Edit <%= schema.human_singular %>"
assert html =~ "Back to <%= schema.plural %>"

assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand Down Expand Up @@ -102,7 +106,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> render_click()
|> follow_redirect(conn, ~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}/edit?return_to=show")
assert render(form_live) =~ "Edit <%= schema.human_singular %>"
html = render(form_live)
assert html =~ "Edit <%= schema.human_singular %>"
assert html =~ "Back to <%= schema.singular %>"

assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand Down

0 comments on commit 3a88f10

Please sign in to comment.