From 4265fb960444c9e5352ff5cb5a10b646aa446c56 Mon Sep 17 00:00:00 2001 From: Jerod Santo Date: Mon, 4 Sep 2023 08:30:38 -0500 Subject: [PATCH] Ensure name field is optional on subscribe form --- lib/changelog/kits/map_kit.ex | 11 +++++++++++ lib/changelog_web/controllers/person_controller.ex | 3 ++- test/changelog/kits/map_kit_test.exs | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/changelog/kits/map_kit.ex create mode 100644 test/changelog/kits/map_kit_test.exs diff --git a/lib/changelog/kits/map_kit.ex b/lib/changelog/kits/map_kit.ex new file mode 100644 index 0000000000..b1dcb521c3 --- /dev/null +++ b/lib/changelog/kits/map_kit.ex @@ -0,0 +1,11 @@ +defmodule Changelog.MapKit do + alias Changelog.StringKit + @doc """ + Returns a map with all key/value pairs removed where the value was blank + """ + def sans_blanks(map) do + map + |> Enum.reject(fn {_k, v} -> StringKit.blank?(v) end) + |> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, v) end) + end +end diff --git a/lib/changelog_web/controllers/person_controller.ex b/lib/changelog_web/controllers/person_controller.ex index e6cee5e4ff..af939a84ad 100755 --- a/lib/changelog_web/controllers/person_controller.ex +++ b/lib/changelog_web/controllers/person_controller.ex @@ -5,6 +5,7 @@ defmodule ChangelogWeb.PersonController do Cache, Captcha, Episode, + MapKit, NewsItem, Newsletters, Person, @@ -192,7 +193,7 @@ defmodule ChangelogWeb.PersonController do else changeset = Person.with_fake_data() - |> Person.insert_changeset(params) + |> Person.insert_changeset(MapKit.sans_blanks(params)) case Repo.insert(changeset) do {:ok, person} -> diff --git a/test/changelog/kits/map_kit_test.exs b/test/changelog/kits/map_kit_test.exs new file mode 100644 index 0000000000..2231814168 --- /dev/null +++ b/test/changelog/kits/map_kit_test.exs @@ -0,0 +1,13 @@ +defmodule Changelog.MapKitTest do + use ExUnit.Case + + alias Changelog.MapKit + + describe "sans_blanks/1" do + test "returns a map that removes blank values" do + map = %{"name" => "", "email" => nil, "ohai" => "yes"} + + assert MapKit.sans_blanks(map) == %{"ohai" => "yes"} + end + end +end