Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Jul 8, 2024
2 parents ce484d1 + 6c94595 commit 0e8010e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 4 additions & 2 deletions lib/philomena/images/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ defmodule Philomena.Images.Source do
@doc false
def changeset(source, attrs) do
source
|> cast(attrs, [])
|> validate_required([])
|> cast(attrs, [:source])
|> validate_required([:source])
|> validate_format(:source, ~r/\Ahttps?:\/\//)
|> validate_length(:source, max: 255)
end
end
10 changes: 4 additions & 6 deletions lib/philomena/images/source_differ.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule Philomena.Images.SourceDiffer do
import Ecto.Changeset
alias Philomena.Images.Source

def diff_input(changeset, old_sources, new_sources) do
old_set = MapSet.new(flatten_input(old_sources))
Expand All @@ -13,12 +12,11 @@ defmodule Philomena.Images.SourceDiffer do
{sources, actually_added, actually_removed} =
apply_changes(source_set, added_sources, removed_sources)

image_id = fetch_field!(changeset, :id)

changeset
|> cast(source_params(sources), [])
|> put_change(:added_sources, actually_added)
|> put_change(:removed_sources, actually_removed)
|> put_assoc(:sources, source_structs(image_id, sources))
|> cast_assoc(:sources)
end

defp apply_changes(source_set, added_set, removed_set) do
Expand All @@ -44,8 +42,8 @@ defmodule Philomena.Images.SourceDiffer do
{sources, actually_added, actually_removed}
end

defp source_structs(image_id, sources) do
Enum.map(sources, &%Source{image_id: image_id, source: &1})
defp source_params(sources) do
%{sources: Enum.map(sources, &%{source: &1})}
end

defp flatten_input(input) when is_map(input) do
Expand Down

0 comments on commit 0e8010e

Please sign in to comment.