From adc77429f67d848a8c63c3ff9a803ceb33d58d6a Mon Sep 17 00:00:00 2001 From: SeinopSys Date: Sun, 10 Oct 2021 01:40:08 +0200 Subject: [PATCH] set default sources for image view page if none are available --- lib/philomena_web/controllers/image_controller.ex | 7 ++++++- lib/philomena_web/templates/image/new.html.slime | 2 -- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index c7743ef0..4c6e5b8b 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -81,6 +81,7 @@ defmodule PhilomenaWeb.ImageController do image_changeset = image + |> update_in([:sources], &sources_for_edit/1) |> Images.change_image() watching = Images.subscribed?(image, conn.assigns.current_user) @@ -109,7 +110,7 @@ defmodule PhilomenaWeb.ImageController do def new(conn, _params) do changeset = - %Image{sources: [%Source{}]} + %Image{sources: sources_for_edit()} |> Images.change_image() render(conn, "new.html", title: "New Image", changeset: changeset) @@ -218,4 +219,8 @@ defmodule PhilomenaWeb.ImageController do |> assign(:source_change_count, source_changes) end end + + defp sources_for_edit(), do: [%Source{}] + defp sources_for_edit([]), do: sources_for_edit() + defp source_for_edit(sources), do: sources end diff --git a/lib/philomena_web/templates/image/new.html.slime b/lib/philomena_web/templates/image/new.html.slime index c23e04e3..0f2a98be 100644 --- a/lib/philomena_web/templates/image/new.html.slime +++ b/lib/philomena_web/templates/image/new.html.slime @@ -43,8 +43,6 @@ span.js-max-source-count> 10 ' source URLs. Leave any sources you don't want to use blank. - = inputs_for f, :sources, [as: "image[old_sources]"], fn fs -> - = hidden_input fs, :source = inputs_for f, :sources, fn fs -> .field.js-image-source.field--inline.flex--no-wrap.flex--centered = text_input fs, :source, class: "input flex__grow js-source-url", placeholder: "Source URL"