Skip to content

Commit 965e5be

Browse files
authored
fix: support old phoenix generators (#365)
* Move new templates to new folder, return old templates * Generalize writing templates and switch by phx version * Return old test for generating live templates * Remove dbg call in template * Add phx_version option and based on that switch * Test both templating with/without phx_version option
1 parent e7b3f9f commit 965e5be

File tree

9 files changed

+653
-57
lines changed

9 files changed

+653
-57
lines changed

lib/ash_phoenix/gen/live.ex

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ if Code.ensure_loaded?(Igniter) do
1010
Keyword.fetch!(options, :resource_plural) ||
1111
resource |> Module.split() |> List.last() |> Macro.underscore() |> Inflex.pluralize()
1212

13-
opts = []
13+
opts = [
14+
interactive?: true,
15+
resource_plural: resource_plural,
16+
phx_version: options[:phx_version]
17+
]
1418

15-
generate(
16-
igniter,
17-
domain,
18-
resource,
19-
Keyword.put(opts, :interactive?, true) |> Keyword.put(:resource_plural, resource_plural)
20-
)
19+
generate(igniter, domain, resource, opts)
2120
end
2221

2322
def generate(igniter, domain, resource, opts \\ []) do
@@ -80,38 +79,7 @@ if Code.ensure_loaded?(Igniter) do
8079
end
8180

8281
igniter =
83-
write_formatted_template(
84-
igniter,
85-
"ash_phoenix.gen.live/index.ex.eex",
86-
"index.ex",
87-
web_live,
88-
assigns,
89-
generate_opts
90-
)
91-
92-
igniter =
93-
if assigns[:update_action] || assigns[:create_action] do
94-
write_formatted_template(
95-
igniter,
96-
"ash_phoenix.gen.live/form.ex.eex",
97-
"form.ex",
98-
web_live,
99-
assigns,
100-
generate_opts
101-
)
102-
else
103-
igniter
104-
end
105-
106-
igniter =
107-
write_formatted_template(
108-
igniter,
109-
"ash_phoenix.gen.live/show.ex.eex",
110-
"show.ex",
111-
web_live,
112-
assigns,
113-
generate_opts
114-
)
82+
write_formatted_templates(igniter, web_live, assigns, generate_opts, opts[:phx_version])
11583

11684
igniter =
11785
if opts[:interactive?] do
@@ -144,21 +112,37 @@ if Code.ensure_loaded?(Igniter) do
144112
|> Enum.reject(&is_nil/1)
145113
end
146114

147-
defp write_formatted_template(igniter, path, destination, web_live, assigns, generate_opts) do
148-
destination_path =
149-
web_live
150-
|> Path.join(destination)
115+
defp write_formatted_templates(igniter, web_live, assigns, generate_opts, phx_version) do
116+
{path, igniter} =
117+
if String.starts_with?(phx_version, "1.8") do
118+
message = """
119+
if Layouts.app causes a problem, you may be on an older version of the generators,
120+
try deleting the files and running the command again with --phx-version 1.7
121+
"""
122+
123+
{"new", Igniter.add_notice(igniter, message)}
124+
else
125+
{"old", igniter}
126+
end
127+
128+
template_folder = template_folder(path)
129+
action? = assigns[:update_action] || assigns[:create_action]
130+
131+
Enum.reduce(File.ls!(template_folder), igniter, fn
132+
"form.ex.eex", igniter when is_nil(action?) ->
133+
igniter
151134

152-
{formatter_function, _options} =
153-
Mix.Tasks.Format.formatter_for_file(destination_path)
135+
file, igniter ->
136+
destination = String.replace_trailing(file, ".eex", "")
137+
destination_path = Path.join(web_live, destination)
154138

155-
contents =
156-
path
157-
|> template()
158-
|> EEx.eval_file(assigns: assigns)
159-
|> formatter_function.()
139+
{formatter_function, _options} =
140+
Mix.Tasks.Format.formatter_for_file(destination_path)
160141

161-
Igniter.create_new_file(igniter, destination_path, contents, generate_opts)
142+
path = Path.join(template_folder, file)
143+
contents = path |> EEx.eval_file(assigns: assigns) |> formatter_function.()
144+
Igniter.create_new_file(igniter, destination_path, contents, generate_opts)
145+
end)
162146
end
163147

164148
defp add_resource_assigns(assigns, resource, opts) do
@@ -341,8 +325,10 @@ if Code.ensure_loaded?(Igniter) do
341325
Igniter.Libs.Phoenix.web_module(igniter)
342326
end
343327

344-
defp template(path) do
345-
:code.priv_dir(:ash_phoenix) |> Path.join("templates") |> Path.join(path)
328+
defp template_folder(path) do
329+
:code.priv_dir(:ash_phoenix)
330+
|> Path.join("templates/ash_phoenix.gen.live")
331+
|> Path.join(path)
346332
end
347333

348334
def inputs(resource, action) do

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if Code.ensure_loaded?(Igniter) do
2222
* `--domain` - Existing domain
2323
* `--resource` - Existing resource module name
2424
* `--resource-plural` - Pluralized version resource name for the route paths and templates
25+
* `--phx-version` - Phoenix version 1.7 (old) or 1.8 (new). Defaults to 1.8
2526
"""
2627

2728
def info(_argv, _composing_task) do
@@ -34,10 +35,11 @@ if Code.ensure_loaded?(Igniter) do
3435
domain: :string,
3536
resource: :string,
3637
resourceplural: :string,
37-
resource_plural: :string
38+
resource_plural: :string,
39+
phx_version: :string
3840
],
3941
# Default values for the options in the `schema`.
40-
defaults: [],
42+
defaults: [phx_version: "1.8"],
4143
# CLI aliases
4244
aliases: [],
4345
# A list of options in the schema that are required

priv/templates/ash_phoenix.gen.live/form.ex.eex renamed to priv/templates/ash_phoenix.gen.live/new/form.ex.eex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ defmodule <%= inspect Module.concat(@web_module, @resource_alias) %>Live.Form do
5151

5252
action = if is_nil(<%= @resource_singular %>), do: "New", else: "Edit"
5353
page_title = action <> " " <> <%= inspect @resource_human_singular %>
54-
dbg(params)
5554
{:ok,
5655
socket
5756
|> assign(:return_to, return_to(params["return_to"]))
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
defmodule <%= inspect Module.concat(@web_module, @resource_alias) %>Live.FormComponent do
2+
3+
use <%= @web_module %>, :live_component
4+
5+
@impl true
6+
def render(assigns) do
7+
~H"""
8+
<div>
9+
<.header>
10+
<%%= @title %>
11+
<:subtitle>Use this form to manage <%= @resource_singular %> records in your database.</:subtitle>
12+
</.header>
13+
14+
<.simple_form
15+
for={@form}
16+
id="<%= @resource_singular %>-form"
17+
phx-target={@myself}
18+
phx-change="validate"
19+
phx-submit="save"
20+
>
21+
<%= cond do %>
22+
<% @create_action && @update_action -> %>
23+
<%= if @create_inputs == @update_inputs do %>
24+
<%= @create_inputs %>
25+
<% else %>
26+
<%%= if @form.source.type == :create do %>
27+
<%= @create_inputs %>
28+
<%% end %>
29+
<%%= if @form.source.type == :update do %>
30+
<%= @update_inputs %>
31+
<%% end %>
32+
<% end %>
33+
<% @create_action -> %>
34+
<%= @create_inputs %>
35+
<% @update_action -> %>
36+
<%= @update_inputs %>
37+
<% end %>
38+
<:actions>
39+
<.button phx-disable-with="Saving...">Save <%= @resource_human_singular %></.button>
40+
</:actions>
41+
</.simple_form>
42+
</div>
43+
"""
44+
end
45+
46+
@impl true
47+
def update(assigns, socket) do
48+
{:ok,
49+
socket
50+
|> assign(assigns)
51+
|> assign_form()}
52+
end
53+
54+
@impl true
55+
def handle_event("validate", %{"<%= @resource_singular %>" => <%= @resource_singular %>_params}, socket) do
56+
{:noreply, assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, <%= @resource_singular %>_params))}
57+
end
58+
59+
def handle_event("save", %{"<%= @resource_singular %>" => <%= @resource_singular %>_params}, socket) do
60+
case AshPhoenix.Form.submit(socket.assigns.form, params: <%= @resource_singular %>_params) do
61+
{:ok, <%= @resource_singular %>} ->
62+
notify_parent({:saved, <%= @resource_singular %>})
63+
64+
socket =
65+
socket
66+
<%= cond do %>
67+
<% @update_action && @create_action -> %>
68+
|> put_flash(:info, "<%= @resource_human_singular %> #{socket.assigns.form.source.type}d successfully")
69+
<% @update_action -> %>
70+
|> put_flash(:info, "<%= @resource_human_singular %> updated successfully")
71+
<% @create_action -> %>
72+
|> put_flash(:info, "<%= @resource_human_singular %> created successfully")
73+
<% end %>
74+
|> push_patch(to: socket.assigns.patch)
75+
76+
{:noreply, socket}
77+
78+
{:error, form} ->
79+
{:noreply, assign(socket, form: form)}
80+
end
81+
end
82+
83+
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
84+
85+
defp assign_form(%{assigns: %{<%= @resource_singular %>: <%= @resource_singular %>}} = socket) do
86+
form =
87+
<%= cond do %>
88+
<% @update_action && @create_action -> %>
89+
if <%= @resource_singular %> do
90+
AshPhoenix.Form.for_update(<%= @resource_singular %>, <%= inspect @update_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>)
91+
else
92+
AshPhoenix.Form.for_create(<%= @resource %>, <%= inspect @create_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>)
93+
end
94+
<% @update_action -> %>
95+
AshPhoenix.Form.for_update(<%= @resource_singular %>, <%= inspect @update_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>)
96+
<% @create_action -> %>
97+
AshPhoenix.Form.for_create(<%= @resource_singular %>, <%= inspect @create_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>)
98+
<% end %>
99+
100+
assign(socket, form: to_form(form))
101+
end
102+
end

0 commit comments

Comments
 (0)