From 7f9e6f9eff47244302983a447d6aaf7074c291bb Mon Sep 17 00:00:00 2001 From: shibao Date: Tue, 19 Apr 2022 20:08:10 -0400 Subject: [PATCH] fix tests --- lib/cannery/ammo.ex | 4 +- lib/cannery_web/live/ammo_type_live/index.ex | 103 +++++++++---------- 2 files changed, 51 insertions(+), 56 deletions(-) diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index 00988b6..ea9c853 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -5,8 +5,8 @@ defmodule Cannery.Ammo do import Ecto.Query, warn: false alias Cannery.{Accounts.User, Containers, Repo} - alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Cannery.ActivityLog.ShotGroup + alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Ecto.Changeset @ammo_group_create_limit 10_000 @@ -45,8 +45,6 @@ defmodule Cannery.Ammo do @doc """ Gets the average cost of a single ammo type - Raises `Ecto.NoResultsError` if the Ammo type does not exist. - ## Examples iex> get_average_cost_for_ammo_type!(%AmmoType{id: 123}, %User{id: 123}) diff --git a/lib/cannery_web/live/ammo_type_live/index.ex b/lib/cannery_web/live/ammo_type_live/index.ex index c02bb1d..2fbe1e8 100644 --- a/lib/cannery_web/live/ammo_type_live/index.ex +++ b/lib/cannery_web/live/ammo_type_live/index.ex @@ -96,61 +96,58 @@ defmodule CanneryWeb.AmmoTypeLive.Index do end defp get_ammo_type_values(ammo_type, columns, current_user) do - assigns = %{ammo_type: ammo_type} - columns |> Enum.into(%{}, fn %{key: key, type: type} -> - value = - case type do - :boolean -> - ammo_type |> Map.get(key |> String.to_existing_atom()) |> humanize() - - :round_count -> - ammo_type |> Ammo.get_round_count_for_ammo_type(current_user) - - :avg_price_paid -> - - case ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) do - nil -> gettext("No cost information") - count -> gettext("$%{amount}", amount: count |> :erlang.float_to_binary(decimals: 2)) - end - - :actions -> - ~H""" -
- <%= live_redirect to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type), - class: "text-primary-600 link", - data: [qa: "view-#{ammo_type.id}"] do %> - - <% end %> - - <%= live_patch to: Routes.ammo_type_index_path(Endpoint, :edit, ammo_type), - class: "text-primary-600 link", - data: [qa: "edit-#{ammo_type.id}"] do %> - - <% end %> - - <%= link to: "#", - class: "text-primary-600 link", - phx_click: "delete", - phx_value_id: ammo_type.id, - data: [ - confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"), - qa: "delete-#{ammo_type.id}" - ] do %> - - <% end %> -
- """ - - nil -> - nil - - _other -> - ammo_type |> Map.get(key |> String.to_existing_atom()) - end - - {key, value} + {key, get_ammo_type_value(type, key, ammo_type, current_user)} end) end + + defp get_ammo_type_value(:boolean, key, ammo_type, _current_user), + do: ammo_type |> Map.get(key |> String.to_existing_atom()) |> humanize() + + defp get_ammo_type_value(:round_count, _key, ammo_type, current_user), + do: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user) + + defp get_ammo_type_value(:avg_price_paid, _key, ammo_type, current_user) do + case ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) do + nil -> gettext("No cost information") + count -> gettext("$%{amount}", amount: count |> :erlang.float_to_binary(decimals: 2)) + end + end + + defp get_ammo_type_value(:actions, _key, ammo_type, _current_user) do + assigns = %{ammo_type: ammo_type} + + ~H""" +
+ <%= live_redirect to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type), + class: "text-primary-600 link", + data: [qa: "view-#{ammo_type.id}"] do %> + + <% end %> + + <%= live_patch to: Routes.ammo_type_index_path(Endpoint, :edit, ammo_type), + class: "text-primary-600 link", + data: [qa: "edit-#{ammo_type.id}"] do %> + + <% end %> + + <%= link to: "#", + class: "text-primary-600 link", + phx_click: "delete", + phx_value_id: ammo_type.id, + data: [ + confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"), + qa: "delete-#{ammo_type.id}" + ] do %> + + <% end %> +
+ """ + end + + defp get_ammo_type_value(nil, _key, _ammo_type, _current_user), do: nil + + defp get_ammo_type_value(_other, key, ammo_type, _current_user), + do: ammo_type |> Map.get(key |> String.to_existing_atom()) end