Skip to content

Commit

Permalink
Colocate live templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumm committed Aug 15, 2024
1 parent e52c465 commit e21c025
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 59 deletions.
2 changes: 0 additions & 2 deletions lib/mix/tasks/phx.gen.live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ defmodule Mix.Tasks.Phx.Gen.Live do
{:eex, "show.ex", Path.join(web_live, "show.ex")},
{:eex, "index.ex", Path.join(web_live, "index.ex")},
{:eex, "form.ex", Path.join(web_live, "form.ex")},
{:eex, "index.html.heex", Path.join(web_live, "index.html.heex")},
{:eex, "show.html.heex", Path.join(web_live, "show.html.heex")},
{:eex, "live_test.exs", Path.join(test_live, "#{schema.singular}_live_test.exs")},
{:new_eex, "core_components.ex",
Path.join([web_prefix, "components", "core_components.ex"])}
Expand Down
36 changes: 36 additions & 0 deletions priv/templates/phx.gen.live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web

alias <%= inspect context.module %>

@impl true
def render(assigns) do
~H"""
<.header>
Listing <%= schema.human_plural %>
<:actions>
<.link navigate={~p"<%= schema.route_prefix %>/new"}>
<.button>New <%= schema.human_singular %></.button>
</.link>
</:actions>
</.header>
<.table
id="<%= schema.plural %>"
rows={@streams.<%= schema.collection %>}
row_click={fn {_id, <%= schema.singular %>} -> JS.navigate(~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}") end}
><%= for {k, _} <- schema.attrs do %>
<:col :let={{_id, <%= schema.singular %>}} label="<%= Phoenix.Naming.humanize(Atom.to_string(k)) %>"><%%= <%= schema.singular %>.<%= k %> %></:col><% end %>
<:action :let={{_id, <%= schema.singular %>}}>
<div class="sr-only">
<.link navigate={~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}"}>Show</.link>
</div>
<.link navigate={~p"<%= schema.route_prefix %>/#{<%= schema.singular %>}/edit"}>Edit</.link>
</:action>
<:action :let={{id, <%= schema.singular %>}}>
<.link
phx-click={JS.push("delete", value: %{id: <%= schema.singular %>.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
"""
end

@impl true
def mount(_params, _session, socket) do
{:ok,
Expand Down
30 changes: 0 additions & 30 deletions priv/templates/phx.gen.live/index.html.heex

This file was deleted.

21 changes: 21 additions & 0 deletions priv/templates/phx.gen.live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web

alias <%= inspect context.module %>

@impl true
def render(assigns) do
~H"""
<.header>
<%= schema.human_singular %> <%%= @<%= schema.singular %>.id %>
<:subtitle>This is a <%= schema.singular %> record from your database.</:subtitle>
<:actions>
<.link navigate={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}/edit?return_to=show"}>
<.button>Edit <%= schema.singular %></.button>
</.link>
</:actions>
</.header>
<.list><%= for {k, _} <- schema.attrs do %>
<:item title="<%= Phoenix.Naming.humanize(Atom.to_string(k)) %>"><%%= @<%= schema.singular %>.<%= k %> %></:item><% end %>
</.list>
<.back navigate={~p"<%= schema.route_prefix %>"}>Back to <%= schema.plural %></.back>
"""
end

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
Expand Down
15 changes: 0 additions & 15 deletions priv/templates/phx.gen.live/show.html.heex

This file was deleted.

17 changes: 5 additions & 12 deletions test/mix/tasks/phx.gen.live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert file =~ "create unique_index(:posts, [:slug])"
end

assert_file "lib/phoenix_web/live/post_live/index.html.heex", fn file ->
assert_file "lib/phoenix_web/live/post_live/index.ex", fn file ->
assert file =~ ~S|~p"/posts/#{post}"|
end

assert_file "lib/phoenix_web/live/post_live/show.html.heex", fn file ->
assert_file "lib/phoenix_web/live/post_live/show.ex", fn file ->
assert file =~ ~S|~p"/posts"|
end

Expand Down Expand Up @@ -236,13 +236,13 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert file =~ "add :title, :string"
end

assert_file "lib/phoenix_web/live/blog/post_live/index.html.heex", fn file ->
assert_file "lib/phoenix_web/live/blog/post_live/index.ex", fn file ->
assert file =~ ~S|~p"/blog/posts/#{post}/edit"|
assert file =~ ~S|~p"/blog/posts/new"|
assert file =~ ~S|~p"/blog/posts/#{post}"|
end

assert_file "lib/phoenix_web/live/blog/post_live/show.html.heex", fn file ->
assert_file "lib/phoenix_web/live/blog/post_live/show.ex", fn file ->
assert file =~ ~S|~p"/blog/posts"|
assert file =~ ~S|~p"/blog/posts/#{@post}/edit?return_to=show"|
end
Expand Down Expand Up @@ -282,8 +282,6 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert_file "lib/phoenix_web/live/post_live/show.ex"
assert_file "lib/phoenix_web/live/post_live/form.ex"

assert_file "lib/phoenix_web/live/post_live/index.html.heex"
assert_file "lib/phoenix_web/live/post_live/show.html.heex"
assert_file "test/phoenix_web/live/post_live_test.exs"
end
end
Expand All @@ -300,8 +298,6 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert_file "lib/phoenix_web/live/post_live/show.ex"
assert_file "lib/phoenix_web/live/post_live/form.ex"

assert_file "lib/phoenix_web/live/post_live/index.html.heex"
assert_file "lib/phoenix_web/live/post_live/show.html.heex"
assert_file "test/phoenix_web/live/post_live_test.exs"
end
end
Expand All @@ -320,8 +316,6 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert_file "lib/phoenix_web/live/comment_live/show.ex"
assert_file "lib/phoenix_web/live/comment_live/form.ex"

assert_file "lib/phoenix_web/live/comment_live/index.html.heex"
assert_file "lib/phoenix_web/live/comment_live/show.html.heex"
assert_file "test/phoenix_web/live/comment_live_test.exs"
end
end
Expand All @@ -340,11 +334,10 @@ defmodule Mix.Tasks.Phx.Gen.LiveTest do
assert_file "lib/phoenix_web/live/series_live/show.ex"
assert_file "lib/phoenix_web/live/series_live/form.ex"

assert_file "lib/phoenix_web/live/series_live/index.html.heex", fn file ->
assert_file "lib/phoenix_web/live/series_live/index.ex", fn file ->
assert file =~ "@streams.series_collection"
end

assert_file "lib/phoenix_web/live/series_live/show.html.heex"
assert_file "test/phoenix_web/live/series_live_test.exs"
end
end
Expand Down

0 comments on commit e21c025

Please sign in to comment.