Skip to content

Instantly share code, notes, and snippets.

@michelson
Created July 5, 2023 01:50
Show Gist options
  • Save michelson/13713f9d007345b2ced4a2cc2257cf01 to your computer and use it in GitHub Desktop.
Save michelson/13713f9d007345b2ced4a2cc2257cf01 to your computer and use it in GitHub Desktop.
Auto Context
defmodule Rauversion.AutoContext do
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
import Ecto.Query, warn: false
alias Rauversion.Repo
def list do
Repo.all(unquote(opts))
end
def get!(id), do: Repo.get!(unquote(opts), id)
def create(attrs \\ %{}) do
%unquote(opts){}
|> unquote(opts).changeset(attrs)
|> Repo.insert()
end
def update(%unquote(opts){} = connected_account, attrs) do
connected_account
|> unquote(opts).changeset(attrs)
|> Repo.update()
end
def delete(%unquote(opts){} = connected_account) do
Repo.delete(connected_account)
end
def change(
%unquote(opts){} = connected_account,
attrs \\ %{}
) do
unquote(opts).changeset(connected_account, attrs)
end
defoverridable list: 0, get!: 1, create: 1, update: 2, delete: 1, change: 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment