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()) ::
{: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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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 ""

View File

@ -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"

View File

@ -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 ""

View File

@ -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 ""

View File

@ -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"

View File

@ -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é"

View File

@ -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"

View File

@ -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"

View File

@ -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