Skip to content

Commit 49bb6e5

Browse files
authored
Add Phoenix.HTML.Safe to Duration (#463)
1 parent ddaf11e commit 49bb6e5

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/phoenix_html/safe.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ defimpl Phoenix.HTML.Safe, for: DateTime do
4444
end
4545
end
4646

47+
if Code.ensure_loaded?(Duration) do
48+
defimpl Phoenix.HTML.Safe, for: Duration do
49+
defdelegate to_iodata(data), to: Duration, as: :to_iso8601
50+
end
51+
end
52+
4753
defimpl Phoenix.HTML.Safe, for: List do
4854
def to_iodata(list), do: recur(list)
4955

test/phoenix_html/safe_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ defmodule Phoenix.HTML.SafeTest do
6262
assert Safe.to_iodata(datetime) == "2000-01-01T12:13:14+00:30"
6363
end
6464

65+
if Code.ensure_loaded?(Duration) do
66+
test "impl for Duration" do
67+
duration = Duration.new!(month: 1)
68+
assert Safe.to_iodata(duration) == "P1M"
69+
end
70+
end
71+
6572
test "impl for URI" do
6673
uri = %URI{scheme: "http", host: "www.example.org", path: "/foo", query: "secret=<a&b>"}
6774

0 commit comments

Comments
 (0)