From 3593334c857f4d4ff034d7203a439f123c6f85d9 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 4 Jul 2022 20:30:05 -0400 Subject: [PATCH] fix container changeset --- lib/cannery/containers.ex | 26 +++---------------- lib/cannery/containers/container.ex | 8 +++--- .../live/container_live/form_component.ex | 5 ++-- priv/gettext/de/LC_MESSAGES/errors.po | 2 +- priv/gettext/de/LC_MESSAGES/prompts.po | 4 +-- priv/gettext/en/LC_MESSAGES/errors.po | 2 +- priv/gettext/en/LC_MESSAGES/prompts.po | 4 +-- priv/gettext/errors.pot | 2 +- priv/gettext/es/LC_MESSAGES/errors.po | 2 +- priv/gettext/es/LC_MESSAGES/prompts.po | 4 +-- priv/gettext/fr/LC_MESSAGES/errors.po | 2 +- priv/gettext/fr/LC_MESSAGES/prompts.po | 4 +-- priv/gettext/prompts.pot | 4 +-- test/cannery/containers_test.exs | 4 --- 14 files changed, 26 insertions(+), 47 deletions(-) diff --git a/lib/cannery/containers.ex b/lib/cannery/containers.ex index b4c4c035..8d9575dc 100644 --- a/lib/cannery/containers.ex +++ b/lib/cannery/containers.ex @@ -71,9 +71,8 @@ defmodule Cannery.Containers do """ @spec create_container(attrs :: map(), User.t()) :: {:ok, Container.t()} | {:error, Changeset.t(Container.new_container())} - def create_container(attrs, %User{id: user_id}) do - attrs = attrs |> Map.put("user_id", user_id) - %Container{} |> Container.create_changeset(attrs) |> Repo.insert() + def create_container(attrs, %User{} = user) do + %Container{} |> Container.create_changeset(user, attrs) |> Repo.insert() end @doc """ @@ -122,7 +121,7 @@ defmodule Cannery.Containers do error = dgettext("errors", "Container must be empty before deleting") container - |> change_container() + |> Container.update_changeset(%{}) |> Changeset.add_error(:ammo_groups, error) |> Changeset.apply_action(:delete) end @@ -143,25 +142,6 @@ defmodule Cannery.Containers do container end - @doc """ - Returns an `%Changeset{}` for tracking container changes. - - ## Examples - - iex> change_container(container) - %Changeset{data: %Container{}} - - iex> change_container(%Changeset{}) - %Changeset{data: %Container{}} - - """ - @spec change_container(Container.t() | Container.new_container()) :: - Changeset.t(Container.t() | Container.new_container()) - @spec change_container(Container.t() | Container.new_container(), attrs :: map()) :: - Changeset.t(Container.t() | Container.new_container()) - def change_container(container, attrs \\ %{}), - do: container |> Container.update_changeset(attrs) - @doc """ Adds a tag to a container diff --git a/lib/cannery/containers/container.ex b/lib/cannery/containers/container.ex index 59cc2f06..c6bd8ae6 100644 --- a/lib/cannery/containers/container.ex +++ b/lib/cannery/containers/container.ex @@ -42,10 +42,12 @@ defmodule Cannery.Containers.Container do @type id :: UUID.t() @doc false - @spec create_changeset(new_container(), attrs :: map()) :: Changeset.t(new_container()) - def create_changeset(container, attrs) do + @spec create_changeset(new_container(), User.t(), attrs :: map()) :: + Changeset.t(new_container()) + def create_changeset(container, %User{id: user_id}, attrs) do container - |> cast(attrs, [:name, :desc, :type, :location, :user_id]) + |> change(user_id: user_id) + |> cast(attrs, [:name, :desc, :type, :location]) |> validate_required([:name, :type, :user_id]) end diff --git a/lib/cannery_web/live/container_live/form_component.ex b/lib/cannery_web/live/container_live/form_component.ex index c22fe4b6..3ac0ae60 100644 --- a/lib/cannery_web/live/container_live/form_component.ex +++ b/lib/cannery_web/live/container_live/form_component.ex @@ -14,7 +14,8 @@ defmodule CanneryWeb.ContainerLive.FormComponent do Socket.t() ) :: {:ok, Socket.t()} def update(%{container: container} = assigns, socket) do - {:ok, socket |> assign(assigns) |> assign(:changeset, Containers.change_container(container))} + changeset = container |> Container.update_changeset(%{}) + {:ok, socket |> assign(assigns) |> assign(:changeset, changeset)} end @impl true @@ -23,7 +24,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do %{"container" => container_params}, %{assigns: %{container: container}} = socket ) do - changeset = container |> Containers.change_container(container_params) + changeset = container |> Container.update_changeset(container_params) {:noreply, socket |> assign(:changeset, changeset)} end diff --git a/priv/gettext/de/LC_MESSAGES/errors.po b/priv/gettext/de/LC_MESSAGES/errors.po index c764d283..7e6cc048 100644 --- a/priv/gettext/de/LC_MESSAGES/errors.po +++ b/priv/gettext/de/LC_MESSAGES/errors.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/containers.ex:122 +#: lib/cannery/containers.ex:121 msgid "Container must be empty before deleting" msgstr "Behälter muss vor dem Löschen leer sein" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index f3b1a48e..2563eb33 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -25,7 +25,7 @@ msgstr "" ## 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/container_live/form_component.ex:65 +#: 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 msgid "%{name} created successfully" @@ -63,7 +63,7 @@ msgstr "%{name} erfolgreich aktualisiert" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:46 -#: lib/cannery_web/live/container_live/form_component.ex:47 +#: 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 msgid "%{name} updated successfully" diff --git a/priv/gettext/en/LC_MESSAGES/errors.po b/priv/gettext/en/LC_MESSAGES/errors.po index 6722c218..14dde8b9 100644 --- a/priv/gettext/en/LC_MESSAGES/errors.po +++ b/priv/gettext/en/LC_MESSAGES/errors.po @@ -11,7 +11,7 @@ msgstr "" "Language: en\n" #, elixir-autogen, elixir-format -#: lib/cannery/containers.ex:122 +#: lib/cannery/containers.ex:121 msgid "Container must be empty before deleting" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index 15d4dfc1..a24e4a87 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -13,7 +13,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:64 -#: lib/cannery_web/live/container_live/form_component.ex:65 +#: 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 msgid "%{name} created successfully" @@ -51,7 +51,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:46 -#: lib/cannery_web/live/container_live/form_component.ex:47 +#: 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 msgid "%{name} updated successfully" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot index bf7fb127..2036b017 100644 --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -11,7 +11,7 @@ msgid "" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery/containers.ex:122 +#: lib/cannery/containers.ex:121 msgid "Container must be empty before deleting" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/errors.po b/priv/gettext/es/LC_MESSAGES/errors.po index f741caae..cec519b4 100644 --- a/priv/gettext/es/LC_MESSAGES/errors.po +++ b/priv/gettext/es/LC_MESSAGES/errors.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/containers.ex:122 +#: lib/cannery/containers.ex:121 msgid "Container must be empty before deleting" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index 1296fea9..183aa199 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -23,7 +23,7 @@ msgstr "" ## 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/container_live/form_component.ex:65 +#: 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 msgid "%{name} created successfully" @@ -61,7 +61,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:46 -#: lib/cannery_web/live/container_live/form_component.ex:47 +#: 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 msgid "%{name} updated successfully" diff --git a/priv/gettext/fr/LC_MESSAGES/errors.po b/priv/gettext/fr/LC_MESSAGES/errors.po index d0809bae..28963d7d 100644 --- a/priv/gettext/fr/LC_MESSAGES/errors.po +++ b/priv/gettext/fr/LC_MESSAGES/errors.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/containers.ex:122 +#: lib/cannery/containers.ex:121 msgid "Container must be empty before deleting" msgstr "Le conteneur doit être vide pour être supprimé" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index f2ee824b..694b2d59 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -25,7 +25,7 @@ msgstr "" ## 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/container_live/form_component.ex:65 +#: 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 msgid "%{name} created successfully" @@ -63,7 +63,7 @@ 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/container_live/form_component.ex:47 +#: 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 msgid "%{name} updated successfully" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 900c620d..326ac199 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -12,7 +12,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:64 -#: lib/cannery_web/live/container_live/form_component.ex:65 +#: 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 msgid "%{name} created successfully" @@ -50,7 +50,7 @@ msgstr "" #, elixir-autogen, elixir-format #: lib/cannery_web/live/ammo_type_live/form_component.ex:46 -#: lib/cannery_web/live/container_live/form_component.ex:47 +#: 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 msgid "%{name} updated successfully" diff --git a/test/cannery/containers_test.exs b/test/cannery/containers_test.exs index e54ff297..022e1a57 100644 --- a/test/cannery/containers_test.exs +++ b/test/cannery/containers_test.exs @@ -87,9 +87,5 @@ defmodule Cannery.ContainersTest do Containers.get_container!(container.id, current_user) end end - - test "change_container/1 returns a container changeset", %{container: container} do - assert %Changeset{} = Containers.change_container(container) - end end end