use changeset helper

This commit is contained in:
shibao 2022-07-04 20:39:21 -04:00
parent 3593334c85
commit 6c09261368
14 changed files with 93 additions and 90 deletions

View File

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

View File

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

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

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_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] ""

View File

@ -177,17 +177,17 @@ msgid "Tag could not be removed"
msgstr "Le tag na 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 ""

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

View File

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

View File

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