From 9c6c9d55ed8b28aeeda826c4dc65f9aacc100dce Mon Sep 17 00:00:00 2001 From: azhi Date: Tue, 16 Jan 2018 18:18:00 +0300 Subject: [PATCH] allow passing binary data structs as attachments arc allows storing `%{filename: filename, binary: data}`, and now arc_ecto allows these structs to be accepted in `cast_attachments` fixes #54 --- lib/arc_ecto/schema.ex | 5 +++++ test/schema_test.exs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/arc_ecto/schema.ex b/lib/arc_ecto/schema.ex index 3b9a56c..1331ad2 100644 --- a/lib/arc_ecto/schema.ex +++ b/lib/arc_ecto/schema.ex @@ -39,6 +39,11 @@ defmodule Arc.Ecto.Schema do # Allow casting Plug.Uploads {field, upload = %{__struct__: Plug.Upload}}, fields -> [{field, {upload, scope}} | fields] + # Allow casting binary data structs + {field, upload = %{filename: filename, binary: binary}}, fields + when is_binary(filename) and is_binary(binary) -> + [{field, {upload, scope}} | fields] + # If casting a binary (path), ensure we've explicitly allowed paths {field, path}, fields when is_binary(path) -> if Keyword.get(options, :allow_paths, false) do diff --git a/test/schema_test.exs b/test/schema_test.exs index 86f71a1..452e55e 100644 --- a/test/schema_test.exs +++ b/test/schema_test.exs @@ -101,4 +101,9 @@ defmodule ArcTest.Ecto.Schema do changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"}) assert called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}}) end + + test_with_mock "casting binary data struct attachments", DummyDefinition, [store: fn({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) -> {:ok, "file.png"} end] do + changeset = TestUser.changeset(%TestUser{}, %{"avatar" => %{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}}) + assert called DummyDefinition.store({%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}) + end end