Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
- Simplify form template
- Apply action on mount since we don't live navigate within the form live view
- Simplify return_to/back button
  • Loading branch information
mcrumm committed Aug 15, 2024
1 parent 3a88f10 commit e52c465
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
49 changes: 22 additions & 27 deletions priv/templates/phx.gen.live/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,32 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%%= @page_title %>
<:subtitle>Use this form to manage <%= schema.singular %> records in your database.</:subtitle>
</.header>
<.simple_form for={@form} id="<%= schema.singular %>-form" phx-change="validate" phx-submit="save">
<%= Mix.Tasks.Phx.Gen.Html.indent_inputs(inputs, 8) %>
<:actions>
<.button phx-disable-with="Saving...">Save <%= schema.human_singular %></.button>
</:actions>
</.simple_form>
<.back navigate={return_path(@return_to.key, @<%= schema.singular %>)}>Back to <%%= @return_to.name %></.back>
</div>
<.header>
<%%= @page_title %>
<:subtitle>Use this form to manage <%= schema.singular %> records in your database.</:subtitle>
</.header>
<.simple_form for={@form} id="<%= schema.singular %>-form" phx-change="validate" phx-submit="save">
<%= Mix.Tasks.Phx.Gen.Html.indent_inputs(inputs, 6) %>
<:actions>
<.button phx-disable-with="Saving...">Save <%= schema.human_singular %></.button>
</:actions>
</.simple_form>
<.back navigate={return_path(@return_to, @<%= schema.singular %>)}>Back</.back>
"""
end

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
def mount(params, _session, socket) do
{:ok,
socket
|> assign(:return_to, return_to(params["return_to"]))
|> apply_action(socket.assigns.live_action, params)}
end

@impl true
def handle_params(params, _url, socket) do
socket = assign(socket, :return_to, return_to(params["return_to"]))
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

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

defp apply_action(socket, :edit, %{"id" => id}) do
<%= schema.singular %> = <%= inspect context.alias %>.get_<%= schema.singular %>!(id)
Expand Down Expand Up @@ -73,7 +68,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.key, <%= schema.singular %>))}
|> push_navigate(to: return_path(socket.assigns.return_to, <%= schema.singular %>))}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
Expand All @@ -86,7 +81,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.key, <%= schema.singular %>))}
|> push_navigate(to: return_path(socket.assigns.return_to, <%= schema.singular %>))}

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

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

assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand All @@ -60,9 +58,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
|> render_click()
|> follow_redirect(conn, ~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}/edit")
html = render(form_live)
assert html =~ "Edit <%= schema.human_singular %>"
assert html =~ "Back to <%= schema.plural %>"
assert render(form_live) =~ "Edit <%= schema.human_singular %>"
assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand Down Expand Up @@ -106,9 +102,7 @@ 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")
html = render(form_live)
assert html =~ "Edit <%= schema.human_singular %>"
assert html =~ "Back to <%= schema.singular %>"
assert render(form_live) =~ "Edit <%= schema.human_singular %>"
assert form_live
|> form("#<%= schema.singular %>-form", <%= schema.singular %>: @invalid_attrs)
Expand Down
14 changes: 7 additions & 7 deletions test/mix/tasks/phx.gen.live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert file =~ ~s(<.input field={@form[:votes]} type="number")
assert file =~ ~s(<.input field={@form[:cost]} type="number" label="Cost" step="any")
assert file =~ """
<.input
field={@form[:tags]}
type="select"
multiple
<.input
field={@form[:tags]}
type="select"
multiple
"""
assert file =~ ~s(<.input field={@form[:popular]} type="checkbox")
assert file =~ ~s(<.input field={@form[:drafted_at]} type="datetime-local")
Expand All @@ -119,9 +119,9 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert file =~ ~s(<.input field={@form[:secret]} type="text" label="Secret" />)
refute file =~ ~s(<field={@form[:metadata]})
assert file =~ """
<.input
field={@form[:status]}
type="select"
<.input
field={@form[:status]}
type="select"
"""
assert file =~ ~s|Ecto.Enum.values(Phoenix.Blog.Post, :status)|

Expand Down

0 comments on commit e52c465

Please sign in to comment.