fix container changeset

This commit is contained in:
shibao 2022-07-04 20:30:05 -04:00
parent 57b5cb432d
commit 3593334c85
14 changed files with 26 additions and 47 deletions

View File

@ -71,9 +71,8 @@ defmodule Cannery.Containers do
""" """
@spec create_container(attrs :: map(), User.t()) :: @spec create_container(attrs :: map(), User.t()) ::
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())} {:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
def create_container(attrs, %User{id: user_id}) do def create_container(attrs, %User{} = user) do
attrs = attrs |> Map.put("user_id", user_id) %Container{} |> Container.create_changeset(user, attrs) |> Repo.insert()
%Container{} |> Container.create_changeset(attrs) |> Repo.insert()
end end
@doc """ @doc """
@ -122,7 +121,7 @@ defmodule Cannery.Containers do
error = dgettext("errors", "Container must be empty before deleting") error = dgettext("errors", "Container must be empty before deleting")
container container
|> change_container() |> Container.update_changeset(%{})
|> Changeset.add_error(:ammo_groups, error) |> Changeset.add_error(:ammo_groups, error)
|> Changeset.apply_action(:delete) |> Changeset.apply_action(:delete)
end end
@ -143,25 +142,6 @@ defmodule Cannery.Containers do
container container
end 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 """ @doc """
Adds a tag to a container Adds a tag to a container

View File

@ -42,10 +42,12 @@ defmodule Cannery.Containers.Container do
@type id :: UUID.t() @type id :: UUID.t()
@doc false @doc false
@spec create_changeset(new_container(), attrs :: map()) :: Changeset.t(new_container()) @spec create_changeset(new_container(), User.t(), attrs :: map()) ::
def create_changeset(container, attrs) do Changeset.t(new_container())
def create_changeset(container, %User{id: user_id}, attrs) do
container 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]) |> validate_required([:name, :type, :user_id])
end end

View File

@ -14,7 +14,8 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{container: container} = assigns, socket) do 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 end
@impl true @impl true
@ -23,7 +24,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
%{"container" => container_params}, %{"container" => container_params},
%{assigns: %{container: container}} = socket %{assigns: %{container: container}} = socket
) do ) do
changeset = container |> Containers.change_container(container_params) changeset = container |> Container.update_changeset(container_params)
{:noreply, socket |> assign(:changeset, changeset)} {:noreply, socket |> assign(:changeset, changeset)}
end end

View File

@ -24,7 +24,7 @@ msgstr ""
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/containers.ex:122 #: lib/cannery/containers.ex:121
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "Behälter muss vor dem Löschen leer sein" msgstr "Behälter muss vor dem Löschen leer sein"

View File

@ -25,7 +25,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -63,7 +63,7 @@ msgstr "%{name} erfolgreich aktualisiert"
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -11,7 +11,7 @@ msgstr ""
"Language: en\n" "Language: en\n"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/containers.ex:122 #: lib/cannery/containers.ex:121
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "" msgstr ""

View File

@ -13,7 +13,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -51,7 +51,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -11,7 +11,7 @@ msgid ""
msgstr "" msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/containers.ex:122 #: lib/cannery/containers.ex:121
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "" msgstr ""

View File

@ -22,7 +22,7 @@ msgstr ""
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/containers.ex:122 #: lib/cannery/containers.ex:121
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "" msgstr ""

View File

@ -23,7 +23,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -61,7 +61,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -24,7 +24,7 @@ msgstr ""
## date. Leave "msgstr"s empty as changing them here has no ## date. Leave "msgstr"s empty as changing them here has no
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery/containers.ex:122 #: lib/cannery/containers.ex:121
msgid "Container must be empty before deleting" msgid "Container must be empty before deleting"
msgstr "Le conteneur doit être vide pour être supprimé" msgstr "Le conteneur doit être vide pour être supprimé"

View File

@ -25,7 +25,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -63,7 +63,7 @@ msgstr "%{name} mis à jour avec succès"
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -12,7 +12,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -50,7 +50,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, 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: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/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -87,9 +87,5 @@ defmodule Cannery.ContainersTest do
Containers.get_container!(container.id, current_user) Containers.get_container!(container.id, current_user)
end end
end end
test "change_container/1 returns a container changeset", %{container: container} do
assert %Changeset{} = Containers.change_container(container)
end
end end
end end