From 4b87e16199950330270ee51db63216d9630367fa Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Fri, 7 Dec 2018 15:29:18 +0000 Subject: [PATCH 1/3] only use change data in form if it exists --- lib/views/custom_view.ex | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/views/custom_view.ex b/lib/views/custom_view.ex index 6fd386b..1487c4d 100644 --- a/lib/views/custom_view.ex +++ b/lib/views/custom_view.ex @@ -17,13 +17,18 @@ defmodule Autoform.CustomView do # Apply any sensible defaults here, but we should allow configuration of individual input options opts = - Keyword.merge( - opts, - [value: form.source.changes[field]] ++ - case schema.__schema__(:type, field) do - :float -> [step: 0.01] - _ -> [] - end + opts + |> Keyword.merge( + case schema.__schema__(:type, field) do + :float -> [step: 0.01] + _ -> [] + end + ) + |> Keyword.merge( + cond do + map_size(form.source.changes) > 0 -> [value: form.source.changes[field]] + true -> [] + end ) apply(Phoenix.HTML.Form, type, [form, field, opts]) From bc811eafc4e7929009fe0b8ba13733f22f1449b1 Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Fri, 7 Dec 2018 15:29:52 +0000 Subject: [PATCH 2/3] renders nested changesets for many-to-many relationships --- lib/templates/custom/custom.html.eex | 4 ++-- lib/views/custom_view.ex | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/templates/custom/custom.html.eex b/lib/templates/custom/custom.html.eex index 0ccbe31..8e0ade1 100644 --- a/lib/templates/custom/custom.html.eex +++ b/lib/templates/custom/custom.html.eex @@ -3,8 +3,8 @@ <%= if html = Map.get(element, :html) do %> <%= html %> <% else %> - <%= if String.to_existing_atom(element.schema_name) in @assoc_list do %> - <%= inputs_for f, :user, fn p -> %> + <%= if assoc_schema = one_of([String.to_existing_atom(element.schema_name), String.to_existing_atom(element.schema_name <> "s")], @assoc_list) do %> + <%= inputs_for f, assoc_schema, fn p -> %> <%= render "field_inputs.html", element: element, f: p, changeset: @changeset %> <% end %> <% else %> diff --git a/lib/views/custom_view.ex b/lib/views/custom_view.ex index 1487c4d..584c52a 100644 --- a/lib/views/custom_view.ex +++ b/lib/views/custom_view.ex @@ -33,4 +33,10 @@ defmodule Autoform.CustomView do apply(Phoenix.HTML.Form, type, [form, field, opts]) end + + def one_of(list_1, list_2) do + Enum.reduce(list_1, nil, fn el, acc -> + if el in list_2, do: el + end) + end end From 9f22e45e25af1bdea79e74fd4563628662055780 Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Fri, 7 Dec 2018 15:52:03 +0000 Subject: [PATCH 3/3] correctly gets plural schema name --- lib/templates/custom/custom.html.eex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/templates/custom/custom.html.eex b/lib/templates/custom/custom.html.eex index 8e0ade1..e9e5478 100644 --- a/lib/templates/custom/custom.html.eex +++ b/lib/templates/custom/custom.html.eex @@ -3,7 +3,7 @@ <%= if html = Map.get(element, :html) do %> <%= html %> <% else %> - <%= if assoc_schema = one_of([String.to_existing_atom(element.schema_name), String.to_existing_atom(element.schema_name <> "s")], @assoc_list) do %> + <%= if assoc_schema = one_of([String.to_existing_atom(element.schema_name), element.schema.__schema__(:source)], @assoc_list) do %> <%= inputs_for f, assoc_schema, fn p -> %> <%= render "field_inputs.html", element: element, f: p, changeset: @changeset %> <% end %>