From c33f15603b53eac953b3f7831d2aa3f2e44a8181 Mon Sep 17 00:00:00 2001 From: shibao Date: Thu, 30 Mar 2023 21:53:52 -0400 Subject: [PATCH] rename ammo type to type --- CHANGELOG.md | 3 +- lib/cannery/activity_log.ex | 52 +- lib/cannery/ammo.ex | 404 ++++++----- lib/cannery/ammo/ammo_type.ex | 20 +- lib/cannery/ammo/pack.ex | 30 +- .../components/ammo_type_table_component.ex | 110 +-- .../core_components/pack_card.html.heex | 2 +- .../core_components/topbar.html.heex | 2 +- .../components/pack_table_component.ex | 24 +- .../components/shot_record_table_component.ex | 4 +- .../controllers/export_controller.ex | 32 +- .../live/ammo_type_live/form_component.ex | 47 +- .../ammo_type_live/form_component.html.heex | 4 +- lib/cannery_web/live/ammo_type_live/index.ex | 64 +- .../live/ammo_type_live/index.html.heex | 60 +- lib/cannery_web/live/ammo_type_live/show.ex | 60 +- .../live/ammo_type_live/show.html.heex | 43 +- lib/cannery_web/live/container_live/show.ex | 8 +- .../live/container_live/show.html.heex | 12 +- .../live/pack_live/form_component.ex | 28 +- .../live/pack_live/form_component.html.heex | 6 +- lib/cannery_web/live/pack_live/index.ex | 12 +- .../live/pack_live/index.html.heex | 16 +- lib/cannery_web/live/pack_live/show.html.heex | 4 +- lib/cannery_web/live/range_live/index.ex | 8 +- .../live/range_live/index.html.heex | 2 +- lib/cannery_web/router.ex | 14 +- priv/gettext/actions.pot | 66 +- priv/gettext/de/LC_MESSAGES/actions.po | 66 +- priv/gettext/de/LC_MESSAGES/default.po | 80 ++- priv/gettext/de/LC_MESSAGES/errors.po | 12 +- priv/gettext/de/LC_MESSAGES/prompts.po | 6 +- priv/gettext/default.pot | 80 ++- priv/gettext/en/LC_MESSAGES/actions.po | 66 +- priv/gettext/en/LC_MESSAGES/default.po | 80 ++- priv/gettext/en/LC_MESSAGES/errors.po | 12 +- priv/gettext/en/LC_MESSAGES/prompts.po | 6 +- priv/gettext/errors.pot | 12 +- priv/gettext/es/LC_MESSAGES/actions.po | 66 +- priv/gettext/es/LC_MESSAGES/default.po | 80 ++- priv/gettext/es/LC_MESSAGES/errors.po | 12 +- priv/gettext/es/LC_MESSAGES/prompts.po | 6 +- priv/gettext/fr/LC_MESSAGES/actions.po | 66 +- priv/gettext/fr/LC_MESSAGES/default.po | 80 ++- priv/gettext/fr/LC_MESSAGES/errors.po | 12 +- priv/gettext/fr/LC_MESSAGES/prompts.po | 6 +- priv/gettext/ga/LC_MESSAGES/actions.po | 66 +- priv/gettext/ga/LC_MESSAGES/default.po | 80 ++- priv/gettext/ga/LC_MESSAGES/errors.po | 12 +- priv/gettext/ga/LC_MESSAGES/prompts.po | 6 +- priv/gettext/prompts.pot | 6 +- test/cannery/activity_log_test.exs | 112 ++- test/cannery/ammo_test.exs | 638 +++++++++--------- .../controllers/export_controller_test.exs | 68 +- test/cannery_web/live/ammo_type_live_test.exs | 236 +++---- test/cannery_web/live/container_live_test.exs | 60 +- test/cannery_web/live/pack_live_test.exs | 76 +-- test/cannery_web/live/range_live_test.exs | 26 +- test/support/fixtures.ex | 22 +- 59 files changed, 1616 insertions(+), 1677 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fba3a4..f70eba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v0.9.1 - Rename ammo type's "type" to "class" to avoid confusion -- Fixes ammo type search +- Rename "ammo type" to "type" to avoid confusion +- Fixes type search - Fixes shot records table disappearing after selecting an empty ammo class - Code quality improvements diff --git a/lib/cannery/activity_log.ex b/lib/cannery/activity_log.ex index 1b7aa1a..10eac3c 100644 --- a/lib/cannery/activity_log.ex +++ b/lib/cannery/activity_log.ex @@ -4,7 +4,7 @@ defmodule Cannery.ActivityLog do """ import Ecto.Query, warn: false - alias Cannery.Ammo.{AmmoType, Pack} + alias Cannery.Ammo.{Pack, Type} alias Cannery.{Accounts.User, ActivityLog.ShotRecord, Repo} alias Ecto.{Multi, Queryable} @@ -23,8 +23,8 @@ defmodule Cannery.ActivityLog do [%ShotRecord{notes: "Shot some rifle rounds"}, ...] """ - @spec list_shot_records(AmmoType.class() | :all, User.t()) :: [ShotRecord.t()] - @spec list_shot_records(search :: nil | String.t(), AmmoType.class() | :all, User.t()) :: + @spec list_shot_records(Type.class() | :all, User.t()) :: [ShotRecord.t()] + @spec list_shot_records(search :: nil | String.t(), Type.class() | :all, User.t()) :: [ShotRecord.t()] def list_shot_records(search \\ nil, type, %{id: user_id}) do from(sg in ShotRecord, @@ -32,9 +32,9 @@ defmodule Cannery.ActivityLog do left_join: ag in Pack, as: :ag, on: sg.pack_id == ag.id, - left_join: at in AmmoType, + left_join: at in Type, as: :at, - on: ag.ammo_type_id == at.id, + on: ag.type_id == at.id, where: sg.user_id == ^user_id, distinct: sg.id ) @@ -79,7 +79,7 @@ defmodule Cannery.ActivityLog do }) end - @spec list_shot_records_filter_type(Queryable.t(), AmmoType.class() | :all) :: + @spec list_shot_records_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t() defp list_shot_records_filter_type(query, :rifle), do: query |> where([at: at], at.class == :rifle) @@ -347,50 +347,50 @@ defmodule Cannery.ActivityLog do end @doc """ - Gets the total number of rounds shot for an ammo type + Gets the total number of rounds shot for a type - Raises `Ecto.NoResultsError` if the Ammo type does not exist. + Raises `Ecto.NoResultsError` if the type does not exist. ## Examples - iex> get_used_count_for_ammo_type(123, %User{id: 123}) + iex> get_used_count_for_type(123, %User{id: 123}) 35 - iex> get_used_count_for_ammo_type(456, %User{id: 123}) + iex> get_used_count_for_type(456, %User{id: 123}) ** (Ecto.NoResultsError) """ - @spec get_used_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer() - def get_used_count_for_ammo_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do - [ammo_type] - |> get_used_count_for_ammo_types(user) - |> Map.get(ammo_type_id, 0) + @spec get_used_count_for_type(Type.t(), User.t()) :: non_neg_integer() + def get_used_count_for_type(%Type{id: type_id} = type, user) do + [type] + |> get_used_count_for_types(user) + |> Map.get(type_id, 0) end @doc """ - Gets the total number of rounds shot for multiple ammo types + Gets the total number of rounds shot for multiple types ## Examples - iex> get_used_count_for_ammo_types(123, %User{id: 123}) + iex> get_used_count_for_types(123, %User{id: 123}) 35 """ - @spec get_used_count_for_ammo_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - def get_used_count_for_ammo_types(ammo_types, %User{id: user_id}) do - ammo_type_ids = - ammo_types - |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) + @spec get_used_count_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => non_neg_integer()} + def get_used_count_for_types(types, %User{id: user_id}) do + type_ids = + types + |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) Repo.all( from ag in Pack, left_join: sg in ShotRecord, on: ag.id == sg.pack_id, - where: ag.ammo_type_id in ^ammo_type_ids, + where: ag.type_id in ^type_ids, where: not (sg.count |> is_nil()), - group_by: ag.ammo_type_id, - select: {ag.ammo_type_id, sum(sg.count)} + group_by: ag.type_id, + select: {ag.type_id, sum(sg.count)} ) |> Map.new() end diff --git a/lib/cannery/ammo.ex b/lib/cannery/ammo.ex index ff823ff..591a8c3 100644 --- a/lib/cannery/ammo.ex +++ b/lib/cannery/ammo.ex @@ -8,46 +8,46 @@ defmodule Cannery.Ammo do alias Cannery.{Accounts.User, Containers, Repo} alias Cannery.Containers.{Container, ContainerTag, Tag} alias Cannery.{ActivityLog, ActivityLog.ShotRecord} - alias Cannery.Ammo.{AmmoType, Pack} + alias Cannery.Ammo.{Pack, Type} alias Ecto.{Changeset, Queryable} @pack_create_limit 10_000 - @pack_preloads [:ammo_type] - @ammo_type_preloads [:packs] + @pack_preloads [:type] + @type_preloads [:packs] @doc """ - Returns the list of ammo_types. + Returns the list of types. ## Examples - iex> list_ammo_types(%User{id: 123}, :all) - [%AmmoType{}, ...] + iex> list_types(%User{id: 123}, :all) + [%Type{}, ...] - iex> list_ammo_types("cool", %User{id: 123}, :shotgun) - [%AmmoType{name: "My cool ammo type", class: :shotgun}, ...] + iex> list_types("cool", %User{id: 123}, :shotgun) + [%Type{name: "My cool type", class: :shotgun}, ...] """ - @spec list_ammo_types(User.t(), AmmoType.class() | :all) :: [AmmoType.t()] - @spec list_ammo_types(search :: nil | String.t(), User.t(), AmmoType.class() | :all) :: - [AmmoType.t()] - def list_ammo_types(search \\ nil, user, type) + @spec list_types(User.t(), Type.class() | :all) :: [Type.t()] + @spec list_types(search :: nil | String.t(), User.t(), Type.class() | :all) :: + [Type.t()] + def list_types(search \\ nil, user, type) - def list_ammo_types(search, %{id: user_id}, type) do - from(at in AmmoType, + def list_types(search, %{id: user_id}, type) do + from(at in Type, as: :at, where: at.user_id == ^user_id, - preload: ^@ammo_type_preloads + preload: ^@type_preloads ) - |> list_ammo_types_filter_type(type) - |> list_ammo_types_filter_search(search) + |> list_types_filter_type(type) + |> list_types_filter_search(search) |> Repo.all() end - @spec list_ammo_types_filter_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t() - defp list_ammo_types_filter_search(query, search) when search in ["", nil], + @spec list_types_filter_search(Queryable.t(), search :: String.t() | nil) :: Queryable.t() + defp list_types_filter_search(query, search) when search in ["", nil], do: query |> order_by([at: at], at.name) - defp list_ammo_types_filter_search(query, search) when search |> is_binary() do + defp list_types_filter_search(query, search) when search |> is_binary() do trimmed_search = String.trim(search) query @@ -72,31 +72,31 @@ defmodule Cannery.Ammo do ) end - @spec list_ammo_types_filter_type(Queryable.t(), AmmoType.class() | :all) :: Queryable.t() - defp list_ammo_types_filter_type(query, :rifle), + @spec list_types_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t() + defp list_types_filter_type(query, :rifle), do: query |> where([at: at], at.class == :rifle) - defp list_ammo_types_filter_type(query, :pistol), + defp list_types_filter_type(query, :pistol), do: query |> where([at: at], at.class == :pistol) - defp list_ammo_types_filter_type(query, :shotgun), + defp list_types_filter_type(query, :shotgun), do: query |> where([at: at], at.class == :shotgun) - defp list_ammo_types_filter_type(query, _all), do: query + defp list_types_filter_type(query, _all), do: query @doc """ - Returns a count of ammo_types. + Returns a count of types. ## Examples - iex> get_ammo_types_count!(%User{id: 123}) + iex> get_types_count!(%User{id: 123}) 3 """ - @spec get_ammo_types_count!(User.t()) :: integer() - def get_ammo_types_count!(%User{id: user_id}) do + @spec get_types_count!(User.t()) :: integer() + def get_types_count!(%User{id: user_id}) do Repo.one( - from at in AmmoType, + from at in Type, where: at.user_id == ^user_id, select: count(at.id), distinct: true @@ -104,67 +104,67 @@ defmodule Cannery.Ammo do end @doc """ - Gets a single ammo_type. + Gets a single type. - Raises `Ecto.NoResultsError` if the Ammo type does not exist. + Raises `Ecto.NoResultsError` if the type does not exist. ## Examples - iex> get_ammo_type!(123, %User{id: 123}) - %AmmoType{} + iex> get_type!(123, %User{id: 123}) + %Type{} - iex> get_ammo_type!(456, %User{id: 123}) + iex> get_type!(456, %User{id: 123}) ** (Ecto.NoResultsError) """ - @spec get_ammo_type!(AmmoType.id(), User.t()) :: AmmoType.t() - def get_ammo_type!(id, %User{id: user_id}) do + @spec get_type!(Type.id(), User.t()) :: Type.t() + def get_type!(id, %User{id: user_id}) do Repo.one!( - from at in AmmoType, + from at in Type, where: at.id == ^id, where: at.user_id == ^user_id, - preload: ^@ammo_type_preloads + preload: ^@type_preloads ) end @doc """ - Gets the average cost of an ammo type from packs with price information. + Gets the average cost of a type from packs with price information. ## Examples - iex> get_average_cost_for_ammo_type( - ...> %AmmoType{id: 123, user_id: 456}, + iex> get_average_cost_for_type( + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) 1.50 """ - @spec get_average_cost_for_ammo_type(AmmoType.t(), User.t()) :: float() | nil - def get_average_cost_for_ammo_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do - [ammo_type] - |> get_average_cost_for_ammo_types(user) - |> Map.get(ammo_type_id) + @spec get_average_cost_for_type(Type.t(), User.t()) :: float() | nil + def get_average_cost_for_type(%Type{id: type_id} = type, user) do + [type] + |> get_average_cost_for_types(user) + |> Map.get(type_id) end @doc """ - Gets the average cost of ammo types from packs with price information - for multiple ammo types. + Gets the average cost of types from packs with price information + for multiple types. ## Examples - iex> get_average_cost_for_ammo_types( - ...> [%AmmoType{id: 123, user_id: 456}], + iex> get_average_cost_for_types( + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456} ...> ) 1.50 """ - @spec get_average_cost_for_ammo_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => float()} - def get_average_cost_for_ammo_types(ammo_types, %User{id: user_id}) do - ammo_type_ids = - ammo_types - |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) + @spec get_average_cost_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => float()} + def get_average_cost_for_types(types, %User{id: user_id}) do + type_ids = + types + |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) sg_total_query = from sg in ShotRecord, @@ -177,202 +177,200 @@ defmodule Cannery.Ammo do as: :pack, left_join: sg_query in subquery(sg_total_query), on: ag.id == sg_query.pack_id, - where: ag.ammo_type_id in ^ammo_type_ids, - group_by: ag.ammo_type_id, + where: ag.type_id in ^type_ids, + group_by: ag.type_id, where: not (ag.price_paid |> is_nil()), - select: - {ag.ammo_type_id, sum(ag.price_paid) / sum(ag.count + coalesce(sg_query.total, 0))} + select: {ag.type_id, sum(ag.price_paid) / sum(ag.count + coalesce(sg_query.total, 0))} ) |> Map.new() end @doc """ - Gets the total number of rounds for an ammo type + Gets the total number of rounds for a type ## Examples - iex> get_round_count_for_ammo_type( - ...> %AmmoType{id: 123, user_id: 456}, + iex> get_round_count_for_type( + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) 35 """ - @spec get_round_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer() - def get_round_count_for_ammo_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do - [ammo_type] - |> get_round_count_for_ammo_types(user) - |> Map.get(ammo_type_id, 0) + @spec get_round_count_for_type(Type.t(), User.t()) :: non_neg_integer() + def get_round_count_for_type(%Type{id: type_id} = type, user) do + [type] + |> get_round_count_for_types(user) + |> Map.get(type_id, 0) end @doc """ - Gets the total number of rounds for multiple ammo types + Gets the total number of rounds for multiple types ## Examples - iex> get_round_count_for_ammo_types( - ...> [%AmmoType{id: 123, user_id: 456}], + iex> get_round_count_for_types( + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456} ...> ) %{123 => 35} """ - @spec get_round_count_for_ammo_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - def get_round_count_for_ammo_types(ammo_types, %User{id: user_id}) do - ammo_type_ids = - ammo_types - |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) + @spec get_round_count_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => non_neg_integer()} + def get_round_count_for_types(types, %User{id: user_id}) do + type_ids = + types + |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) Repo.all( from ag in Pack, - where: ag.ammo_type_id in ^ammo_type_ids, + where: ag.type_id in ^type_ids, where: ag.user_id == ^user_id, - group_by: ag.ammo_type_id, - select: {ag.ammo_type_id, sum(ag.count)} + group_by: ag.type_id, + select: {ag.type_id, sum(ag.count)} ) |> Map.new() end @doc """ - Gets the total number of ammo ever bought for an ammo type + Gets the total number of ammo ever bought for a type ## Examples - iex> get_historical_count_for_ammo_type( - ...> %AmmoType{id: 123, user_id: 456}, + iex> get_historical_count_for_type( + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) 5 """ - @spec get_historical_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer() - def get_historical_count_for_ammo_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do - [ammo_type] - |> get_historical_count_for_ammo_types(user) - |> Map.get(ammo_type_id, 0) + @spec get_historical_count_for_type(Type.t(), User.t()) :: non_neg_integer() + def get_historical_count_for_type(%Type{id: type_id} = type, user) do + [type] + |> get_historical_count_for_types(user) + |> Map.get(type_id, 0) end @doc """ - Gets the total number of ammo ever bought for multiple ammo types + Gets the total number of ammo ever bought for multiple types ## Examples - iex> get_historical_count_for_ammo_types( - ...> [%AmmoType{id: 123, user_id: 456}], + iex> get_historical_count_for_types( + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456} ...> ) %{123 => 5} """ - @spec get_historical_count_for_ammo_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - def get_historical_count_for_ammo_types(ammo_types, %User{id: user_id} = user) do - used_counts = ammo_types |> ActivityLog.get_used_count_for_ammo_types(user) - round_counts = ammo_types |> get_round_count_for_ammo_types(user) + @spec get_historical_count_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => non_neg_integer()} + def get_historical_count_for_types(types, %User{id: user_id} = user) do + used_counts = types |> ActivityLog.get_used_count_for_types(user) + round_counts = types |> get_round_count_for_types(user) - ammo_types - |> Enum.filter(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> - Map.has_key?(used_counts, ammo_type_id) or Map.has_key?(round_counts, ammo_type_id) + types + |> Enum.filter(fn %Type{id: type_id, user_id: ^user_id} -> + Map.has_key?(used_counts, type_id) or Map.has_key?(round_counts, type_id) end) - |> Map.new(fn %{id: ammo_type_id} -> - historical_count = - Map.get(used_counts, ammo_type_id, 0) + Map.get(round_counts, ammo_type_id, 0) + |> Map.new(fn %{id: type_id} -> + historical_count = Map.get(used_counts, type_id, 0) + Map.get(round_counts, type_id, 0) - {ammo_type_id, historical_count} + {type_id, historical_count} end) end @doc """ - Creates a ammo_type. + Creates a type. ## Examples - iex> create_ammo_type(%{field: value}, %User{id: 123}) - {:ok, %AmmoType{}} + iex> create_type(%{field: value}, %User{id: 123}) + {:ok, %Type{}} - iex> create_ammo_type(%{field: bad_value}, %User{id: 123}) + iex> create_type(%{field: bad_value}, %User{id: 123}) {:error, %Changeset{}} """ - @spec create_ammo_type(attrs :: map(), User.t()) :: - {:ok, AmmoType.t()} | {:error, AmmoType.changeset()} - def create_ammo_type(attrs \\ %{}, %User{} = user) do - %AmmoType{} - |> AmmoType.create_changeset(user, attrs) + @spec create_type(attrs :: map(), User.t()) :: + {:ok, Type.t()} | {:error, Type.changeset()} + def create_type(attrs \\ %{}, %User{} = user) do + %Type{} + |> Type.create_changeset(user, attrs) |> Repo.insert() |> case do - {:ok, ammo_type} -> {:ok, ammo_type |> preload_ammo_type()} + {:ok, type} -> {:ok, type |> preload_type()} {:error, changeset} -> {:error, changeset} end end - @spec preload_ammo_type(AmmoType.t()) :: AmmoType.t() - @spec preload_ammo_type([AmmoType.t()]) :: [AmmoType.t()] - defp preload_ammo_type(ammo_type_or_ammo_types) do - ammo_type_or_ammo_types |> Repo.preload(@ammo_type_preloads) + @spec preload_type(Type.t()) :: Type.t() + @spec preload_type([Type.t()]) :: [Type.t()] + defp preload_type(type_or_types) do + type_or_types |> Repo.preload(@type_preloads) end @doc """ - Updates a ammo_type. + Updates a type. ## Examples - iex> update_ammo_type(ammo_type, %{field: new_value}, %User{id: 123}) - {:ok, %AmmoType{}} + iex> update_type(type, %{field: new_value}, %User{id: 123}) + {:ok, %Type{}} - iex> update_ammo_type(ammo_type, %{field: bad_value}, %User{id: 123}) + iex> update_type(type, %{field: bad_value}, %User{id: 123}) {:error, %Changeset{}} """ - @spec update_ammo_type(AmmoType.t(), attrs :: map(), User.t()) :: - {:ok, AmmoType.t()} | {:error, AmmoType.changeset()} - def update_ammo_type(%AmmoType{user_id: user_id} = ammo_type, attrs, %User{id: user_id}) do - ammo_type - |> AmmoType.update_changeset(attrs) + @spec update_type(Type.t(), attrs :: map(), User.t()) :: + {:ok, Type.t()} | {:error, Type.changeset()} + def update_type(%Type{user_id: user_id} = type, attrs, %User{id: user_id}) do + type + |> Type.update_changeset(attrs) |> Repo.update() |> case do - {:ok, ammo_type} -> {:ok, ammo_type |> preload_ammo_type()} + {:ok, type} -> {:ok, type |> preload_type()} {:error, changeset} -> {:error, changeset} end end @doc """ - Deletes a ammo_type. + Deletes a type. ## Examples - iex> delete_ammo_type(ammo_type, %User{id: 123}) - {:ok, %AmmoType{}} + iex> delete_type(type, %User{id: 123}) + {:ok, %Type{}} - iex> delete_ammo_type(ammo_type, %User{id: 123}) + iex> delete_type(type, %User{id: 123}) {:error, %Changeset{}} """ - @spec delete_ammo_type(AmmoType.t(), User.t()) :: - {:ok, AmmoType.t()} | {:error, AmmoType.changeset()} - def delete_ammo_type(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}) do - ammo_type + @spec delete_type(Type.t(), User.t()) :: + {:ok, Type.t()} | {:error, Type.changeset()} + def delete_type(%Type{user_id: user_id} = type, %User{id: user_id}) do + type |> Repo.delete() |> case do - {:ok, ammo_type} -> {:ok, ammo_type |> preload_ammo_type()} + {:ok, type} -> {:ok, type |> preload_type()} {:error, changeset} -> {:error, changeset} end end @doc """ - Deletes a ammo_type. + Deletes a type. ## Examples - iex> delete_ammo_type!(ammo_type, %User{id: 123}) - %AmmoType{} + iex> delete_type!(type, %User{id: 123}) + %Type{} """ - @spec delete_ammo_type!(AmmoType.t(), User.t()) :: AmmoType.t() - def delete_ammo_type!(ammo_type, user) do - {:ok, ammo_type} = delete_ammo_type(ammo_type, user) - ammo_type + @spec delete_type!(Type.t(), User.t()) :: Type.t() + def delete_type!(type, user) do + {:ok, type} = delete_type(type, user) + type end @doc """ @@ -381,32 +379,32 @@ defmodule Cannery.Ammo do ## Examples iex> list_packs_for_type( - ...> %AmmoType{id: 123, user_id: 456}, + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) [%Pack{}, ...] iex> list_packs_for_type( - ...> %AmmoType{id: 123, user_id: 456}, + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456}, ...> true ...> ) [%Pack{}, %Pack{}, ...] """ - @spec list_packs_for_type(AmmoType.t(), User.t()) :: [Pack.t()] - @spec list_packs_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: + @spec list_packs_for_type(Type.t(), User.t()) :: [Pack.t()] + @spec list_packs_for_type(Type.t(), User.t(), show_used :: boolean()) :: [Pack.t()] - def list_packs_for_type(ammo_type, user, show_used \\ false) + def list_packs_for_type(type, user, show_used \\ false) def list_packs_for_type( - %AmmoType{id: ammo_type_id, user_id: user_id}, + %Type{id: type_id, user_id: user_id}, %User{id: user_id}, show_used ) do from(ag in Pack, as: :ag, - where: ag.ammo_type_id == ^ammo_type_id, + where: ag.type_id == ^type_id, where: ag.user_id == ^user_id, preload: ^@pack_preloads ) @@ -443,7 +441,7 @@ defmodule Cannery.Ammo do """ @spec list_packs_for_container( Container.t(), - AmmoType.t() | :all, + Type.t() | :all, User.t() ) :: [Pack.t()] def list_packs_for_container( @@ -453,7 +451,7 @@ defmodule Cannery.Ammo do ) do from(ag in Pack, as: :ag, - join: at in assoc(ag, :ammo_type), + join: at in assoc(ag, :type), as: :at, where: ag.container_id == ^container_id, where: ag.user_id == ^user_id, @@ -464,7 +462,7 @@ defmodule Cannery.Ammo do |> Repo.all() end - @spec list_packs_for_container_filter_type(Queryable.t(), AmmoType.class() | :all) :: + @spec list_packs_for_container_filter_type(Queryable.t(), Type.class() | :all) :: Queryable.t() defp list_packs_for_container_filter_type(query, :rifle), do: query |> where([at: at], at.class == :rifle) @@ -509,71 +507,71 @@ defmodule Cannery.Ammo do defp get_packs_count_show_used(query, _true), do: query @doc """ - Returns the count of packs for an ammo type. + Returns the count of packs for a type. ## Examples iex> get_packs_count_for_type( - ...> %AmmoType{id: 123, user_id: 456}, + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) 3 iex> get_packs_count_for_type( - ...> %AmmoType{id: 123, user_id: 456}, + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456}, ...> true ...> ) 5 """ - @spec get_packs_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() - @spec get_packs_count_for_type(AmmoType.t(), User.t(), show_used :: boolean()) :: + @spec get_packs_count_for_type(Type.t(), User.t()) :: non_neg_integer() + @spec get_packs_count_for_type(Type.t(), User.t(), show_used :: boolean()) :: non_neg_integer() def get_packs_count_for_type( - %AmmoType{id: ammo_type_id} = ammo_type, + %Type{id: type_id} = type, user, show_used \\ false ) do - [ammo_type] + [type] |> get_packs_count_for_types(user, show_used) - |> Map.get(ammo_type_id, 0) + |> Map.get(type_id, 0) end @doc """ - Returns the count of packs for multiple ammo types. + Returns the count of packs for multiple types. ## Examples iex> get_packs_count_for_types( - ...> [%AmmoType{id: 123, user_id: 456}], + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456} ...> ) 3 iex> get_packs_count_for_types( - ...> [%AmmoType{id: 123, user_id: 456}], + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456}, ...> true ...> ) 5 """ - @spec get_packs_count_for_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - @spec get_packs_count_for_types([AmmoType.t()], User.t(), show_used :: boolean()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - def get_packs_count_for_types(ammo_types, %User{id: user_id}, show_used \\ false) do - ammo_type_ids = - ammo_types - |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) + @spec get_packs_count_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => non_neg_integer()} + @spec get_packs_count_for_types([Type.t()], User.t(), show_used :: boolean()) :: + %{optional(Type.id()) => non_neg_integer()} + def get_packs_count_for_types(types, %User{id: user_id}, show_used \\ false) do + type_ids = + types + |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) from(ag in Pack, as: :ag, where: ag.user_id == ^user_id, - where: ag.ammo_type_id in ^ammo_type_ids, - group_by: ag.ammo_type_id, - select: {ag.ammo_type_id, count(ag.id)} + where: ag.type_id in ^type_ids, + group_by: ag.type_id, + select: {ag.type_id, count(ag.id)} ) |> get_packs_count_for_types_maybe_show_used(show_used) |> Repo.all() @@ -589,50 +587,50 @@ defmodule Cannery.Ammo do end @doc """ - Returns the count of used packs for an ammo type. + Returns the count of used packs for a type. ## Examples iex> get_used_packs_count_for_type( - ...> %AmmoType{id: 123, user_id: 456}, + ...> %Type{id: 123, user_id: 456}, ...> %User{id: 456} ...> ) 3 """ - @spec get_used_packs_count_for_type(AmmoType.t(), User.t()) :: non_neg_integer() - def get_used_packs_count_for_type(%AmmoType{id: ammo_type_id} = ammo_type, user) do - [ammo_type] + @spec get_used_packs_count_for_type(Type.t(), User.t()) :: non_neg_integer() + def get_used_packs_count_for_type(%Type{id: type_id} = type, user) do + [type] |> get_used_packs_count_for_types(user) - |> Map.get(ammo_type_id, 0) + |> Map.get(type_id, 0) end @doc """ - Returns the count of used packs for multiple ammo types. + Returns the count of used packs for multiple types. ## Examples iex> get_used_packs_count_for_types( - ...> [%AmmoType{id: 123, user_id: 456}], + ...> [%Type{id: 123, user_id: 456}], ...> %User{id: 456} ...> ) %{123 => 3} """ - @spec get_used_packs_count_for_types([AmmoType.t()], User.t()) :: - %{optional(AmmoType.id()) => non_neg_integer()} - def get_used_packs_count_for_types(ammo_types, %User{id: user_id}) do - ammo_type_ids = - ammo_types - |> Enum.map(fn %AmmoType{id: ammo_type_id, user_id: ^user_id} -> ammo_type_id end) + @spec get_used_packs_count_for_types([Type.t()], User.t()) :: + %{optional(Type.id()) => non_neg_integer()} + def get_used_packs_count_for_types(types, %User{id: user_id}) do + type_ids = + types + |> Enum.map(fn %Type{id: type_id, user_id: ^user_id} -> type_id end) Repo.all( from ag in Pack, where: ag.user_id == ^user_id, - where: ag.ammo_type_id in ^ammo_type_ids, + where: ag.type_id in ^type_ids, where: ag.count == 0, - group_by: ag.ammo_type_id, - select: {ag.ammo_type_id, count(ag.id)} + group_by: ag.type_id, + select: {ag.type_id, count(ag.id)} ) |> Map.new() end @@ -748,18 +746,18 @@ defmodule Cannery.Ammo do [%Pack{notes: "My cool pack"}, ...] """ - @spec list_packs(search :: String.t() | nil, AmmoType.class() | :all, User.t()) :: + @spec list_packs(search :: String.t() | nil, Type.class() | :all, User.t()) :: [Pack.t()] @spec list_packs( search :: nil | String.t(), - AmmoType.class() | :all, + Type.class() | :all, User.t(), show_used :: boolean() ) :: [Pack.t()] def list_packs(search, type, %{id: user_id}, show_used \\ false) do from(ag in Pack, as: :ag, - join: at in assoc(ag, :ammo_type), + join: at in assoc(ag, :type), as: :at, join: c in Container, on: ag.container_id == c.id, @@ -781,7 +779,7 @@ defmodule Cannery.Ammo do |> Repo.all() end - @spec list_packs_filter_on_type(Queryable.t(), AmmoType.class() | :all) :: Queryable.t() + @spec list_packs_filter_on_type(Queryable.t(), Type.class() | :all) :: Queryable.t() defp list_packs_filter_on_type(query, :rifle), do: query |> where([at: at], at.class == :rifle) @@ -1063,13 +1061,13 @@ defmodule Cannery.Ammo do end defp do_create_packs( - %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, + %{"type_id" => type_id, "container_id" => container_id} = attrs, multiplier, user ) when multiplier >= 1 and multiplier <= @pack_create_limit and - ammo_type_id |> is_binary() and + type_id |> is_binary() and container_id |> is_binary() do now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) @@ -1077,7 +1075,7 @@ defmodule Cannery.Ammo do Enum.map(1..multiplier, fn _count -> %Pack{} |> Pack.create_changeset( - get_ammo_type!(ammo_type_id, user), + get_type!(type_id, user), Containers.get_container!(container_id, user), user, attrs @@ -1107,15 +1105,15 @@ defmodule Cannery.Ammo do end defp do_create_packs( - %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, + %{"type_id" => type_id, "container_id" => container_id} = attrs, _multiplier, user ) - when is_binary(ammo_type_id) and is_binary(container_id) do + when is_binary(type_id) and is_binary(container_id) do changeset = %Pack{} |> Pack.create_changeset( - get_ammo_type!(ammo_type_id, user), + get_type!(type_id, user), Containers.get_container!(container_id, user), user, attrs diff --git a/lib/cannery/ammo/ammo_type.ex b/lib/cannery/ammo/ammo_type.ex index baa2d8c..2ea4a01 100644 --- a/lib/cannery/ammo/ammo_type.ex +++ b/lib/cannery/ammo/ammo_type.ex @@ -1,4 +1,4 @@ -defmodule Cannery.Ammo.AmmoType do +defmodule Cannery.Ammo.Type do @moduledoc """ An ammunition type. @@ -38,7 +38,7 @@ defmodule Cannery.Ammo.AmmoType do ]} @primary_key {:id, :binary_id, autogenerate: true} @foreign_key_type :binary_id - schema "ammo_types" do + schema "types" do field :name, :string field :desc, :string @@ -127,9 +127,9 @@ defmodule Cannery.Ammo.AmmoType do inserted_at: NaiveDateTime.t(), updated_at: NaiveDateTime.t() } - @type new_ammo_type :: %__MODULE__{} + @type new_type :: %__MODULE__{} @type id :: UUID.t() - @type changeset :: Changeset.t(t() | new_ammo_type()) + @type changeset :: Changeset.t(t() | new_type()) @type class :: :rifle | :shotgun | :pistol | nil @spec changeset_fields() :: [atom()] @@ -197,10 +197,10 @@ defmodule Cannery.Ammo.AmmoType do ] @doc false - @spec create_changeset(new_ammo_type(), User.t(), attrs :: map()) :: changeset() - def create_changeset(ammo_type, %User{id: user_id}, attrs) do + @spec create_changeset(new_type(), User.t(), attrs :: map()) :: changeset() + def create_changeset(type, %User{id: user_id}, attrs) do changeset = - ammo_type + type |> change(user_id: user_id) |> cast(attrs, changeset_fields()) @@ -210,10 +210,10 @@ defmodule Cannery.Ammo.AmmoType do end @doc false - @spec update_changeset(t() | new_ammo_type(), attrs :: map()) :: changeset() - def update_changeset(ammo_type, attrs) do + @spec update_changeset(t() | new_type(), attrs :: map()) :: changeset() + def update_changeset(type, attrs) do changeset = - ammo_type + type |> cast(attrs, changeset_fields()) string_fields() diff --git a/lib/cannery/ammo/pack.ex b/lib/cannery/ammo/pack.ex index c7a04d7..a33521c 100644 --- a/lib/cannery/ammo/pack.ex +++ b/lib/cannery/ammo/pack.ex @@ -9,7 +9,7 @@ defmodule Cannery.Ammo.Pack do use Ecto.Schema import CanneryWeb.Gettext import Ecto.Changeset - alias Cannery.Ammo.AmmoType + alias Cannery.Ammo.Type alias Cannery.{Accounts.User, Containers, Containers.Container} alias Ecto.{Changeset, UUID} @@ -20,7 +20,7 @@ defmodule Cannery.Ammo.Pack do :notes, :price_paid, :staged, - :ammo_type_id, + :type_id, :container_id ]} @primary_key {:id, :binary_id, autogenerate: true} @@ -32,7 +32,7 @@ defmodule Cannery.Ammo.Pack do field :staged, :boolean, default: false field :purchased_on, :date - belongs_to :ammo_type, AmmoType + belongs_to :type, Type field :container_id, :binary_id field :user_id, :binary_id @@ -46,8 +46,8 @@ defmodule Cannery.Ammo.Pack do price_paid: float() | nil, staged: boolean(), purchased_on: Date.t(), - ammo_type: AmmoType.t() | nil, - ammo_type_id: AmmoType.id(), + type: Type.t() | nil, + type_id: Type.id(), container_id: Container.id(), user_id: User.id(), inserted_at: NaiveDateTime.t(), @@ -60,36 +60,36 @@ defmodule Cannery.Ammo.Pack do @doc false @spec create_changeset( new_pack(), - AmmoType.t() | nil, + Type.t() | nil, Container.t() | nil, User.t(), attrs :: map() ) :: changeset() def create_changeset( pack, - %AmmoType{id: ammo_type_id}, + %Type{id: type_id}, %Container{id: container_id, user_id: user_id}, %User{id: user_id}, attrs ) - when is_binary(ammo_type_id) and is_binary(container_id) and is_binary(user_id) do + when is_binary(type_id) and is_binary(container_id) and is_binary(user_id) do pack - |> change(ammo_type_id: ammo_type_id) + |> change(type_id: type_id) |> change(user_id: user_id) |> change(container_id: container_id) |> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on]) |> validate_number(:count, greater_than: 0) - |> validate_required([:count, :staged, :purchased_on, :ammo_type_id, :container_id, :user_id]) + |> validate_required([:count, :staged, :purchased_on, :type_id, :container_id, :user_id]) end @doc """ - Invalid changeset, used to prompt user to select ammo type and container + Invalid changeset, used to prompt user to select type and container """ - def create_changeset(pack, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do + def create_changeset(pack, _invalid_type, _invalid_container, _invalid_user, attrs) do pack - |> 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")) + |> cast(attrs, [:type_id, :container_id]) + |> validate_required([:type_id, :container_id]) + |> add_error(:invalid, dgettext("errors", "Please select a type and container")) end @doc false diff --git a/lib/cannery_web/components/ammo_type_table_component.ex b/lib/cannery_web/components/ammo_type_table_component.ex index 640229a..2528887 100644 --- a/lib/cannery_web/components/ammo_type_table_component.ex +++ b/lib/cannery_web/components/ammo_type_table_component.ex @@ -1,9 +1,9 @@ -defmodule CanneryWeb.Components.AmmoTypeTableComponent do +defmodule CanneryWeb.Components.TypeTableComponent do @moduledoc """ - A component that displays a list of ammo type + A component that displays a list of types """ use CanneryWeb, :live_component - alias Cannery.{Accounts.User, ActivityLog, Ammo, Ammo.AmmoType} + alias Cannery.{Accounts.User, ActivityLog, Ammo, Ammo.Type} alias CanneryWeb.Components.TableComponent alias Ecto.UUID alias Phoenix.LiveView.{Rendered, Socket} @@ -13,30 +13,30 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do %{ required(:id) => UUID.t(), required(:current_user) => User.t(), - optional(:class) => AmmoType.class() | nil, + optional(:class) => Type.class() | nil, optional(:show_used) => boolean(), - optional(:ammo_types) => [AmmoType.t()], + optional(:types) => [Type.t()], optional(:actions) => Rendered.t(), optional(any()) => any() }, Socket.t() ) :: {:ok, Socket.t()} - def update(%{id: _id, ammo_types: _ammo_types, current_user: _current_user} = assigns, socket) do + def update(%{id: _id, types: _types, current_user: _current_user} = assigns, socket) do socket = socket |> assign(assigns) |> assign_new(:show_used, fn -> false end) |> assign_new(:class, fn -> :all end) |> assign_new(:actions, fn -> [] end) - |> display_ammo_types() + |> display_types() {:ok, socket} end - defp display_ammo_types( + defp display_types( %{ assigns: %{ - ammo_types: ammo_types, + types: types, current_user: current_user, show_used: show_used, class: class, @@ -92,8 +92,8 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do # remove columns if all values match defaults default_value = if type == :atom, do: false, else: nil - ammo_types - |> Enum.any?(fn ammo_type -> Map.get(ammo_type, key, default_value) != default_value end) + types + |> Enum.any?(fn type -> Map.get(type, key, default_value) != default_value end) end) columns = @@ -152,17 +152,17 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do ) |> TableComponent.maybe_compose_columns(%{label: gettext("Name"), key: :name, type: :name}) - round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user) - packs_count = ammo_types |> Ammo.get_packs_count_for_types(current_user) - average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user) + round_counts = types |> Ammo.get_round_count_for_types(current_user) + packs_count = types |> Ammo.get_packs_count_for_types(current_user) + average_costs = types |> Ammo.get_average_cost_for_types(current_user) [used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] = if show_used do [ - ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user), - ammo_types |> Ammo.get_historical_count_for_ammo_types(current_user), - ammo_types |> Ammo.get_packs_count_for_types(current_user, true), - ammo_types |> Ammo.get_used_packs_count_for_types(current_user) + types |> ActivityLog.get_used_count_for_types(current_user), + types |> Ammo.get_historical_count_for_types(current_user), + types |> Ammo.get_packs_count_for_types(current_user, true), + types |> Ammo.get_used_packs_count_for_types(current_user) ] else [nil, nil, nil, nil] @@ -181,9 +181,9 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do } rows = - ammo_types - |> Enum.map(fn ammo_type -> - ammo_type |> get_ammo_type_values(columns, extra_data) + types + |> Enum.map(fn type -> + type |> get_type_values(columns, extra_data) end) socket |> assign(columns: columns, rows: rows) @@ -198,92 +198,92 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do """ end - defp get_ammo_type_values(ammo_type, columns, extra_data) do + defp get_type_values(type, columns, extra_data) do columns - |> Map.new(fn %{key: key, type: type} -> - {key, get_ammo_type_value(type, key, ammo_type, extra_data)} + |> Map.new(fn %{key: key, type: column_type} -> + {key, get_type_value(column_type, key, type, extra_data)} end) end - defp get_ammo_type_value(:atom, key, ammo_type, _other_data), - do: ammo_type |> Map.get(key) |> humanize() + defp get_type_value(:atom, key, type, _other_data), + do: type |> Map.get(key) |> humanize() - defp get_ammo_type_value(:round_count, _key, %{id: ammo_type_id}, %{round_counts: round_counts}), - do: Map.get(round_counts, ammo_type_id, 0) + defp get_type_value(:round_count, _key, %{id: type_id}, %{round_counts: round_counts}), + do: Map.get(round_counts, type_id, 0) - defp get_ammo_type_value( + defp get_type_value( :historical_round_count, _key, - %{id: ammo_type_id}, + %{id: type_id}, %{historical_round_counts: historical_round_counts} ) do - Map.get(historical_round_counts, ammo_type_id, 0) + Map.get(historical_round_counts, type_id, 0) end - defp get_ammo_type_value( + defp get_type_value( :used_round_count, _key, - %{id: ammo_type_id}, + %{id: type_id}, %{used_counts: used_counts} ) do - Map.get(used_counts, ammo_type_id, 0) + Map.get(used_counts, type_id, 0) end - defp get_ammo_type_value( + defp get_type_value( :historical_pack_count, _key, - %{id: ammo_type_id}, + %{id: type_id}, %{historical_pack_counts: historical_pack_counts} ) do - Map.get(historical_pack_counts, ammo_type_id, 0) + Map.get(historical_pack_counts, type_id, 0) end - defp get_ammo_type_value( + defp get_type_value( :used_pack_count, _key, - %{id: ammo_type_id}, + %{id: type_id}, %{used_pack_counts: used_pack_counts} ) do - Map.get(used_pack_counts, ammo_type_id, 0) + Map.get(used_pack_counts, type_id, 0) end - defp get_ammo_type_value(:ammo_count, _key, %{id: ammo_type_id}, %{packs_count: packs_count}), - do: Map.get(packs_count, ammo_type_id) + defp get_type_value(:ammo_count, _key, %{id: type_id}, %{packs_count: packs_count}), + do: Map.get(packs_count, type_id) - defp get_ammo_type_value( + defp get_type_value( :avg_price_paid, _key, - %{id: ammo_type_id}, + %{id: type_id}, %{average_costs: average_costs} ) do - case Map.get(average_costs, ammo_type_id) do + case Map.get(average_costs, type_id) do nil -> {0, gettext("No cost information")} count -> {count, gettext("$%{amount}", amount: display_currency(count))} end end - defp get_ammo_type_value(:name, _key, %{name: ammo_type_name} = ammo_type, _other_data) do - assigns = %{ammo_type: ammo_type} + defp get_type_value(:name, _key, %{name: type_name} = type, _other_data) do + assigns = %{type: type} - {ammo_type_name, + {type_name, ~H""" - <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} class="link"> - <%= @ammo_type.name %> + <.link navigate={Routes.type_show_path(Endpoint, :show, @type)} class="link"> + <%= @type.name %> """} end - defp get_ammo_type_value(:actions, _key, ammo_type, %{actions: actions}) do - assigns = %{actions: actions, ammo_type: ammo_type} + defp get_type_value(:actions, _key, type, %{actions: actions}) do + assigns = %{actions: actions, type: type} ~H""" - <%= render_slot(@actions, @ammo_type) %> + <%= render_slot(@actions, @type) %> """ end - defp get_ammo_type_value(nil, _key, _ammo_type, _other_data), do: nil + defp get_type_value(nil, _key, _type, _other_data), do: nil - defp get_ammo_type_value(_other, key, ammo_type, _other_data), do: ammo_type |> Map.get(key) + defp get_type_value(_other, key, type, _other_data), do: type |> Map.get(key) @spec display_currency(float()) :: String.t() defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2) diff --git a/lib/cannery_web/components/core_components/pack_card.html.heex b/lib/cannery_web/components/core_components/pack_card.html.heex index 58f232c..517a3cf 100644 --- a/lib/cannery_web/components/core_components/pack_card.html.heex +++ b/lib/cannery_web/components/core_components/pack_card.html.heex @@ -7,7 +7,7 @@ > <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="mb-2 link">

