elixir-waffle / waffle_ecto

Waffle.Ecto provides an integration with Waffle and Ecto
https://hexdocs.pm/waffle_ecto
112 stars 30 forks source link

delete/2 is undefined or private #33

Closed mrcasals closed 3 years ago

mrcasals commented 3 years ago

Hi!

I'm trying to delete an attachment that was uploaded via Waffle to the filesystem, and I'm getting this error:

** (UndefinedFunctionError) function MyApp.Uploaders.Attachment.delete/2 is undefined or private. Did you mean one of:

           * delete/1

I have an Ecto model called Attachment which holds the actual attachment:

defmodule MyApp.Attachments.Attachment do
  use MyApp.Schema
  use Waffle.Ecto.Schema

  import Ecto.Changeset

  schema "attachments" do
    belongs_to(:post, MyApp.Posts.Post)
    belongs_to(:user, MyApp.Accounts.User)
    field(:type, AttachmentTypeEnum)
    field(:file, MyApp.Uploaders.Attachment.Type)
    field :url, :string
    field :metadata, :map

    timestamps()
  end

  def changeset(post, attrs) do
    post
    |> cast(attrs, [:type])
    |> validate_required([:user, :type])
  end
end

And here's the uploader I'm using:

defmodule MyApp.Uploaders.Attachment do
  use Waffle.Definition
  use Waffle.Ecto.Definition

  # some extra stuff like versions definitions, transformations

  def storage_dir(_versions, {_file, %{id: id} = parent}) when is_integer(id) do
    parent_name =
      parent.__struct__
      |> Module.split()
      |> List.last()
      |> String.downcase()

    PoliticAll.Uploaders.prefix("#{parent_name}/#{unquote(name)}/#{id}")
  end

  def storage_dir(_versions, {_file, %{id: id}}) when is_nil(id) do
    ""
  end
end

The context function to delete the attachment:

  def delete_raw_file(%Attachment{} = attachment) do
    attachment
    |> MyApp.Uploaders.Attachment.delete({attachment.file, attachment})
  end

And calling this function is when I get the previous error. Any idea what's going on? 😕

mrcasals commented 3 years ago

OMG I found the error.

The context function should not have the param piped, so it should look like this:

  def delete_raw_file(%Attachment{} = attachment) do
    MyApp.Uploaders.Attachment.delete({attachment.file, attachment})
  end

Sorry everyone!