Repository: phoenixframework/phoenix
Pull request #5108 (2022-11-14 02:37:15)

Solves array issues for phx generator. #5058
Adds values for an array of strings and integers, which solve issue #5058


Patch:
diff --git a/lib/mix/phoenix/schema.ex b/lib/mix/phoenix/schema.ex
index 4777a31bf..17bf41da0 100644
--- a/lib/mix/phoenix/schema.ex
+++ b/lib/mix/phoenix/schema.ex
@@ -279,7 +279,7 @@ defmodule Mix.Phoenix.Schema do
 
   defp type_to_default(key, t, :create) do
     case t do
-        {:array, _}     -> []
+        {:array, type} -> build_array_values(type, :create)
         {:enum, values} -> build_enum_values(values, :create)
         :integer        -> 42
         :float          -> 120.5
@@ -300,7 +300,7 @@ defmodule Mix.Phoenix.Schema do
   end
   defp type_to_default(key, t, :update) do
     case t do
-        {:array, _}     -> []
+        {:array, type}  -> build_array_values(type, :update)
         {:enum, values} -> build_enum_values(values, :update)
         :integer        -> 43
         :float          -> 456.7
@@ -320,6 +320,17 @@ defmodule Mix.Phoenix.Schema do
     end
   end
 
+  defp build_array_values(:string, :create),
+    do: Enum.map([1,2], &("option#{&1}"))
+  defp build_array_values(:integer, :create),
+    do: [1,2]
+  defp build_array_values(:string, :update),
+    do: ["option1"]
+  defp build_array_values(:integer, :update),
+    do: [1]
+  defp build_array_values(_, _),
+    do: []
+
   defp build_enum_values(values, action) do
     case {action, translate_enum_vals(values)} do
       {:create, vals} -> hd(vals)
diff --git a/lib/mix/tasks/phx.gen.html.ex b/lib/mix/tasks/phx.gen.html.ex
index f2af40499..bd2d598e0 100644
--- a/lib/mix/tasks/phx.gen.html.ex
+++ b/lib/mix/tasks/phx.gen.html.ex
@@ -201,17 +201,14 @@ defmodule Mix.Tasks.Phx.Gen.Html do
       {key, :naive_datetime} ->
         ~s(<.input field={{f, #{inspect(key)}}} type="datetime-local" label="#{label(key)}" />)
 
-      {key, {:array, :integer}} ->
-        ~s(<.input field={{f, #{inspect(key)}}} type="datetime-local" label="#{label(key)}" />)
-
-      {key, {:array, _}} ->
+      {key, {:array, _} = type} ->
         ~s"""
         <.input
           field={{f, #{inspect(key)}}}
           type="select"
           multiple
           label="#{label(key)}"
-          options={[{"Option 1", "option1"}, {"Option 2", "option2"}]}
+          options={#{inspect(default_options(type))}}
         />
         """
 
@@ -231,6 +228,14 @@ defmodule Mix.Tasks.Phx.Gen.Html do
     end)
   end
 
+  defp default_options({:array, :string}),
+    do: Enum.map([1,2], &({"Option #{&1}", "option#{&1}"}))
+
+  defp default_options({:array, :integer}),
+    do: Enum.map([1,2], &({"#{&1}", &1}))
+
+  defp default_options({:array, _}), do: []
+
   defp label(key), do: Phoenix.Naming.humanize(to_string(key))
 
   @doc false


Test patch:
diff --git a/test/mix/phoenix_test.exs b/test/mix/phoenix_test.exs
index 2fde37a23..fce3de3f6 100644
--- a/test/mix/phoenix_test.exs
+++ b/test/mix/phoenix_test.exs
@@ -18,6 +18,7 @@ defmodule Mix.PhoenixTest do
   test "attrs/1 defaults each type" do
     attrs = [
       "logins:array:string",
+      "lottery_numbers:array:integer",
       "age:integer",
       "temp:float",
       "temp_2:decimal",
@@ -32,6 +33,7 @@ defmodule Mix.PhoenixTest do
 
     assert Mix.Phoenix.Schema.attrs(attrs) == [
              logins: {:array, :string},
+             lottery_numbers: {:array, :integer},
              age: :integer,
              temp: :float,
              temp_2: :decimal,
@@ -71,7 +73,7 @@ defmodule Mix.PhoenixTest do
     ]
 
     assert Mix.Phoenix.Schema.params(params) == %{
-             logins: [],
+             logins: ["option1", "option2"],
              age: 42,
              temp: 120.5,
              temp_2: "120.5",
@@ -110,6 +112,9 @@ defmodule Mix.PhoenixTest do
     assert Mix.Phoenix.Schema.live_form_value(~U[2020-10-09T14:00:00Z]) ==
              "2020-10-09T14:00:00Z"
 
+    assert Mix.Phoenix.Schema.live_form_value([1]) == [1]
+    assert Mix.Phoenix.Schema.live_form_value(["option1"]) == ["option1"]
+
     assert Mix.Phoenix.Schema.live_form_value(:value) == :value
   end