- <%= @pack.ammo_type.name %> + <%= @pack.type.name %>

diff --git a/lib/cannery_web/components/core_components/topbar.html.heex b/lib/cannery_web/components/core_components/topbar.html.heex index 67e4208..08997be 100644 --- a/lib/cannery_web/components/core_components/topbar.html.heex +++ b/lib/cannery_web/components/core_components/topbar.html.heex @@ -44,7 +44,7 @@
  • <.link - navigate={Routes.ammo_type_index_path(Endpoint, :index)} + navigate={Routes.type_index_path(Endpoint, :index)} class="text-white hover:underline" > <%= gettext("Catalog") %> diff --git a/lib/cannery_web/components/pack_table_component.ex b/lib/cannery_web/components/pack_table_component.ex index 8178b37..749fdc1 100644 --- a/lib/cannery_web/components/pack_table_component.ex +++ b/lib/cannery_web/components/pack_table_component.ex @@ -16,7 +16,7 @@ defmodule CanneryWeb.Components.PackTableComponent do required(:current_user) => User.t(), required(:packs) => [Pack.t()], required(:show_used) => boolean(), - optional(:ammo_type) => Rendered.t(), + optional(:type) => Rendered.t(), optional(:range) => Rendered.t(), optional(:container) => Rendered.t(), optional(:actions) => Rendered.t(), @@ -31,7 +31,7 @@ defmodule CanneryWeb.Components.PackTableComponent do socket = socket |> assign(assigns) - |> assign_new(:ammo_type, fn -> [] end) + |> assign_new(:type, fn -> [] end) |> assign_new(:range, fn -> [] end) |> assign_new(:container, fn -> [] end) |> assign_new(:actions, fn -> [] end) @@ -45,7 +45,7 @@ defmodule CanneryWeb.Components.PackTableComponent do assigns: %{ packs: packs, current_user: current_user, - ammo_type: ammo_type, + type: type, range: range, container: container, actions: actions, @@ -92,8 +92,8 @@ defmodule CanneryWeb.Components.PackTableComponent do key: :count }) |> TableComponent.maybe_compose_columns( - %{label: gettext("Ammo type"), key: :ammo_type}, - ammo_type != [] + %{label: gettext("Type"), key: :type}, + type != [] ) containers = @@ -103,7 +103,7 @@ defmodule CanneryWeb.Components.PackTableComponent do extra_data = %{ current_user: current_user, - ammo_type: ammo_type, + type: type, columns: columns, container: container, containers: containers, @@ -144,15 +144,15 @@ defmodule CanneryWeb.Components.PackTableComponent do @spec get_value_for_key(atom(), Pack.t(), additional_data :: map()) :: any() | {any(), Rendered.t()} defp get_value_for_key( - :ammo_type, - %{ammo_type: %{name: ammo_type_name} = ammo_type}, - %{ammo_type: ammo_type_block} + :type, + %{type: %{name: type_name} = type}, + %{type: type_block} ) do - assigns = %{ammo_type: ammo_type, ammo_type_block: ammo_type_block} + assigns = %{type: type, type_block: type_block} - {ammo_type_name, + {type_name, ~H""" - <%= render_slot(@ammo_type_block, @ammo_type) %> + <%= render_slot(@type_block, @type) %> """} end diff --git a/lib/cannery_web/components/shot_record_table_component.ex b/lib/cannery_web/components/shot_record_table_component.ex index 9df52fa..23c893e 100644 --- a/lib/cannery_web/components/shot_record_table_component.ex +++ b/lib/cannery_web/components/shot_record_table_component.ex @@ -96,10 +96,10 @@ defmodule CanneryWeb.Components.ShotRecordTableComponent do defp get_row_value(:name, %{pack_id: pack_id}, %{packs: packs}) do assigns = %{pack: pack = Map.fetch!(packs, pack_id)} - {pack.ammo_type.name, + {pack.type.name, ~H""" <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="link"> - <%= @pack.ammo_type.name %> + <%= @pack.type.name %> """} end diff --git a/lib/cannery_web/controllers/export_controller.ex b/lib/cannery_web/controllers/export_controller.ex index 4c1913d..66f285c 100644 --- a/lib/cannery_web/controllers/export_controller.ex +++ b/lib/cannery_web/controllers/export_controller.ex @@ -3,27 +3,27 @@ defmodule CanneryWeb.ExportController do alias Cannery.{ActivityLog, Ammo, Containers} def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do - ammo_types = Ammo.list_ammo_types(current_user, :all) - used_counts = ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user) - round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user) - pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user) + types = Ammo.list_types(current_user, :all) + used_counts = types |> ActivityLog.get_used_count_for_types(current_user) + round_counts = types |> Ammo.get_round_count_for_types(current_user) + pack_counts = types |> Ammo.get_packs_count_for_types(current_user) - total_pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user, true) + total_pack_counts = types |> Ammo.get_packs_count_for_types(current_user, true) - average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user) + average_costs = types |> Ammo.get_average_cost_for_types(current_user) - ammo_types = - ammo_types - |> Enum.map(fn %{id: ammo_type_id} = ammo_type -> - ammo_type + types = + types + |> Enum.map(fn %{id: type_id} = type -> + type |> Jason.encode!() |> Jason.decode!() |> Map.merge(%{ - "average_cost" => Map.get(average_costs, ammo_type_id), - "round_count" => Map.get(round_counts, ammo_type_id, 0), - "used_count" => Map.get(used_counts, ammo_type_id, 0), - "pack_count" => Map.get(pack_counts, ammo_type_id, 0), - "total_pack_count" => Map.get(total_pack_counts, ammo_type_id, 0) + "average_cost" => Map.get(average_costs, type_id), + "round_count" => Map.get(round_counts, type_id, 0), + "used_count" => Map.get(used_counts, type_id, 0), + "pack_count" => Map.get(pack_counts, type_id, 0), + "total_pack_count" => Map.get(total_pack_counts, type_id, 0) }) end) @@ -66,7 +66,7 @@ defmodule CanneryWeb.ExportController do json(conn, %{ user: current_user, - ammo_types: ammo_types, + types: types, packs: packs, shot_records: shot_records, containers: containers diff --git a/lib/cannery_web/live/ammo_type_live/form_component.ex b/lib/cannery_web/live/ammo_type_live/form_component.ex index f4a41fe..643c0ee 100644 --- a/lib/cannery_web/live/ammo_type_live/form_component.ex +++ b/lib/cannery_web/live/ammo_type_live/form_component.ex @@ -1,16 +1,16 @@ -defmodule CanneryWeb.AmmoTypeLive.FormComponent do +defmodule CanneryWeb.TypeLive.FormComponent do @moduledoc """ - Livecomponent that can update or create an Cannery.Ammo.AmmoType + Livecomponent that can update or create an Cannery.Ammo.Type """ use CanneryWeb, :live_component - alias Cannery.{Accounts.User, Ammo, Ammo.AmmoType} + alias Cannery.{Accounts.User, Ammo, Ammo.Type} alias Ecto.Changeset alias Phoenix.LiveView.Socket @impl true @spec update( - %{:ammo_type => AmmoType.t(), :current_user => User.t(), optional(any) => any}, + %{:type => Type.t(), :current_user => User.t(), optional(any) => any}, Socket.t() ) :: {:ok, Socket.t()} def update(%{current_user: _current_user} = assigns, socket) do @@ -18,21 +18,21 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do end @impl true - def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do - {:noreply, socket |> assign_changeset(ammo_type_params)} + def handle_event("validate", %{"type" => type_params}, socket) do + {:noreply, socket |> assign_changeset(type_params)} end def handle_event( "save", - %{"ammo_type" => ammo_type_params}, + %{"type" => type_params}, %{assigns: %{action: action}} = socket ) do - save_ammo_type(socket, action, ammo_type_params) + save_type(socket, action, type_params) end defp assign_changeset( - %{assigns: %{action: action, ammo_type: ammo_type, current_user: user}} = socket, - ammo_type_params + %{assigns: %{action: action, type: type, current_user: user}} = socket, + type_params ) do changeset_action = case action do @@ -43,10 +43,10 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do changeset = case action do create when create in [:new, :clone] -> - ammo_type |> AmmoType.create_changeset(user, ammo_type_params) + type |> Type.create_changeset(user, type_params) :edit -> - ammo_type |> AmmoType.update_changeset(ammo_type_params) + type |> Type.update_changeset(type_params) end changeset = @@ -58,16 +58,15 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do socket |> assign(changeset: changeset) end - defp save_ammo_type( - %{assigns: %{ammo_type: ammo_type, current_user: current_user, return_to: return_to}} = - socket, + defp save_type( + %{assigns: %{type: type, current_user: current_user, return_to: return_to}} = socket, :edit, - ammo_type_params + type_params ) do socket = - case Ammo.update_ammo_type(ammo_type, ammo_type_params, current_user) do - {:ok, %{name: ammo_type_name}} -> - prompt = dgettext("prompts", "%{name} updated successfully", name: ammo_type_name) + case Ammo.update_type(type, type_params, current_user) do + {:ok, %{name: type_name}} -> + prompt = dgettext("prompts", "%{name} updated successfully", name: type_name) socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) {:error, %Changeset{} = changeset} -> @@ -77,16 +76,16 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do {:noreply, socket} end - defp save_ammo_type( + defp save_type( %{assigns: %{current_user: current_user, return_to: return_to}} = socket, action, - ammo_type_params + type_params ) when action in [:new, :clone] do socket = - case Ammo.create_ammo_type(ammo_type_params, current_user) do - {:ok, %{name: ammo_type_name}} -> - prompt = dgettext("prompts", "%{name} created successfully", name: ammo_type_name) + case Ammo.create_type(type_params, current_user) do + {:ok, %{name: type_name}} -> + prompt = dgettext("prompts", "%{name} created successfully", name: type_name) socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) {:error, %Changeset{} = changeset} -> diff --git a/lib/cannery_web/live/ammo_type_live/form_component.html.heex b/lib/cannery_web/live/ammo_type_live/form_component.html.heex index 32cc3bf..765dfcc 100644 --- a/lib/cannery_web/live/ammo_type_live/form_component.html.heex +++ b/lib/cannery_web/live/ammo_type_live/form_component.html.heex @@ -5,7 +5,7 @@ <.form :let={f} for={@changeset} - id="ammo_type-form" + id="type-form" phx-target={@myself} phx-change="validate" phx-submit="save" @@ -37,7 +37,7 @@ <%= label(f, :desc, gettext("Description"), class: "title text-lg text-primary-600") %> <%= textarea(f, :desc, - id: "ammo-type-form-desc", + id: "type-form-desc", class: "text-center col-span-2 input input-primary", phx_hook: "MaintainAttrs", phx_update: "ignore" diff --git a/lib/cannery_web/live/ammo_type_live/index.ex b/lib/cannery_web/live/ammo_type_live/index.ex index 5a455bb..accaed3 100644 --- a/lib/cannery_web/live/ammo_type_live/index.ex +++ b/lib/cannery_web/live/ammo_type_live/index.ex @@ -1,18 +1,18 @@ -defmodule CanneryWeb.AmmoTypeLive.Index do +defmodule CanneryWeb.TypeLive.Index do @moduledoc """ - Liveview for showing a Cannery.Ammo.AmmoType index + Liveview for showing a Cannery.Ammo.Type index """ use CanneryWeb, :live_view - alias Cannery.{Ammo, Ammo.AmmoType} + alias Cannery.{Ammo, Ammo.Type} @impl true def mount(%{"search" => search}, _session, socket) do - {:ok, socket |> assign(class: :all, show_used: false, search: search) |> list_ammo_types()} + {:ok, socket |> assign(class: :all, show_used: false, search: search) |> list_types()} end def mount(_params, _session, socket) do - {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> list_ammo_types()} + {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> list_types()} end @impl true @@ -21,28 +21,28 @@ defmodule CanneryWeb.AmmoTypeLive.Index do end defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do - %{name: ammo_type_name} = ammo_type = Ammo.get_ammo_type!(id, current_user) + %{name: type_name} = type = Ammo.get_type!(id, current_user) socket |> assign( - page_title: gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name), - ammo_type: ammo_type + page_title: gettext("Edit %{type_name}", type_name: type_name), + type: type ) end defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do socket |> assign( - page_title: gettext("New Ammo type"), - ammo_type: %{Ammo.get_ammo_type!(id, current_user) | id: nil} + page_title: gettext("New Type"), + type: %{Ammo.get_type!(id, current_user) | id: nil} ) end defp apply_action(socket, :new, _params) do socket |> assign( - page_title: gettext("New Ammo type"), - ammo_type: %AmmoType{} + page_title: gettext("New Type"), + type: %Type{} ) end @@ -51,9 +51,9 @@ defmodule CanneryWeb.AmmoTypeLive.Index do |> assign( page_title: gettext("Catalog"), search: nil, - ammo_type: nil + type: nil ) - |> list_ammo_types() + |> list_types() end defp apply_action(socket, :search, %{"search" => search}) do @@ -61,54 +61,54 @@ defmodule CanneryWeb.AmmoTypeLive.Index do |> assign( page_title: gettext("Catalog"), search: search, - ammo_type: nil + type: nil ) - |> list_ammo_types() + |> list_types() end @impl true def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do - %{name: name} = Ammo.get_ammo_type!(id, current_user) |> Ammo.delete_ammo_type!(current_user) + %{name: name} = Ammo.get_type!(id, current_user) |> Ammo.delete_type!(current_user) prompt = dgettext("prompts", "%{name} deleted succesfully", name: name) - {:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()} + {:noreply, socket |> put_flash(:info, prompt) |> list_types()} end def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do - {:noreply, socket |> assign(:show_used, !show_used) |> list_ammo_types()} + {:noreply, socket |> assign(:show_used, !show_used) |> list_types()} end def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do - {:noreply, socket |> push_patch(to: Routes.ammo_type_index_path(Endpoint, :index))} + {:noreply, socket |> push_patch(to: Routes.type_index_path(Endpoint, :index))} end def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do - search_path = Routes.ammo_type_index_path(Endpoint, :search, search_term) + search_path = Routes.type_index_path(Endpoint, :search, search_term) {:noreply, socket |> push_patch(to: search_path)} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do - {:noreply, socket |> assign(:class, :rifle) |> list_ammo_types()} + def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do + {:noreply, socket |> assign(:class, :rifle) |> list_types()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do - {:noreply, socket |> assign(:class, :shotgun) |> list_ammo_types()} + def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do + {:noreply, socket |> assign(:class, :shotgun) |> list_types()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do - {:noreply, socket |> assign(:class, :pistol) |> list_ammo_types()} + def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do + {:noreply, socket |> assign(:class, :pistol) |> list_types()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do - {:noreply, socket |> assign(:class, :all) |> list_ammo_types()} + def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do + {:noreply, socket |> assign(:class, :all) |> list_types()} end - defp list_ammo_types( + defp list_types( %{assigns: %{class: class, search: search, current_user: current_user}} = socket ) do socket |> assign( - ammo_types: Ammo.list_ammo_types(search, current_user, class), - ammo_types_count: Ammo.get_ammo_types_count!(current_user) + types: Ammo.list_types(search, current_user, class), + types_count: Ammo.get_types_count!(current_user) ) end end diff --git a/lib/cannery_web/live/ammo_type_live/index.html.heex b/lib/cannery_web/live/ammo_type_live/index.html.heex index 102c36a..33ee230 100644 --- a/lib/cannery_web/live/ammo_type_live/index.html.heex +++ b/lib/cannery_web/live/ammo_type_live/index.html.heex @@ -3,25 +3,25 @@ <%= gettext("Catalog") %> - <%= if @ammo_types_count == 0 do %> + <%= if @types_count == 0 do %>

    - <%= gettext("No Ammo types") %> + <%= gettext("No Types") %> <%= display_emoji("😔") %>

    - <.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> + <.link patch={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary"> <%= dgettext("actions", "Add your first type!") %> <% else %> - <.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> - <%= dgettext("actions", "New Ammo type") %> + <.link patch={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary"> + <%= dgettext("actions", "New Type") %>
    <.form :let={f} for={%{}} - as={:ammo_type} + as={:type} phx-change="change_class" phx-submit="change_class" class="flex items-center" @@ -66,49 +66,43 @@
    - <%= if @ammo_types |> Enum.empty?() do %> + <%= if @types |> Enum.empty?() do %>

    - <%= gettext("No Ammo types") %> + <%= gettext("No Types") %> <%= display_emoji("😔") %>

    <% else %> <.live_component - module={CanneryWeb.Components.AmmoTypeTableComponent} - id="ammo_types_index_table" + module={CanneryWeb.Components.TypeTableComponent} + id="types_index_table" action={@live_action} - ammo_types={@ammo_types} + types={@types} current_user={@current_user} show_used={@show_used} class={@class} > - <:actions :let={ammo_type}> + <:actions :let={type}>
    <.link - navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} + navigate={Routes.type_show_path(Endpoint, :show, type)} class="text-primary-600 link" - aria-label={ - dgettext("actions", "View %{ammo_type_name}", ammo_type_name: ammo_type.name) - } + aria-label={dgettext("actions", "View %{type_name}", type_name: type.name)} > <.link - patch={Routes.ammo_type_index_path(Endpoint, :edit, ammo_type)} + patch={Routes.type_index_path(Endpoint, :edit, type)} class="text-primary-600 link" - aria-label={ - dgettext("actions", "Edit %{ammo_type_name}", ammo_type_name: ammo_type.name) - } + aria-label={dgettext("actions", "Edit %{type_name}", type_name: type.name)} > <.link - patch={Routes.ammo_type_index_path(Endpoint, :clone, ammo_type)} + patch={Routes.type_index_path(Endpoint, :clone, type)} class="text-primary-600 link" - aria-label={ - dgettext("actions", "Clone %{ammo_type_name}", ammo_type_name: ammo_type.name) - } + aria-label={dgettext("actions", "Clone %{type_name}", type_name: type.name)} > @@ -117,17 +111,15 @@ href="#" class="text-primary-600 link" phx-click="delete" - phx-value-id={ammo_type.id} + phx-value-id={type.id} data-confirm={ dgettext( "prompts", "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!", - name: ammo_type.name + name: type.name ) } - aria-label={ - dgettext("actions", "Delete %{ammo_type_name}", ammo_type_name: ammo_type.name) - } + aria-label={dgettext("actions", "Delete %{type_name}", type_name: type.name)} > @@ -140,15 +132,15 @@ <.modal :if={@live_action in [:new, :edit, :clone]} - return_to={Routes.ammo_type_index_path(Endpoint, :index)} + return_to={Routes.type_index_path(Endpoint, :index)} > <.live_component - module={CanneryWeb.AmmoTypeLive.FormComponent} - id={@ammo_type.id || :new} + module={CanneryWeb.TypeLive.FormComponent} + id={@type.id || :new} title={@page_title} action={@live_action} - ammo_type={@ammo_type} - return_to={Routes.ammo_type_index_path(Endpoint, :index)} + type={@type} + return_to={Routes.type_index_path(Endpoint, :index)} current_user={@current_user} } /> diff --git a/lib/cannery_web/live/ammo_type_live/show.ex b/lib/cannery_web/live/ammo_type_live/show.ex index 1143eb8..d18b7f3 100644 --- a/lib/cannery_web/live/ammo_type_live/show.ex +++ b/lib/cannery_web/live/ammo_type_live/show.ex @@ -1,10 +1,10 @@ -defmodule CanneryWeb.AmmoTypeLive.Show do +defmodule CanneryWeb.TypeLive.Show do @moduledoc """ - Liveview for showing and editing an Cannery.Ammo.AmmoType + Liveview for showing and editing an Cannery.Ammo.Type """ use CanneryWeb, :live_view - alias Cannery.{ActivityLog, Ammo, Ammo.AmmoType, Containers} + alias Cannery.{ActivityLog, Ammo, Ammo.Type, Containers} alias CanneryWeb.Endpoint @impl true @@ -13,38 +13,38 @@ defmodule CanneryWeb.AmmoTypeLive.Show do @impl true def handle_params(%{"id" => id}, _params, socket) do - {:noreply, socket |> display_ammo_type(id)} + {:noreply, socket |> display_type(id)} end @impl true def handle_event( "delete", _params, - %{assigns: %{ammo_type: ammo_type, current_user: current_user}} = socket + %{assigns: %{type: type, current_user: current_user}} = socket ) do - %{name: ammo_type_name} = ammo_type |> Ammo.delete_ammo_type!(current_user) + %{name: type_name} = type |> Ammo.delete_type!(current_user) - prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name) - redirect_to = Routes.ammo_type_index_path(socket, :index) + prompt = dgettext("prompts", "%{name} deleted succesfully", name: type_name) + redirect_to = Routes.type_index_path(socket, :index) {:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)} end def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do - {:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_type()} + {:noreply, socket |> assign(:show_used, !show_used) |> display_type()} end def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do {:noreply, socket |> assign(:view_table, !view_table)} end - defp display_ammo_type( + defp display_type( %{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} = socket, - %AmmoType{name: ammo_type_name} = ammo_type + %Type{name: type_name} = type ) do custom_fields? = - fields_to_display(ammo_type) + fields_to_display(type) |> Enum.any?(fn %{key: field, type: type} -> default_value = case type do @@ -52,10 +52,10 @@ defmodule CanneryWeb.AmmoTypeLive.Show do _other_type -> nil end - ammo_type |> Map.get(field) != default_value + type |> Map.get(field) != default_value end) - packs = ammo_type |> Ammo.list_packs_for_type(current_user, show_used) + packs = type |> Ammo.list_packs_for_type(current_user, show_used) [ original_counts, @@ -67,10 +67,10 @@ defmodule CanneryWeb.AmmoTypeLive.Show do if show_used do [ packs |> Ammo.get_original_counts(current_user), - ammo_type |> Ammo.get_used_packs_count_for_type(current_user), - ammo_type |> Ammo.get_packs_count_for_type(current_user, true), - ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user), - ammo_type |> Ammo.get_historical_count_for_ammo_type(current_user) + type |> Ammo.get_used_packs_count_for_type(current_user), + type |> Ammo.get_packs_count_for_type(current_user, true), + type |> ActivityLog.get_used_count_for_type(current_user), + type |> Ammo.get_historical_count_for_type(current_user) ] else [nil, nil, nil, nil, nil] @@ -78,8 +78,8 @@ defmodule CanneryWeb.AmmoTypeLive.Show do page_title = case live_action do - :show -> ammo_type_name - :edit -> gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name) + :show -> type_name + :edit -> gettext("Edit %{type_name}", type_name: type_name) end containers = @@ -90,33 +90,33 @@ defmodule CanneryWeb.AmmoTypeLive.Show do socket |> assign( page_title: page_title, - ammo_type: ammo_type, + type: type, packs: packs, containers: containers, cprs: packs |> Ammo.get_cprs(current_user), last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user), - avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type(current_user), - rounds: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user), + avg_cost_per_round: type |> Ammo.get_average_cost_for_type(current_user), + rounds: type |> Ammo.get_round_count_for_type(current_user), original_counts: original_counts, used_rounds: used_rounds, historical_round_count: historical_round_count, - packs_count: ammo_type |> Ammo.get_packs_count_for_type(current_user), + packs_count: type |> Ammo.get_packs_count_for_type(current_user), used_packs_count: used_packs_count, historical_packs_count: historical_packs_count, - fields_to_display: fields_to_display(ammo_type), + fields_to_display: fields_to_display(type), custom_fields?: custom_fields? ) end - defp display_ammo_type(%{assigns: %{current_user: current_user}} = socket, ammo_type_id) do - socket |> display_ammo_type(Ammo.get_ammo_type!(ammo_type_id, current_user)) + defp display_type(%{assigns: %{current_user: current_user}} = socket, type_id) do + socket |> display_type(Ammo.get_type!(type_id, current_user)) end - defp display_ammo_type(%{assigns: %{ammo_type: ammo_type}} = socket) do - socket |> display_ammo_type(ammo_type) + defp display_type(%{assigns: %{type: type}} = socket) do + socket |> display_type(type) end - defp fields_to_display(%AmmoType{class: class}) do + defp fields_to_display(%Type{class: class}) do [ %{label: gettext("Cartridge:"), key: :cartridge, type: :string}, %{ diff --git a/lib/cannery_web/live/ammo_type_live/show.html.heex b/lib/cannery_web/live/ammo_type_live/show.html.heex index 44db85c..87b9ce5 100644 --- a/lib/cannery_web/live/ammo_type_live/show.html.heex +++ b/lib/cannery_web/live/ammo_type_live/show.html.heex @@ -1,22 +1,22 @@

    - <%= @ammo_type.name %> + <%= @type.name %>

    - <%= @ammo_type.desc %> + <%= @type.desc %>
    <.link - patch={Routes.ammo_type_show_path(Endpoint, :edit, @ammo_type)} + patch={Routes.type_show_path(Endpoint, :edit, @type)} class="text-primary-600 link" - aria-label={dgettext("actions", "Edit %{ammo_type_name}", ammo_type_name: @ammo_type.name)} + aria-label={dgettext("actions", "Edit %{type_name}", type_name: @type.name)} > @@ -29,12 +29,10 @@ dgettext( "prompts", "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!", - name: @ammo_type.name + name: @type.name ) } - aria-label={ - dgettext("actions", "Delete %{ammo_type_name}", ammo_type_name: @ammo_type.name) - } + aria-label={dgettext("actions", "Delete %{type_name}", type_name: @type.name)} > @@ -42,14 +40,14 @@
    - <%= if @ammo_type.class || @custom_fields? do %> + <%= if @type.class || @custom_fields? do %>

    <%= gettext("Class") %>

    - <%= case @ammo_type.class do %> + <%= case @type.class do %> <% :shotgun -> %> <%= gettext("Shotgun") %> <% :rifle -> %> @@ -62,7 +60,7 @@ <%= for %{label: label, key: key, type: type} <- @fields_to_display do %> - <%= if @ammo_type |> Map.get(key) do %> + <%= if @type |> Map.get(key) do %>

    <%= label %>

    @@ -70,9 +68,9 @@ <%= case type do %> <% :boolean -> %> - <%= @ammo_type |> Map.get(key) |> humanize() %> + <%= @type |> Map.get(key) |> humanize() %> <% _ -> %> - <%= @ammo_type |> Map.get(key) %> + <%= @type |> Map.get(key) %> <% end %> <% end %> @@ -140,7 +138,7 @@ - <.datetime id={"#{@ammo_type.id}-inserted-at"} datetime={@ammo_type.inserted_at} /> + <.datetime id={"#{@type.id}-inserted-at"} datetime={@type.inserted_at} /> <%= if @avg_cost_per_round do %> @@ -184,7 +182,7 @@ <%= if @view_table do %> <.live_component module={CanneryWeb.Components.PackTableComponent} - id="ammo-type-show-table" + id="type-show-table" packs={@packs} current_user={@current_user} show_used={@show_used} @@ -228,17 +226,14 @@
    -<.modal - :if={@live_action == :edit} - return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} -> +<.modal :if={@live_action == :edit} return_to={Routes.type_show_path(Endpoint, :show, @type)}> <.live_component - module={CanneryWeb.AmmoTypeLive.FormComponent} - id={@ammo_type.id} + module={CanneryWeb.TypeLive.FormComponent} + id={@type.id} title={@page_title} action={@live_action} - ammo_type={@ammo_type} - return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} + type={@type} + return_to={Routes.type_show_path(Endpoint, :show, @type)} current_user={@current_user} /> diff --git a/lib/cannery_web/live/container_live/show.ex b/lib/cannery_web/live/container_live/show.ex index 042a17b..a211460 100644 --- a/lib/cannery_web/live/container_live/show.ex +++ b/lib/cannery_web/live/container_live/show.ex @@ -86,19 +86,19 @@ defmodule CanneryWeb.ContainerLive.Show do {:noreply, socket |> assign(:view_table, !view_table) |> render_container()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do {:noreply, socket |> assign(:class, :rifle) |> render_container()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do {:noreply, socket |> assign(:class, :shotgun) |> render_container()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do {:noreply, socket |> assign(:class, :pistol) |> render_container()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do {:noreply, socket |> assign(:class, :all) |> render_container()} end diff --git a/lib/cannery_web/live/container_live/show.html.heex b/lib/cannery_web/live/container_live/show.html.heex index 580cb74..9ffeebf 100644 --- a/lib/cannery_web/live/container_live/show.html.heex +++ b/lib/cannery_web/live/container_live/show.html.heex @@ -89,7 +89,7 @@ <.form :let={f} for={%{}} - as={:ammo_type} + as={:type} phx-change="change_class" phx-submit="change_class" class="flex items-center" @@ -126,16 +126,16 @@ <%= if @view_table do %> <.live_component module={CanneryWeb.Components.PackTableComponent} - id="ammo-type-show-table" + id="type-show-table" packs={@packs} current_user={@current_user} show_used={false} > - <:ammo_type :let={%{name: ammo_type_name} = ammo_type}> - <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link"> - <%= ammo_type_name %> + <:type :let={%{name: type_name} = type}> + <.link navigate={Routes.type_show_path(Endpoint, :show, type)} class="link"> + <%= type_name %> - + <% else %>
    diff --git a/lib/cannery_web/live/pack_live/form_component.ex b/lib/cannery_web/live/pack_live/form_component.ex index 96e350d..77c05a2 100644 --- a/lib/cannery_web/live/pack_live/form_component.ex +++ b/lib/cannery_web/live/pack_live/form_component.ex @@ -4,7 +4,7 @@ defmodule CanneryWeb.PackLive.FormComponent do """ use CanneryWeb, :live_component - alias Cannery.Ammo.{AmmoType, Pack} + alias Cannery.Ammo.{Pack, Type} alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container} alias Ecto.Changeset alias Phoenix.LiveView.Socket @@ -22,17 +22,17 @@ defmodule CanneryWeb.PackLive.FormComponent do @spec update(Socket.t()) :: {:ok, Socket.t()} def update(%{assigns: %{current_user: current_user}} = socket) do - %{assigns: %{ammo_types: ammo_types, containers: containers}} = + %{assigns: %{types: types, containers: containers}} = socket = socket |> assign(:pack_create_limit, @pack_create_limit) - |> assign(:ammo_types, Ammo.list_ammo_types(current_user, :all)) + |> assign(:types, Ammo.list_types(current_user, :all)) |> assign_new(:containers, fn -> Containers.list_containers(current_user) end) params = - if ammo_types |> List.first() |> is_nil(), + if types |> List.first() |> is_nil(), do: %{}, - else: %{} |> Map.put("ammo_type_id", ammo_types |> List.first() |> Map.get(:id)) + else: %{} |> Map.put("type_id", types |> List.first() |> Map.get(:id)) params = if containers |> List.first() |> is_nil(), @@ -62,9 +62,9 @@ defmodule CanneryWeb.PackLive.FormComponent 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) + @spec type_options([Type.t()]) :: [{String.t(), Type.id()}] + defp type_options(types) do + types |> Enum.map(fn %{id: id, name: name} -> {name, id} end) end # Save Helpers @@ -83,9 +83,9 @@ defmodule CanneryWeb.PackLive.FormComponent do changeset = case default_action do :insert -> - ammo_type = maybe_get_ammo_type(pack_params, user) + type = maybe_get_type(pack_params, user) container = maybe_get_container(pack_params, user) - pack |> Pack.create_changeset(ammo_type, container, user, pack_params) + pack |> Pack.create_changeset(type, container, user, pack_params) :update -> pack |> Pack.update_changeset(pack_params, user) @@ -107,12 +107,12 @@ defmodule CanneryWeb.PackLive.FormComponent do defp maybe_get_container(_params_not_found, _user), do: nil - defp maybe_get_ammo_type(%{"ammo_type_id" => ammo_type_id}, user) - when is_binary(ammo_type_id) do - ammo_type_id |> Ammo.get_ammo_type!(user) + defp maybe_get_type(%{"type_id" => type_id}, user) + when is_binary(type_id) do + type_id |> Ammo.get_type!(user) end - defp maybe_get_ammo_type(_params_not_found, _user), do: nil + defp maybe_get_type(_params_not_found, _user), do: nil defp save_pack( %{assigns: %{pack: pack, current_user: current_user, return_to: return_to}} = socket, diff --git a/lib/cannery_web/live/pack_live/form_component.html.heex b/lib/cannery_web/live/pack_live/form_component.html.heex index 4e8149f..4a5b93d 100644 --- a/lib/cannery_web/live/pack_live/form_component.html.heex +++ b/lib/cannery_web/live/pack_live/form_component.html.heex @@ -19,11 +19,11 @@ <%= changeset_errors(@changeset) %>
    - <%= label(f, :ammo_type_id, gettext("Ammo type"), class: "title text-lg text-primary-600") %> - <%= select(f, :ammo_type_id, ammo_type_options(@ammo_types), + <%= label(f, :type_id, gettext("Type"), class: "title text-lg text-primary-600") %> + <%= select(f, :type_id, type_options(@types), class: "text-center col-span-2 input input-primary" ) %> - <%= error_tag(f, :ammo_type_id, "col-span-3 text-center") %> + <%= error_tag(f, :type_id, "col-span-3 text-center") %> <%= label(f, :count, gettext("Count"), class: "title text-lg text-primary-600") %> <%= number_input(f, :count, diff --git a/lib/cannery_web/live/pack_live/index.ex b/lib/cannery_web/live/pack_live/index.ex index 4f82777..9224687 100644 --- a/lib/cannery_web/live/pack_live/index.ex +++ b/lib/cannery_web/live/pack_live/index.ex @@ -122,19 +122,19 @@ defmodule CanneryWeb.PackLive.Index do {:noreply, socket} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do {:noreply, socket |> assign(:class, :rifle) |> display_packs()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do {:noreply, socket |> assign(:class, :shotgun) |> display_packs()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do {:noreply, socket |> assign(:class, :pistol) |> display_packs()} end - def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do + def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do {:noreply, socket |> assign(:class, :all) |> display_packs()} end @@ -152,13 +152,13 @@ defmodule CanneryWeb.PackLive.Index do # prompts packs_count = Ammo.get_packs_count!(current_user, true) packs = Ammo.list_packs(search, class, current_user, show_used) - ammo_types_count = Ammo.get_ammo_types_count!(current_user) + types_count = Ammo.get_types_count!(current_user) containers_count = Containers.get_containers_count!(current_user) socket |> assign( packs: packs, - ammo_types_count: ammo_types_count, + types_count: types_count, containers_count: containers_count, packs_count: packs_count ) diff --git a/lib/cannery_web/live/pack_live/index.html.heex b/lib/cannery_web/live/pack_live/index.html.heex index 88d2db4..41a8d3a 100644 --- a/lib/cannery_web/live/pack_live/index.html.heex +++ b/lib/cannery_web/live/pack_live/index.html.heex @@ -14,14 +14,14 @@ <%= dgettext("actions", "add a container first") %>
    - <% @ammo_types_count == 0 -> %> + <% @types_count == 0 -> %>

    <%= dgettext("prompts", "You'll need to") %>

    - <.link navigate={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> - <%= dgettext("actions", "add an ammo type first") %> + <.link navigate={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary"> + <%= dgettext("actions", "add a type first") %>
    <% @packs_count == 0 -> %> @@ -42,7 +42,7 @@ <.form :let={f} for={%{}} - as={:ammo_type} + as={:type} phx-change="change_class" phx-submit="change_class" class="flex items-center" @@ -102,11 +102,11 @@ current_user={@current_user} show_used={@show_used} > - <:ammo_type :let={%{name: ammo_type_name} = ammo_type}> - <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link"> - <%= ammo_type_name %> + <:type :let={%{name: type_name} = type}> + <.link navigate={Routes.type_show_path(Endpoint, :show, type)} class="link"> + <%= type_name %> - + <:range :let={pack}>