mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-25 17:30:59 +00:00
Simplify changesets
This commit is contained in:
parent
b3033d0a94
commit
4f474b6462
1 changed files with 8 additions and 8 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue