From 6c09261368c27f065a64336c8ad49e67fd8bc1c3 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 4 Jul 2022 20:39:21 -0400 Subject: [PATCH] use changeset helper --- lib/cannery/ammo.ex | 16 ------ .../live/ammo_group_live/form_component.ex | 55 ++++++++++--------- .../live/ammo_type_live/form_component.ex | 37 ++++++++++--- priv/gettext/de/LC_MESSAGES/errors.po | 6 +- priv/gettext/de/LC_MESSAGES/prompts.po | 8 +-- priv/gettext/en/LC_MESSAGES/errors.po | 6 +- priv/gettext/en/LC_MESSAGES/prompts.po | 8 +-- priv/gettext/errors.pot | 6 +- priv/gettext/es/LC_MESSAGES/errors.po | 6 +- priv/gettext/es/LC_MESSAGES/prompts.po | 8 +-- priv/gettext/fr/LC_MESSAGES/errors.po | 6 +- priv/gettext/fr/LC_MESSAGES/prompts.po | 8 +-- priv/gettext/prompts.pot | 8 +-- test/cannery/ammo_test.exs | 5 -- 14 files changed, 93 insertions(+), 90 deletions(-) diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 0be8e860..94f47ead 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -191,22 +191,6 @@ defmodule Cannery.Ammo do def delete_ammo_type!(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}), do: ammo_type |> Repo.delete!() - @doc """ - Returns an `%Changeset{}` for tracking ammo_type changes. - - ## Examples - - iex> change_ammo_type(ammo_type) - %Changeset{data: %AmmoType{}} - - """ - @spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type()) :: - Changeset.t(AmmoType.t() | AmmoType.new_ammo_type()) - @spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) :: - Changeset.t(AmmoType.t() | AmmoType.new_ammo_type()) - def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}), - do: AmmoType.update_changeset(ammo_type, attrs) - @doc """ Returns the list of ammo_groups for a user and type. diff --git a/lib/cannery_web/live/ammo_group_live/form_component.ex b/lib/cannery_web/live/ammo_group_live/form_component.ex index b17a1dbd..1dbbc951 100644 --- a/lib/cannery_web/live/ammo_group_live/form_component.ex +++ b/lib/cannery_web/live/ammo_group_live/form_component.ex @@ -21,11 +21,11 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do end @spec update(Socket.t()) :: {:ok, Socket.t()} - def update(%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket) do + def update(%{assigns: %{current_user: current_user}} = socket) do socket = socket |> assign(:ammo_group_create_limit, @ammo_group_create_limit) - |> assign(:changeset, ammo_group |> AmmoGroup.update_changeset(%{})) + |> assign_changeset(%{}) |> assign(:ammo_types, Ammo.list_ammo_types(current_user)) |> assign_new(:containers, fn -> Containers.list_containers(current_user) end) @@ -33,11 +33,35 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do end @impl true + def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do + {:noreply, socket |> assign_changeset(ammo_group_params)} + end + def handle_event( - "validate", + "save", %{"ammo_group" => ammo_group_params}, - %{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket + %{assigns: %{action: action}} = socket ) do + save_ammo_group(socket, action, ammo_group_params) + end + + # HTML Helpers + @spec container_options([Container.t()]) :: [{String.t(), Container.id()}] + defp container_options(containers) do + containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end) + end + + @spec ammo_type_options([AmmoType.t()]) :: [{String.t(), AmmoType.id()}] + defp ammo_type_options(ammo_types) do + ammo_types |> Enum.map(fn %{id: id, name: name} -> {name, id} end) + end + + # Save Helpers + + defp assign_changeset( + %{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket, + ammo_group_params + ) do changeset_action = case action do :new -> :insert @@ -69,30 +93,9 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do {:error, changeset} -> changeset end - {:noreply, socket |> assign(:changeset, changeset)} + socket |> assign(:changeset, changeset) end - def handle_event( - "save", - %{"ammo_group" => ammo_group_params}, - %{assigns: %{action: action}} = socket - ) do - save_ammo_group(socket, action, ammo_group_params) - end - - # HTML Helpers - @spec container_options([Container.t()]) :: [{String.t(), Container.id()}] - defp container_options(containers) do - containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end) - end - - @spec ammo_type_options([AmmoType.t()]) :: [{String.t(), AmmoType.id()}] - defp ammo_type_options(ammo_types) do - ammo_types |> Enum.map(fn %{id: id, name: name} -> {name, id} end) - end - - # Save Helpers - defp save_ammo_group( %{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} = socket, diff --git a/lib/cannery_web/live/ammo_type_live/form_component.ex b/lib/cannery_web/live/ammo_type_live/form_component.ex index ac5e910e..37812a28 100644 --- a/lib/cannery_web/live/ammo_type_live/form_component.ex +++ b/lib/cannery_web/live/ammo_type_live/form_component.ex @@ -13,17 +13,13 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do %{:ammo_type => AmmoType.t(), :current_user => User.t(), optional(any) => any}, Socket.t() ) :: {:ok, Socket.t()} - def update(%{ammo_type: ammo_type, current_user: _current_user} = assigns, socket) do - {:ok, socket |> assign(assigns) |> assign(:changeset, Ammo.change_ammo_type(ammo_type))} + def update(%{current_user: _current_user} = assigns, socket) do + {:ok, socket |> assign(assigns) |> assign_changeset(%{})} end @impl true - def handle_event( - "validate", - %{"ammo_type" => ammo_type_params}, - %{assigns: %{ammo_type: ammo_type}} = socket - ) do - {:noreply, socket |> assign(:changeset, ammo_type |> Ammo.change_ammo_type(ammo_type_params))} + def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do + {:noreply, socket |> assign_changeset(ammo_type_params)} end def handle_event( @@ -34,6 +30,31 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do save_ammo_type(socket, action, ammo_type_params) end + defp assign_changeset( + %{assigns: %{action: action, ammo_type: ammo_type, current_user: user}} = socket, + ammo_type_params + ) do + changeset_action = + case action do + :new -> :insert + :edit -> :update + end + + changeset = + case action do + :new -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params) + :edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params) + end + + changeset = + case changeset |> Changeset.apply_action(changeset_action) do + {:ok, _data} -> changeset + {:error, changeset} -> changeset + end + + socket |> assign(changeset: changeset) + end + defp save_ammo_type( %{assigns: %{ammo_type: ammo_type, current_user: current_user, return_to: return_to}} = socket, diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index 7e6cc048..8dadcfa8 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.po @@ -176,19 +176,19 @@ msgid "Tag could not be removed" msgstr "Tag konnte nicht gelöscht werden" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:146 msgid "Could not parse number of copies" msgstr "Konnte die Anzahl der Kopien nicht verstehen" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:131 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" "Ungültige Nummer an Kopien. Muss zwischen 1 and %{max} liegen. War " "%{multiplier}" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:404 +#: lib/cannery/ammo.ex:388 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index 2563eb33..2ab84dd5 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -24,7 +24,7 @@ msgstr "" ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:64 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/tag_live/form_component.ex:101 @@ -62,7 +62,7 @@ msgid "%{name} updated succesfully" msgstr "%{name} erfolgreich aktualisiert" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:46 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/tag_live/form_component.ex:83 @@ -283,12 +283,12 @@ msgid "Ammo unstaged succesfully" msgstr "Munition erfolgreich demarkiert" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:108 msgid "Ammo updated successfully" msgstr "Munitionsgruppe erfolgreich aktualisiert" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:167 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "Munitionsgruppe erfolgreich aktualisiert" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 14dde8b9..923e26f6 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -161,17 +161,17 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:146 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:131 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:404 +#: lib/cannery/ammo.ex:388 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index a24e4a87..8035b4b0 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -12,7 +12,7 @@ msgstr "" "Plural-Forms: nplurals=2\n" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:64 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/tag_live/form_component.ex:101 @@ -50,7 +50,7 @@ msgid "%{name} updated succesfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:46 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/tag_live/form_component.ex:83 @@ -263,12 +263,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:108 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:167 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index 2036b017..e4b67c57 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -160,17 +160,17 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:146 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:131 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:404 +#: lib/cannery/ammo.ex:388 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index cec519b4..98710ca8 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.po @@ -171,17 +171,17 @@ msgid "Tag could not be removed" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:146 msgid "Could not parse number of copies" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:131 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:404 +#: lib/cannery/ammo.ex:388 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index 183aa199..1499315e 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -22,7 +22,7 @@ msgstr "" ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:64 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/tag_live/form_component.ex:101 @@ -60,7 +60,7 @@ msgid "%{name} updated succesfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:46 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/tag_live/form_component.ex:83 @@ -273,12 +273,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:108 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:167 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index 28963d7d..1d158dc4 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.po @@ -177,17 +177,17 @@ msgid "Tag could not be removed" msgstr "Le tag n’a pas pu être retiré" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:143 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:146 msgid "Could not parse number of copies" msgstr "Impossible d'analyser le nombre de copies" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:128 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:131 msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}" msgstr "Nombre de copies invalide, doit être 1 et %{max}. Été %{multiplier}" #, elixir-autogen, elixir-format -#: lib/cannery/ammo.ex:404 +#: lib/cannery/ammo.ex:388 msgid "Invalid multiplier" msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 694b2d59..4081a091 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -24,7 +24,7 @@ msgstr "" ## date. Leave "msgstr"s empty as changing them here has no ## effect: edit them in PO (.po) files instead. #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:64 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/tag_live/form_component.ex:101 @@ -62,7 +62,7 @@ msgid "%{name} updated succesfully" msgstr "%{name} mis à jour avec succès" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:46 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/tag_live/form_component.ex:83 @@ -284,12 +284,12 @@ msgid "Ammo unstaged succesfully" msgstr "Groupe de munition désélectionner avec succès" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:108 msgid "Ammo updated successfully" msgstr "Groupe de munition mis à jour avec succès" #, elixir-autogen, elixir-format, fuzzy -#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:167 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "Groupe de munition mis à jour avec succès" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 326ac199..4d63b19c 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -11,7 +11,7 @@ msgid "" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:64 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/tag_live/form_component.ex:101 @@ -49,7 +49,7 @@ msgid "%{name} updated succesfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_type_live/form_component.ex:46 +#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/tag_live/form_component.ex:83 @@ -262,12 +262,12 @@ msgid "Ammo unstaged succesfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:105 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:108 msgid "Ammo updated successfully" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/ammo_group_live/form_component.ex:164 +#: lib/cannery_web/live/ammo_group_live/form_component.ex:167 msgid "Ammo added successfully" msgid_plural "Ammo added successfully" msgstr[0] "" diff --git a/test/cannery/ammo_test.exs b/test/cannery/ammo_test.exs index 317c5a57..64384a23 100644 --- a/test/cannery/ammo_test.exs +++ b/test/cannery/ammo_test.exs @@ -92,11 +92,6 @@ defmodule Cannery.AmmoTest do assert {:ok, %AmmoType{}} = Ammo.delete_ammo_type(ammo_type, current_user) assert_raise Ecto.NoResultsError, fn -> Ammo.get_ammo_type!(ammo_type.id, current_user) end end - - test "change_ammo_type/1 returns a ammo_type changeset", - %{ammo_type: ammo_type} do - assert %Changeset{} = Ammo.change_ammo_type(ammo_type) - end end describe "ammo_groups" do