Updated source's friendly_name to default to the collection_name (#29)

This commit is contained in:
Kieran
2024-02-20 14:24:47 -08:00
committed by GitHub
parent 47f0fd533f
commit 94f777d1ed
9 changed files with 101 additions and 3 deletions
+3
View File
@@ -5,6 +5,7 @@ defmodule Pinchflat.Sources.Source do
use Ecto.Schema
import Ecto.Changeset
import Pinchflat.Utils.ChangesetUtils
alias Pinchflat.Media.MediaItem
alias Pinchflat.Profiles.MediaProfile
@@ -25,6 +26,7 @@ defmodule Pinchflat.Sources.Source do
collection_name
collection_id
collection_type
friendly_name
download_media
original_url
media_profile_id
@@ -53,6 +55,7 @@ defmodule Pinchflat.Sources.Source do
def changeset(source, attrs) do
source
|> cast(attrs, @allowed_fields)
|> dynamic_default(:friendly_name, fn cs -> get_field(cs, :collection_name) end)
|> validate_required(@required_fields)
|> unique_constraint([:collection_id, :media_profile_id])
end
+1 -1
View File
@@ -118,7 +118,7 @@ defmodule Pinchflat.Sources do
defp add_source_details_by_collection_type(source, changeset, source_details) do
%Ecto.Changeset{changes: changes} = changeset
collection_type = source.collection_type || changes[:collection_type]
collection_type = Ecto.Changeset.get_field(changeset, :collection_type)
collection_changes =
case collection_type do
+19
View File
@@ -0,0 +1,19 @@
defmodule Pinchflat.Utils.ChangesetUtils do
@moduledoc """
Utility methods for working with changesets
"""
import Ecto.Changeset
@doc """
Sets the default value of a field if it is nil by applying the given function.
Returns %Ecto.Changeset{}.
"""
def dynamic_default(changeset, key, value_fn) do
case get_field(changeset, key) do
nil -> put_change(changeset, key, value_fn.(changeset))
_ -> changeset
end
end
end
+1 -1
View File
@@ -1,6 +1,6 @@
defmodule Pinchflat.Utils.FunctionUtils do
@moduledoc """
Utility functions for working with functions
Utility methods for working with functions
"""
@doc """
+1 -1
View File
@@ -1,6 +1,6 @@
defmodule Pinchflat.Utils.StringUtils do
@moduledoc """
Utility functions for working with strings
Utility methods for working with strings
"""
@doc """