harden tag changesets

This commit is contained in:
shibao 2022-07-04 21:06:35 -04:00
parent 36f56528ee
commit ee6266be3f
19 changed files with 70 additions and 62 deletions

View File

@ -74,8 +74,8 @@ defmodule Cannery.Tags do
"""
@spec create_tag(attrs :: map(), User.t()) ::
{:ok, Tag.t()} | {:error, Changeset.t(Tag.new_tag())}
def create_tag(attrs, %User{id: user_id}),
do: %Tag{} |> Tag.create_changeset(attrs |> Map.put("user_id", user_id)) |> Repo.insert()
def create_tag(attrs, %User{} = user),
do: %Tag{} |> Tag.create_changeset(user, attrs) |> Repo.insert()
@doc """
Updates a tag.
@ -121,20 +121,6 @@ defmodule Cannery.Tags do
@spec delete_tag!(Tag.t(), User.t()) :: Tag.t()
def delete_tag!(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete!()
@doc """
Returns an `%Changeset{}` for tracking tag changes.
## Examples
iex> change_tag(tag)
%Changeset{data: %Tag{}}
"""
@spec change_tag(Tag.t() | Tag.new_tag()) :: Changeset.t(Tag.t() | Tag.new_tag())
@spec change_tag(Tag.t() | Tag.new_tag(), attrs :: map()) ::
Changeset.t(Tag.t() | Tag.new_tag())
def change_tag(tag, attrs \\ %{}), do: Tag.update_changeset(tag, attrs)
@doc """
Get a random tag bg_color in `#ffffff` hex format

View File

@ -35,10 +35,11 @@ defmodule Cannery.Tags.Tag do
@type id() :: UUID.t()
@doc false
@spec create_changeset(new_tag(), attrs :: map()) :: Changeset.t(new_tag())
def create_changeset(tag, attrs) do
@spec create_changeset(new_tag(), User.t(), attrs :: map()) :: Changeset.t(new_tag())
def create_changeset(tag, %User{id: user_id}, attrs) do
tag
|> cast(attrs, [:name, :bg_color, :text_color, :user_id])
|> change(user_id: user_id)
|> cast(attrs, [:name, :bg_color, :text_color])
|> validate_required([:name, :bg_color, :text_color, :user_id])
end

View File

@ -12,19 +12,44 @@ defmodule CanneryWeb.TagLive.FormComponent do
@impl true
@spec update(%{:tag => Tag.t(), :current_user => User.t(), optional(any) => any}, Socket.t()) ::
{:ok, Socket.t()}
def update(%{tag: tag} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign(:changeset, Tags.change_tag(tag))}
def update(%{tag: _tag} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
end
@impl true
def handle_event("validate", %{"tag" => tag_params}, %{assigns: %{tag: tag}} = socket) do
{:noreply, socket |> assign(:changeset, tag |> Tags.change_tag(tag_params))}
def handle_event("validate", %{"tag" => tag_params}, socket) do
{:noreply, socket |> assign_changeset(tag_params)}
end
def handle_event("save", %{"tag" => tag_params}, %{assigns: %{action: action}} = socket) do
save_tag(socket, action, tag_params)
end
defp assign_changeset(
%{assigns: %{action: action, current_user: user, tag: tag}} = socket,
tag_params
) do
changeset_action =
case action do
:new -> :insert
:edit -> :update
end
changeset =
case action do
:new -> tag |> Tag.create_changeset(user, tag_params)
:edit -> tag |> Tag.update_changeset(tag_params)
end
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
socket |> assign(:changeset, changeset)
end
@impl true
def render(assigns) do
~H"""

View File

@ -126,7 +126,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:50
#: lib/cannery_web/live/invite_live/form_component.html.heex:28
#: lib/cannery_web/live/range_live/form_component.html.heex:40
#: lib/cannery_web/live/tag_live/form_component.ex:66
#: lib/cannery_web/live/tag_live/form_component.ex:91
msgid "Save"
msgstr ""

View File

@ -139,7 +139,7 @@ msgstr "Passwort zurücksetzen"
#: lib/cannery_web/live/container_live/form_component.html.heex:50
#: lib/cannery_web/live/invite_live/form_component.html.heex:28
#: lib/cannery_web/live/range_live/form_component.html.heex:40
#: lib/cannery_web/live/tag_live/form_component.ex:66
#: lib/cannery_web/live/tag_live/form_component.ex:91
msgid "Save"
msgstr "Speichern"

View File

@ -65,7 +65,7 @@ msgid "Average Price paid"
msgstr "Durchschnittlicher Kaufpreis"
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:54
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "Background color"
msgstr "Hintergrundfarbe"
@ -287,7 +287,7 @@ msgstr "Meine coole Munitionskiste"
#: lib/cannery_web/live/ammo_type_live/index.ex:51
#: lib/cannery_web/live/container_live/form_component.html.heex:20
#: lib/cannery_web/live/invite_live/form_component.html.heex:20
#: lib/cannery_web/live/tag_live/form_component.ex:50
#: lib/cannery_web/live/tag_live/form_component.ex:75
msgid "Name"
msgstr "Name"
@ -448,7 +448,7 @@ msgid "Tags can be added to your containers to help you organize"
msgstr "Tags können zur besseren Ordnung einem Behälter hinzugefügt werden"
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:60
#: lib/cannery_web/live/tag_live/form_component.ex:85
msgid "Text color"
msgstr "Textfarbe"

View File

@ -27,7 +27,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:85
#: 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:126
msgid "%{name} created successfully"
msgstr "%{name} erfolgreich erstellt"
@ -65,7 +65,7 @@ msgstr "%{name} erfolgreich aktualisiert"
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:67
#: 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:108
msgid "%{name} updated successfully"
msgstr "%{name} erfolgreich aktualisiert"
@ -173,7 +173,7 @@ msgstr "Registrieren Sie sich, um %{name} zu bearbeiten"
#: lib/cannery_web/live/container_live/form_component.html.heex:52
#: lib/cannery_web/live/invite_live/form_component.html.heex:30
#: lib/cannery_web/live/range_live/form_component.html.heex:42
#: lib/cannery_web/live/tag_live/form_component.ex:68
#: lib/cannery_web/live/tag_live/form_component.ex:93
msgid "Saving..."
msgstr "Speichere..."

View File

@ -50,7 +50,7 @@ msgid "Average Price paid"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:54
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "Background color"
msgstr ""
@ -272,7 +272,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/index.ex:51
#: lib/cannery_web/live/container_live/form_component.html.heex:20
#: lib/cannery_web/live/invite_live/form_component.html.heex:20
#: lib/cannery_web/live/tag_live/form_component.ex:50
#: lib/cannery_web/live/tag_live/form_component.ex:75
msgid "Name"
msgstr ""
@ -431,7 +431,7 @@ msgid "Tags can be added to your containers to help you organize"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:60
#: lib/cannery_web/live/tag_live/form_component.ex:85
msgid "Text color"
msgstr ""

View File

@ -127,7 +127,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:50
#: lib/cannery_web/live/invite_live/form_component.html.heex:28
#: lib/cannery_web/live/range_live/form_component.html.heex:40
#: lib/cannery_web/live/tag_live/form_component.ex:66
#: lib/cannery_web/live/tag_live/form_component.ex:91
msgid "Save"
msgstr ""

View File

@ -51,7 +51,7 @@ msgid "Average Price paid"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:54
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "Background color"
msgstr ""
@ -273,7 +273,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/index.ex:51
#: lib/cannery_web/live/container_live/form_component.html.heex:20
#: lib/cannery_web/live/invite_live/form_component.html.heex:20
#: lib/cannery_web/live/tag_live/form_component.ex:50
#: lib/cannery_web/live/tag_live/form_component.ex:75
msgid "Name"
msgstr ""
@ -432,7 +432,7 @@ msgid "Tags can be added to your containers to help you organize"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:60
#: lib/cannery_web/live/tag_live/form_component.ex:85
msgid "Text color"
msgstr ""

View File

@ -15,7 +15,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:85
#: 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:126
msgid "%{name} created successfully"
msgstr ""
@ -53,7 +53,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:67
#: 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:108
msgid "%{name} updated successfully"
msgstr ""
@ -155,7 +155,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:52
#: lib/cannery_web/live/invite_live/form_component.html.heex:30
#: lib/cannery_web/live/range_live/form_component.html.heex:42
#: lib/cannery_web/live/tag_live/form_component.ex:68
#: lib/cannery_web/live/tag_live/form_component.ex:93
msgid "Saving..."
msgstr ""

View File

@ -137,7 +137,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:50
#: lib/cannery_web/live/invite_live/form_component.html.heex:28
#: lib/cannery_web/live/range_live/form_component.html.heex:40
#: lib/cannery_web/live/tag_live/form_component.ex:66
#: lib/cannery_web/live/tag_live/form_component.ex:91
msgid "Save"
msgstr ""

View File

@ -61,7 +61,7 @@ msgid "Average Price paid"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:54
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "Background color"
msgstr ""
@ -283,7 +283,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/index.ex:51
#: lib/cannery_web/live/container_live/form_component.html.heex:20
#: lib/cannery_web/live/invite_live/form_component.html.heex:20
#: lib/cannery_web/live/tag_live/form_component.ex:50
#: lib/cannery_web/live/tag_live/form_component.ex:75
msgid "Name"
msgstr ""
@ -442,7 +442,7 @@ msgid "Tags can be added to your containers to help you organize"
msgstr ""
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:60
#: lib/cannery_web/live/tag_live/form_component.ex:85
msgid "Text color"
msgstr ""

View File

@ -25,7 +25,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:85
#: 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:126
msgid "%{name} created successfully"
msgstr ""
@ -63,7 +63,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:67
#: 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:108
msgid "%{name} updated successfully"
msgstr ""
@ -165,7 +165,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:52
#: lib/cannery_web/live/invite_live/form_component.html.heex:30
#: lib/cannery_web/live/range_live/form_component.html.heex:42
#: lib/cannery_web/live/tag_live/form_component.ex:68
#: lib/cannery_web/live/tag_live/form_component.ex:93
msgid "Saving..."
msgstr ""

View File

@ -139,7 +139,7 @@ msgstr "Réinitialisé le mot de passe"
#: lib/cannery_web/live/container_live/form_component.html.heex:50
#: lib/cannery_web/live/invite_live/form_component.html.heex:28
#: lib/cannery_web/live/range_live/form_component.html.heex:40
#: lib/cannery_web/live/tag_live/form_component.ex:66
#: lib/cannery_web/live/tag_live/form_component.ex:91
msgid "Save"
msgstr "Sauvegarder"

View File

@ -65,7 +65,7 @@ msgid "Average Price paid"
msgstr "Prix acheté moyen"
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:54
#: lib/cannery_web/live/tag_live/form_component.ex:79
msgid "Background color"
msgstr "Couleur de fond"
@ -287,7 +287,7 @@ msgstr "Ma superbe boite de munition"
#: lib/cannery_web/live/ammo_type_live/index.ex:51
#: lib/cannery_web/live/container_live/form_component.html.heex:20
#: lib/cannery_web/live/invite_live/form_component.html.heex:20
#: lib/cannery_web/live/tag_live/form_component.ex:50
#: lib/cannery_web/live/tag_live/form_component.ex:75
msgid "Name"
msgstr "Nom"
@ -450,7 +450,7 @@ msgstr ""
"organiser"
#, elixir-autogen, elixir-format
#: lib/cannery_web/live/tag_live/form_component.ex:60
#: lib/cannery_web/live/tag_live/form_component.ex:85
msgid "Text color"
msgstr "Couleur du texte"

View File

@ -27,7 +27,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:85
#: 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:126
msgid "%{name} created successfully"
msgstr "%{name} créé· avec succès"
@ -65,7 +65,7 @@ msgstr "%{name} mis à jour avec succès"
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:67
#: 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:108
msgid "%{name} updated successfully"
msgstr "%{name} mis à jour avec succès"
@ -174,7 +174,7 @@ msgstr "Senregistrer pour mettre en place %{name}"
#: lib/cannery_web/live/container_live/form_component.html.heex:52
#: lib/cannery_web/live/invite_live/form_component.html.heex:30
#: lib/cannery_web/live/range_live/form_component.html.heex:42
#: lib/cannery_web/live/tag_live/form_component.ex:68
#: lib/cannery_web/live/tag_live/form_component.ex:93
msgid "Saving..."
msgstr "Sauvegarde en cours…"

View File

@ -14,7 +14,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:85
#: 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:126
msgid "%{name} created successfully"
msgstr ""
@ -52,7 +52,7 @@ msgstr ""
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:67
#: 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:108
msgid "%{name} updated successfully"
msgstr ""
@ -154,7 +154,7 @@ msgstr ""
#: lib/cannery_web/live/container_live/form_component.html.heex:52
#: lib/cannery_web/live/invite_live/form_component.html.heex:30
#: lib/cannery_web/live/range_live/form_component.html.heex:42
#: lib/cannery_web/live/tag_live/form_component.ex:68
#: lib/cannery_web/live/tag_live/form_component.ex:93
msgid "Saving..."
msgstr ""

View File

@ -68,9 +68,5 @@ defmodule Cannery.TagsTest do
assert {:ok, %Tag{}} = Tags.delete_tag(tag, current_user)
assert_raise Ecto.NoResultsError, fn -> Tags.get_tag!(tag.id, current_user) end
end
test "change_tag/1 returns a tag changeset", %{tag: tag} do
assert %Changeset{} = Tags.change_tag(tag)
end
end
end