andrewtimberlake/trunk

Trunk and Ecto

Closed this issue · 2 comments

Hi, @andrewtimberlake

First of all thanks for this library ❤️

I'm not entirely sure what is the idiom way to integrate trunk with Ecto.
I'm looking for adding an attachment to a model.

this would be nice to get documented or even something similar to arc_ecto

I'm willing to contribute to this library but I would like to get some guidance on what way you see this resolved.

Thanks @Schultzer
My current workflow is to save the associated record, then upload the attachment, then update the record with the attachment information.

Something like,

Ecto.Multi.new
|> Ecto.Multi.insert(:insert_record, record_changeset)
|> Ecto.Multi.run(:attachment, fn %{insert_record: record} ->
  Trunk.store(file, record)
end)
|> Ecto.Multi.run(:update_record, fn %{insert_record: record, attachment: attachment} ->
  record
  |> Ecto.Changeset.change(record, file_info: Trunk.State.save(attachment))
  |> Repo.update
end)
|> Repo.transaction
|> case do
    {:ok, %{update_record: record}} -> {:ok, record}
    error -> error
  end

I’m not wild about the hoops arc_ecto jumps through to work.
I wrote Trunk because I wanted a lot more control over how attachments were processed and a lot less hidden magic.
If you have ideas on a clean API for the Ecto side, I’d love to hear them.
It would need to support the various ways you can save attachment info with Trunk—simple string, map etc.

@andrewtimberlake I like this a lot.

I will try implement Trunk into our production app instead of arc and then I will reply with our experience and see if there is anything we can do to make Trunk better 😄