Simplify changesets

This commit is contained in:
Chris McCord 2021-11-08 14:53:02 -05:00
parent b3033d0a94
commit 4f474b6462

View file

@ -187,15 +187,15 @@ defmodule LiveBeats.MediaLibrary do
Repo.delete(song) Repo.delete(song)
end end
def change_song(song_or_changeset, attrs \\ %{}) do def change_song(song_or_changeset, attrs \\ %{})
song_or_changeset
|> recycle_changeset() def change_song(%Song{} = song, attrs) do
|> Song.changeset(attrs) Song.changeset(song, attrs)
end end
defp recycle_changeset(%Ecto.Changeset{} = changeset) do def change_song(%Ecto.Changeset{} = prev_changeset, attrs) do
Map.merge(changeset, %{action: nil, errors: [], valid?: true}) %Song{}
|> change_song(attrs)
|> Ecto.Changeset.change(Map.take(prev_changeset.changes, [:duration]))
end end
defp recycle_changeset(%{} = other), do: other
end end