mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
Fix source differ and caller to accept nested fields_for input
This commit is contained in:
parent
adc77429f6
commit
cb1d2b97ff
3 changed files with 36 additions and 19 deletions
|
@ -335,9 +335,7 @@ defmodule Philomena.Images do
|
||||||
new_sources = attrs["sources"]
|
new_sources = attrs["sources"]
|
||||||
|
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.run(
|
|> Multi.run(:image, fn repo, _chg ->
|
||||||
:image,
|
|
||||||
fn repo, _chg ->
|
|
||||||
image = repo.preload(image, [:sources])
|
image = repo.preload(image, [:sources])
|
||||||
|
|
||||||
image
|
image
|
||||||
|
@ -383,7 +381,7 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
%{
|
%{
|
||||||
image_id: image.id,
|
image_id: image.id,
|
||||||
source: source,
|
source_url: source,
|
||||||
user_id: user_id,
|
user_id: user_id,
|
||||||
created_at: now,
|
created_at: now,
|
||||||
updated_at: now,
|
updated_at: now,
|
||||||
|
|
|
@ -3,8 +3,8 @@ defmodule Philomena.Images.SourceDiffer do
|
||||||
alias Philomena.Images.Source
|
alias Philomena.Images.Source
|
||||||
|
|
||||||
def diff_input(changeset, old_sources, new_sources) do
|
def diff_input(changeset, old_sources, new_sources) do
|
||||||
old_set = MapSet.new(old_sources)
|
old_set = MapSet.new(flatten_input(old_sources))
|
||||||
new_set = MapSet.new(new_sources)
|
new_set = MapSet.new(flatten_input(new_sources))
|
||||||
|
|
||||||
source_set = MapSet.new(get_field(changeset, :sources), & &1.source)
|
source_set = MapSet.new(get_field(changeset, :sources), & &1.source)
|
||||||
added_sources = MapSet.difference(new_set, old_set)
|
added_sources = MapSet.difference(new_set, old_set)
|
||||||
|
@ -47,4 +47,23 @@ defmodule Philomena.Images.SourceDiffer do
|
||||||
defp source_structs(image_id, sources) do
|
defp source_structs(image_id, sources) do
|
||||||
Enum.map(sources, &%Source{image_id: image_id, source: &1})
|
Enum.map(sources, &%Source{image_id: image_id, source: &1})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp flatten_input(input) when is_map(input) do
|
||||||
|
Enum.flat_map(Map.values(input), fn
|
||||||
|
%{"source" => source} ->
|
||||||
|
source = String.trim(source)
|
||||||
|
|
||||||
|
if source != "" do
|
||||||
|
[source]
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
_ -> []
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp flatten_input(_input) do
|
||||||
|
[]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue