Extract safe result from hidden fields within inputs_for/4

Also switches to using `with` instead of `lexical_scoping` because it is
more idiomatic.
This commit is contained in:
Mitchell Hanberg 2019-07-07 22:26:32 -04:00
parent 2929e6c7c3
commit d210d3bff5
8 changed files with 32 additions and 19 deletions

View file

@ -6,7 +6,8 @@ defmodule Mix.Tasks.UpdateMdnDocs do
@shortdoc "Update the MDN documentation"
def run(_) do
IO.puts "Downloading MDN documentation"
IO.puts("Downloading MDN documentation")
(Temple.Tags.nonvoid_elements() ++ Temple.Tags.void_elements())
|> Enum.map(fn el ->
Task.async(fn ->

View file

@ -35,17 +35,15 @@ defmodule Temple do
quote do
import Kernel, except: [div: 2]
Temple.Utils.lexical_scope(fn ->
{:ok, var!(buff, Temple.Tags)} = Temple.Utils.start_buffer([])
with {:ok, var!(buff, Temple.Tags)} <- Temple.Utils.start_buffer([]) do
unquote(block)
markup = Temple.Utils.get_buffer(var!(buff, Temple.Tags))
:ok = Temple.Utils.stop_buffer(var!(buff, Temple.Tags))
markup |> Enum.reverse() |> Enum.join("") |> Phoenix.HTML.raw()
end)
Temple.Utils.join_and_escape(markup)
end
end
end

View file

@ -293,7 +293,9 @@ defmodule Temple.Form do
form.impl.to_form(form.source, form, field, options)
|> Enum.each(fn form ->
Enum.map(form.hidden, fn {k, v} ->
Phoenix.HTML.Form.hidden_input(form, k, value: v)
{:safe, hidden_input} = Phoenix.HTML.Form.hidden_input(form, k, value: v)
hidden_input
end)
|> Enum.each(&Utils.put_buffer(var!(buff, Temple.Tags), &1))

View file

@ -43,7 +43,7 @@ defmodule Temple.Tags do
input name: "comments", placeholder: "Enter a comment..."
end
# {:save,
# {:safe,
# "<div></div>
# <div class=\"text-red\" id=\"my-el\"></div>
# <div>

View file

@ -45,8 +45,8 @@ defmodule Temple.Utils do
end
end
def lexical_scope(work) do
work.()
def join_and_escape(markup) do
markup |> Enum.reverse() |> Enum.join("") |> Phoenix.HTML.raw()
end
def start_buffer(initial_buffer), do: Agent.start(fn -> initial_buffer end)

View file

@ -56,7 +56,7 @@ defmodule Temple.FormTest do
defmodule Person do
use Ecto.Schema
embedded_schema do
schema "persons" do
field(:name)
belongs_to(:company, Company)
has_many(:responsibilities, Reponsibility)
@ -66,7 +66,7 @@ defmodule Temple.FormTest do
defmodule Company do
use Ecto.Schema
embedded_schema do
schema "companies" do
field(:name)
field(:field)
end
@ -75,8 +75,10 @@ defmodule Temple.FormTest do
defmodule Responsibility do
use Ecto.Schema
embedded_schema do
schema "responsibilities" do
field(:description)
belongs_to(:person, Person)
end
end
@ -110,7 +112,14 @@ defmodule Temple.FormTest do
end
test "generates inputs for has_many" do
person = %Person{responsibilities: [%Responsibility{}, %Responsibility{}]}
person = %Person{
id: 1,
responsibilities: [
%Responsibility{id: 1, person_id: 1},
%Responsibility{id: 2, person_id: 1}
]
}
changeset = Ecto.Changeset.change(person)
action = "/"
opts = []
@ -121,7 +130,8 @@ defmodule Temple.FormTest do
text_input(form, :name)
inputs_for form, :responsibilities do
text_input(inner_form, :description)
phx_label(inner_form, :description)
text_area(inner_form, :description)
_ = "Bob"
end
end

View file

@ -42,9 +42,7 @@ defmodule Temple.TagsTest do
test "renders a #{tag} with a block" do
{:safe, result} =
htm do
unquote(tag)() do
unquote(tag)()
end
unquote(tag)(do: unquote(tag)())
end
assert result == ~s{<#{unquote(tag)}><#{unquote(tag)}></#{unquote(tag)}></#{unquote(tag)}>}
@ -58,7 +56,10 @@ defmodule Temple.TagsTest do
end
end
assert result == ~s{<#{unquote(tag)} class="hello"><#{unquote(tag)}></#{unquote(tag)}></#{unquote(tag)}>}
assert result ==
~s{<#{unquote(tag)} class="hello"><#{unquote(tag)}></#{unquote(tag)}></#{
unquote(tag)
}>}
end
end

View file

@ -160,6 +160,7 @@ defmodule TempleTest do
variable_as_prop_with_block props do
div()
end
variable_as_prop_with_block %{bob: "hi"} do
div()
end