mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +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,21 +335,19 @@ 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,
|
image = repo.preload(image, [:sources])
|
||||||
fn repo, _chg ->
|
|
||||||
image = repo.preload(image, [:sources])
|
|
||||||
|
|
||||||
image
|
image
|
||||||
|> Image.source_changeset(%{}, old_sources, new_sources)
|
|> Image.source_changeset(%{}, old_sources, new_sources)
|
||||||
|> repo.update()
|
|> repo.update()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, image} ->
|
{:ok, image} ->
|
||||||
{:ok, {image, image.added_sources, image.removed_sources}}
|
{:ok, {image, image.added_sources, image.removed_sources}}
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
error
|
error
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|> Multi.run(:added_source_changes, fn repo, %{image: {image, added_sources, _removed}} ->
|
|> Multi.run(:added_source_changes, fn repo, %{image: {image, added_sources, _removed}} ->
|
||||||
source_changes =
|
source_changes =
|
||||||
|
@ -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
|
||||||
|
|
|
@ -841,9 +841,9 @@ ALTER SEQUENCE public.image_intensities_id_seq OWNED BY public.image_intensities
|
||||||
-- Name: image_sources; Type: TABLE; Schema: public; Owner: -
|
-- Name: image_sources; Type: TABLE; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
|
|
||||||
CREATE TABLE public.image_sources(
|
CREATE TABLE public.image_sources (
|
||||||
image_id bigint NOT NULL,
|
image_id bigint NOT NULL,
|
||||||
source character varying(255) NOT NULL,
|
source character varying(255) NOT NULL,
|
||||||
CONSTRAINT image_sources_source_check CHECK (((source)::text ~* '^https?://'::text))
|
CONSTRAINT image_sources_source_check CHECK (((source)::text ~* '^https?://'::text))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue