Skip to content

Commit

Permalink
Ensure name field is optional on subscribe form
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Sep 4, 2023
1 parent d0c850e commit 4265fb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/changelog/kits/map_kit.ex
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/changelog_web/controllers/person_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule ChangelogWeb.PersonController do
Cache,
Captcha,
Episode,
MapKit,
NewsItem,
Newsletters,
Person,
Expand Down Expand Up @@ -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} ->
Expand Down
13 changes: 13 additions & 0 deletions test/changelog/kits/map_kit_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4265fb9

Please sign in to comment.