fix ammo_group changesets

This commit is contained in:
shibao 2022-07-04 20:06:41 -04:00
parent 9ebca20dc6
commit 947659b207
15 changed files with 196 additions and 71 deletions

View File

@ -3,6 +3,7 @@ defmodule Cannery.Ammo do
The Ammo context.
"""
import CanneryWeb.Gettext
import Ecto.Query, warn: false
alias Cannery.{Accounts.User, Containers, Repo}
alias Cannery.ActivityLog.ShotGroup
@ -350,18 +351,21 @@ defmodule Cannery.Ammo do
def create_ammo_groups(
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
multiplier,
%User{id: user_id} = user
%User{} = user
)
when multiplier >= 1 and multiplier <= @ammo_group_create_limit do
# validate ammo type and container ids belong to user
_valid_ammo_type = get_ammo_type!(ammo_type_id, user)
_valid_container = Containers.get_container!(container_id, user)
when multiplier >= 1 and multiplier <= @ammo_group_create_limit and
not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
changesets =
Enum.map(1..multiplier, fn _count ->
%AmmoGroup{} |> AmmoGroup.create_changeset(attrs |> Map.put("user_id", user_id))
%AmmoGroup{}
|> AmmoGroup.create_changeset(
get_ammo_type!(ammo_type_id, user),
Containers.get_container!(container_id, user),
user,
attrs
)
end)
if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do
@ -386,8 +390,27 @@ defmodule Cannery.Ammo do
end
end
def create_ammo_groups(invalid_attrs, _multiplier, _user) do
{:error, %AmmoGroup{} |> AmmoGroup.create_changeset(invalid_attrs)}
def create_ammo_groups(
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
_multiplier,
user
)
when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do
changeset =
%AmmoGroup{}
|> AmmoGroup.create_changeset(
get_ammo_type!(ammo_type_id, user),
Containers.get_container!(container_id, user),
user,
attrs
)
|> Changeset.add_error(:multiplier, dgettext("errors", "Invalid multiplier"))
{:error, changeset}
end
def create_ammo_groups(invalid_attrs, _multiplier, user) do
{:error, %AmmoGroup{} |> AmmoGroup.create_changeset(nil, nil, user, invalid_attrs)}
end
@doc """
@ -436,18 +459,4 @@ defmodule Cannery.Ammo do
@spec delete_ammo_group!(AmmoGroup.t(), User.t()) :: AmmoGroup.t()
def delete_ammo_group!(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
do: ammo_group |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking ammo_group changes.
## Examples
iex> change_ammo_group(ammo_group)
%Changeset{data: %AmmoGroup{}}
"""
@spec change_ammo_group(AmmoGroup.t()) :: Changeset.t(AmmoGroup.t())
@spec change_ammo_group(AmmoGroup.t(), attrs :: map()) :: Changeset.t(AmmoGroup.t())
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}),
do: AmmoGroup.update_changeset(ammo_group, attrs)
end

View File

@ -7,6 +7,7 @@ defmodule Cannery.Ammo.AmmoGroup do
"""
use Ecto.Schema
import CanneryWeb.Gettext
import Ecto.Changeset
alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers.Container}
@ -48,22 +49,49 @@ defmodule Cannery.Ammo.AmmoGroup do
@type id :: UUID.t()
@doc false
@spec create_changeset(new_ammo_group(), attrs :: map()) :: Changeset.t(new_ammo_group())
def create_changeset(ammo_group, attrs) do
@spec create_changeset(
new_ammo_group(),
AmmoType.t() | nil,
Container.t() | nil,
User.t(),
attrs :: map()
) :: Changeset.t(new_ammo_group())
def create_changeset(
ammo_group,
%AmmoType{id: ammo_type_id},
%Container{id: container_id, user_id: user_id},
%User{id: user_id},
attrs
)
when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) and
not (user_id |> is_nil()) do
ammo_group
|> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id, :user_id])
|> change(ammo_type_id: ammo_type_id)
|> change(user_id: user_id)
|> change(container_id: container_id)
|> cast(attrs, [:count, :price_paid, :notes, :staged])
|> validate_number(:count, greater_than: 0)
|> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id])
end
@doc """
Invalid changeset, used to prompt user to select ammo type and container
"""
def create_changeset(ammo_group, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do
ammo_group
|> cast(attrs, [:ammo_type_id, :container_id])
|> validate_required([:ammo_type_id, :container_id])
|> add_error(:invalid, dgettext("errors", "Please select an ammo type and container"))
end
@doc false
@spec update_changeset(t() | new_ammo_group(), attrs :: map()) ::
Changeset.t(t() | new_ammo_group())
def update_changeset(ammo_group, attrs) do
ammo_group
|> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id])
|> cast(attrs, [:count, :price_paid, :notes, :staged])
|> validate_number(:count, greater_than_or_equal_to: 0)
|> validate_required([:count, :staged, :ammo_type_id, :container_id])
|> validate_required([:count, :staged])
end
@doc """

View File

@ -22,7 +22,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
assigns,
socket
) do
changeset = Ammo.change_ammo_group(ammo_group)
changeset = ammo_group |> AmmoGroup.update_changeset(%{})
containers =
Containers.list_containers(current_user)

View File

@ -25,7 +25,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
socket =
socket
|> assign(:ammo_group_create_limit, @ammo_group_create_limit)
|> assign(:changeset, Ammo.change_ammo_group(ammo_group))
|> assign(:changeset, ammo_group |> AmmoGroup.update_changeset(%{}))
|> assign(:ammo_types, Ammo.list_ammo_types(current_user))
|> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
@ -36,7 +36,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
def handle_event(
"validate",
%{"ammo_group" => ammo_group_params},
%{assigns: %{action: action, ammo_group: ammo_group}} = socket
%{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket
) do
changeset_action =
case action do
@ -44,7 +44,24 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
:edit -> :update
end
changeset = ammo_group |> Ammo.change_ammo_group(ammo_group_params)
changeset =
case action do
:new ->
ammo_type =
if ammo_group_params |> Map.has_key?("ammo_type_id"),
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
else: nil
container =
if ammo_group_params |> Map.has_key?("container_id"),
do: ammo_group_params |> Map.get("container_id") |> Containers.get_container!(user),
else: nil
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
:edit ->
ammo_group |> AmmoGroup.update_changeset(ammo_group_params)
end
changeset =
case changeset |> Changeset.apply_action(changeset_action) do

View File

@ -152,13 +152,13 @@ msgid "Tag could not be added"
msgstr "Tag konnte nicht hinzugefügt werden"
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:102
#: lib/cannery/activity_log/shot_group.ex:115
msgid "Count must be at least 1"
msgstr "Anzahl muss mindestens 1 sein"
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
#: lib/cannery/activity_log/shot_group.ex:74
#: lib/cannery/activity_log/shot_group.ex:111
msgid "Count must be less than %{count}"
msgstr "Anzahl muss weniger als %{count} betragen"
@ -176,13 +176,28 @@ 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:126
#: lib/cannery_web/live/ammo_group_live/form_component.ex:143
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:111
#: lib/cannery_web/live/ammo_group_live/form_component.ex:128
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:407
msgid "Invalid multiplier"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo/ammo_group.ex:84
msgid "Please select an ammo type and container"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:69
msgid "Please select a valid user and ammo group"
msgstr ""

View File

@ -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:88
#: lib/cannery_web/live/ammo_group_live/form_component.ex:105
msgid "Ammo updated successfully"
msgstr "Munitionsgruppe erfolgreich aktualisiert"
#, elixir-autogen, elixir-format, fuzzy
#: lib/cannery_web/live/ammo_group_live/form_component.ex:147
#: lib/cannery_web/live/ammo_group_live/form_component.ex:164
msgid "Ammo added successfully"
msgid_plural "Ammo added successfully"
msgstr[0] "Munitionsgruppe erfolgreich aktualisiert"

View File

@ -139,13 +139,13 @@ msgid "Tag could not be added"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:102
#: lib/cannery/activity_log/shot_group.ex:115
msgid "Count must be at least 1"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
#: lib/cannery/activity_log/shot_group.ex:74
#: lib/cannery/activity_log/shot_group.ex:111
msgid "Count must be less than %{count}"
msgstr ""
@ -161,11 +161,26 @@ msgid "Tag could not be removed"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:126
#: lib/cannery_web/live/ammo_group_live/form_component.ex:143
msgid "Could not parse number of copies"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:111
#: lib/cannery_web/live/ammo_group_live/form_component.ex:128
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo.ex:407
msgid "Invalid multiplier"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo/ammo_group.ex:84
msgid "Please select an ammo type and container"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:69
msgid "Please select a valid user and ammo group"
msgstr ""

View File

@ -263,12 +263,12 @@ msgid "Ammo unstaged succesfully"
msgstr ""
#, elixir-autogen, elixir-format, fuzzy
#: lib/cannery_web/live/ammo_group_live/form_component.ex:88
#: lib/cannery_web/live/ammo_group_live/form_component.ex:105
msgid "Ammo updated successfully"
msgstr ""
#, elixir-autogen, elixir-format, fuzzy
#: lib/cannery_web/live/ammo_group_live/form_component.ex:147
#: lib/cannery_web/live/ammo_group_live/form_component.ex:164
msgid "Ammo added successfully"
msgid_plural "Ammo added successfully"
msgstr[0] ""

View File

@ -138,13 +138,13 @@ msgid "Tag could not be added"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:102
#: lib/cannery/activity_log/shot_group.ex:115
msgid "Count must be at least 1"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
#: lib/cannery/activity_log/shot_group.ex:74
#: lib/cannery/activity_log/shot_group.ex:111
msgid "Count must be less than %{count}"
msgstr ""
@ -160,11 +160,26 @@ msgid "Tag could not be removed"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:126
#: lib/cannery_web/live/ammo_group_live/form_component.ex:143
msgid "Could not parse number of copies"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:111
#: lib/cannery_web/live/ammo_group_live/form_component.ex:128
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo.ex:407
msgid "Invalid multiplier"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo/ammo_group.ex:84
msgid "Please select an ammo type and container"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:69
msgid "Please select a valid user and ammo group"
msgstr ""

View File

@ -149,13 +149,13 @@ msgid "Tag could not be added"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:102
#: lib/cannery/activity_log/shot_group.ex:115
msgid "Count must be at least 1"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
#: lib/cannery/activity_log/shot_group.ex:74
#: lib/cannery/activity_log/shot_group.ex:111
msgid "Count must be less than %{count}"
msgstr ""
@ -171,11 +171,26 @@ msgid "Tag could not be removed"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:126
#: lib/cannery_web/live/ammo_group_live/form_component.ex:143
msgid "Could not parse number of copies"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:111
#: lib/cannery_web/live/ammo_group_live/form_component.ex:128
msgid "Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo.ex:407
msgid "Invalid multiplier"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo/ammo_group.ex:84
msgid "Please select an ammo type and container"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:69
msgid "Please select a valid user and ammo group"
msgstr ""

View File

@ -273,12 +273,12 @@ msgid "Ammo unstaged succesfully"
msgstr ""
#, elixir-autogen, elixir-format, fuzzy
#: lib/cannery_web/live/ammo_group_live/form_component.ex:88
#: lib/cannery_web/live/ammo_group_live/form_component.ex:105
msgid "Ammo updated successfully"
msgstr ""
#, elixir-autogen, elixir-format, fuzzy
#: lib/cannery_web/live/ammo_group_live/form_component.ex:147
#: lib/cannery_web/live/ammo_group_live/form_component.ex:164
msgid "Ammo added successfully"
msgid_plural "Ammo added successfully"
msgstr[0] ""

View File

@ -153,13 +153,13 @@ msgid "Tag could not be added"
msgstr "Le tag na pas pu être ajouté"
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:102
#: lib/cannery/activity_log/shot_group.ex:115
msgid "Count must be at least 1"
msgstr "Le nombre doit être au moins égal à 1"
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:61
#: lib/cannery/activity_log/shot_group.ex:98
#: lib/cannery/activity_log/shot_group.ex:74
#: lib/cannery/activity_log/shot_group.ex:111
msgid "Count must be less than %{count}"
msgstr "La quantité doit être inférieur à %{count}"
@ -177,11 +177,26 @@ 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:126
#: lib/cannery_web/live/ammo_group_live/form_component.ex:143
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:111
#: lib/cannery_web/live/ammo_group_live/form_component.ex:128
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:407
msgid "Invalid multiplier"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/ammo/ammo_group.ex:84
msgid "Please select an ammo type and container"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery/activity_log/shot_group.ex:69
msgid "Please select a valid user and ammo group"
msgstr ""

View File

@ -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:88
#: lib/cannery_web/live/ammo_group_live/form_component.ex:105
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:147
#: lib/cannery_web/live/ammo_group_live/form_component.ex:164
msgid "Ammo added successfully"
msgid_plural "Ammo added successfully"
msgstr[0] "Groupe de munition mis à jour avec succès"

View File

@ -262,12 +262,12 @@ msgid "Ammo unstaged succesfully"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:88
#: lib/cannery_web/live/ammo_group_live/form_component.ex:105
msgid "Ammo updated successfully"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_group_live/form_component.ex:147
#: lib/cannery_web/live/ammo_group_live/form_component.ex:164
msgid "Ammo added successfully"
msgid_plural "Ammo added successfully"
msgstr[0] ""

View File

@ -180,9 +180,5 @@ defmodule Cannery.AmmoTest do
Ammo.get_ammo_group!(ammo_group.id, current_user)
end
end
test "change_ammo_group/1 returns a ammo_group changeset", %{ammo_group: ammo_group} do
assert %Changeset{} = Ammo.change_ammo_group(ammo_group)
end
end
end