From cd2e7f01645bb59d367649dabf6b93b9c80e2c46 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 2 Nov 2026 09:14:17 +0100 Subject: [PATCH] Rename Tag to Theme across code and schema Rename the endgame-label concept from Tag to Theme to match the glossary: the context becomes Chesstrainer.Themes, the schema Chesstrainer.Themes.Theme, and the Endgame association is :themes. The category enum (endgame/opening/tactic) is preserved. Rewrite the original migrations in place so themes / endgames_themes (with theme_id) are created directly, instead of adding a transition migration. Closes #12 --- lib/chesstrainer/endgames.ex | 10 +- lib/chesstrainer/endgames/endgame.ex | 4 +- lib/chesstrainer/tags.ex | 156 ------------------ lib/chesstrainer/themes.ex | 156 ++++++++++++++++++ .../{tags/tag.ex => themes/theme.ex} | 13 +- .../live/endgame_live/form.ex | 84 +++++----- .../live/endgame_live/index.ex | 6 +- .../live/endgame_live/show.ex | 6 +- ...s.exs => 20251105204020_create_themes.exs} | 6 +- .../20251205210539_create_endgames_tags.exs | 12 -- .../20251205210539_create_endgames_themes.exs | 12 ++ ...61101120000_case_insensitive_tag_names.exs | 15 -- ...101120000_case_insensitive_theme_names.exs | 17 ++ priv/repo/seeds.exs | 8 +- test/chesstrainer/endgames_test.exs | 16 +- test/chesstrainer/tags_test.exs | 79 --------- test/chesstrainer/themes_test.exs | 79 +++++++++ .../live/endgame_live/form_test.exs | 32 ++-- .../live/endgame_live/index_test.exs | 6 +- test/support/fixtures/tags_fixtures.ex | 26 --- test/support/fixtures/themes_fixtures.ex | 26 +++ 21 files changed, 387 insertions(+), 382 deletions(-) delete mode 100644 lib/chesstrainer/tags.ex create mode 100644 lib/chesstrainer/themes.ex rename lib/chesstrainer/{tags/tag.ex => themes/theme.ex} (68%) rename priv/repo/migrations/{20251105204020_create_tags.exs => 20251105204020_create_themes.exs} (52%) delete mode 100644 priv/repo/migrations/20251205210539_create_endgames_tags.exs create mode 100644 priv/repo/migrations/20251205210539_create_endgames_themes.exs delete mode 100644 priv/repo/migrations/20261101120000_case_insensitive_tag_names.exs create mode 100644 priv/repo/migrations/20261101120000_case_insensitive_theme_names.exs delete mode 100644 test/chesstrainer/tags_test.exs create mode 100644 test/chesstrainer/themes_test.exs delete mode 100644 test/support/fixtures/tags_fixtures.ex create mode 100644 test/support/fixtures/themes_fixtures.ex diff --git a/lib/chesstrainer/endgames.ex b/lib/chesstrainer/endgames.ex index da838a4..c7832a1 100644 --- a/lib/chesstrainer/endgames.ex +++ b/lib/chesstrainer/endgames.ex @@ -49,10 +49,10 @@ defmodule Chesstrainer.Endgames do {:error, %Ecto.Changeset{}} """ - def create_endgame(attrs, tags \\ []) do + def create_endgame(attrs, themes \\ []) do %Endgame{} |> Endgame.changeset(attrs) - |> Ecto.Changeset.put_assoc(:tags, tags) + |> Ecto.Changeset.put_assoc(:themes, themes) |> Repo.insert() end @@ -68,14 +68,14 @@ defmodule Chesstrainer.Endgames do {:error, %Ecto.Changeset{}} """ - def update_endgame(%Endgame{} = endgame, attrs, tags \\ []) do + def update_endgame(%Endgame{} = endgame, attrs, themes \\ []) do # ignore fen on update attrs = Map.delete(attrs, "fen") endgame - |> Repo.preload(:tags) + |> Repo.preload(:themes) |> Endgame.changeset(attrs) - |> Ecto.Changeset.put_assoc(:tags, tags) + |> Ecto.Changeset.put_assoc(:themes, themes) |> Repo.update() end diff --git a/lib/chesstrainer/endgames/endgame.ex b/lib/chesstrainer/endgames/endgame.ex index 3055c9e..791ecba 100644 --- a/lib/chesstrainer/endgames/endgame.ex +++ b/lib/chesstrainer/endgames/endgame.ex @@ -20,8 +20,8 @@ defmodule Chesstrainer.Endgames.Endgame do field :times_attempted, :integer field :times_solved, :integer - many_to_many :tags, Chesstrainer.Tags.Tag, - join_through: "endgames_tags", + many_to_many :themes, Chesstrainer.Themes.Theme, + join_through: "endgames_themes", on_replace: :delete timestamps(type: :utc_datetime) diff --git a/lib/chesstrainer/tags.ex b/lib/chesstrainer/tags.ex deleted file mode 100644 index 3ed1e22..0000000 --- a/lib/chesstrainer/tags.ex +++ /dev/null @@ -1,156 +0,0 @@ -defmodule Chesstrainer.Tags do - @moduledoc """ - The Tags context. - """ - - import Ecto.Query, warn: false - alias Chesstrainer.Repo - - alias Chesstrainer.Tags.Tag - - @doc """ - Returns the list of tags. - - ## Examples - - iex> list_tags() - [%Tag{}, ...] - - """ - def list_tags do - Repo.all(Tag) - end - - @doc """ - Gets a single tag. - - Raises `Ecto.NoResultsError` if the Tag does not exist. - - ## Examples - - iex> get_tag!(123) - %Tag{} - - iex> get_tag!(456) - ** (Ecto.NoResultsError) - - """ - def get_tag!(id), do: Repo.get!(Tag, id) - - @doc """ - Creates a tag. - - ## Examples - - iex> create_tag(%{field: value}) - {:ok, %Tag{}} - - iex> create_tag(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def create_tag(attrs) do - %Tag{} - |> Tag.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a tag. - - ## Examples - - iex> update_tag(tag, %{field: new_value}) - {:ok, %Tag{}} - - iex> update_tag(tag, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_tag(%Tag{} = tag, attrs) do - tag - |> Tag.changeset(attrs) - |> Repo.update() - end - - @doc """ - Deletes a tag. - - ## Examples - - iex> delete_tag(tag) - {:ok, %Tag{}} - - iex> delete_tag(tag) - {:error, %Ecto.Changeset{}} - - """ - def delete_tag(%Tag{} = tag) do - Repo.delete(tag) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking tag changes. - - ## Examples - - iex> change_tag(tag) - %Ecto.Changeset{data: %Tag{}} - - """ - def change_tag(%Tag{} = tag, attrs \\ %{}) do - Tag.changeset(tag, attrs) - end - - def search("", _category), do: [] - - def search(query, category) when is_binary(query) do - case String.trim(query) do - "" -> - [] - - query -> - pattern = "%#{query}%" - - Tag - |> where([t], ilike(t.name, ^pattern) and t.category == ^category) - |> order_by([t], asc: t.name) - |> limit(20) - |> Repo.all() - end - end - - @doc """ - Finds a tag by name (trimmed and case-insensitive) for a category, creating it - if it does not exist. - """ - def get_or_create(name, category) when is_binary(name) do - name = String.trim(name) - - if name == "" do - {:error, :blank_name} - else - case find_by_name(name, category) do - nil -> create_tag(%{name: name, category: category}) - tag -> {:ok, tag} - end - end - end - - defp find_by_name(name, category) do - downcased = String.downcase(name) - - Repo.one( - from t in Tag, - where: t.category == ^category and fragment("lower(?)", t.name) == ^downcased, - limit: 1 - ) - end - - @doc """ - Returns the names of the given tags, sorted. - """ - def names(tags) when is_list(tags) do - tags |> Enum.map(& &1.name) |> Enum.sort() - end -end diff --git a/lib/chesstrainer/themes.ex b/lib/chesstrainer/themes.ex new file mode 100644 index 0000000..81789b9 --- /dev/null +++ b/lib/chesstrainer/themes.ex @@ -0,0 +1,156 @@ +defmodule Chesstrainer.Themes do + @moduledoc """ + The Themes context. + """ + + import Ecto.Query, warn: false + alias Chesstrainer.Repo + + alias Chesstrainer.Themes.Theme + + @doc """ + Returns the list of themes. + + ## Examples + + iex> list_themes() + [%Theme{}, ...] + + """ + def list_themes do + Repo.all(Theme) + end + + @doc """ + Gets a single theme. + + Raises `Ecto.NoResultsError` if the Theme does not exist. + + ## Examples + + iex> get_theme!(123) + %Theme{} + + iex> get_theme!(456) + ** (Ecto.NoResultsError) + + """ + def get_theme!(id), do: Repo.get!(Theme, id) + + @doc """ + Creates a theme. + + ## Examples + + iex> create_theme(%{field: value}) + {:ok, %Theme{}} + + iex> create_theme(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_theme(attrs) do + %Theme{} + |> Theme.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a theme. + + ## Examples + + iex> update_theme(theme, %{field: new_value}) + {:ok, %Theme{}} + + iex> update_theme(theme, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_theme(%Theme{} = theme, attrs) do + theme + |> Theme.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a theme. + + ## Examples + + iex> delete_theme(theme) + {:ok, %Theme{}} + + iex> delete_theme(theme) + {:error, %Ecto.Changeset{}} + + """ + def delete_theme(%Theme{} = theme) do + Repo.delete(theme) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking theme changes. + + ## Examples + + iex> change_theme(theme) + %Ecto.Changeset{data: %Theme{}} + + """ + def change_theme(%Theme{} = theme, attrs \\ %{}) do + Theme.changeset(theme, attrs) + end + + def search("", _category), do: [] + + def search(query, category) when is_binary(query) do + case String.trim(query) do + "" -> + [] + + query -> + pattern = "%#{query}%" + + Theme + |> where([t], ilike(t.name, ^pattern) and t.category == ^category) + |> order_by([t], asc: t.name) + |> limit(20) + |> Repo.all() + end + end + + @doc """ + Finds a theme by name (trimmed and case-insensitive) for a category, creating it + if it does not exist. + """ + def get_or_create(name, category) when is_binary(name) do + name = String.trim(name) + + if name == "" do + {:error, :blank_name} + else + case find_by_name(name, category) do + nil -> create_theme(%{name: name, category: category}) + theme -> {:ok, theme} + end + end + end + + defp find_by_name(name, category) do + downcased = String.downcase(name) + + Repo.one( + from t in Theme, + where: t.category == ^category and fragment("lower(?)", t.name) == ^downcased, + limit: 1 + ) + end + + @doc """ + Returns the names of the given themes, sorted. + """ + def names(themes) when is_list(themes) do + themes |> Enum.map(& &1.name) |> Enum.sort() + end +end diff --git a/lib/chesstrainer/tags/tag.ex b/lib/chesstrainer/themes/theme.ex similarity index 68% rename from lib/chesstrainer/tags/tag.ex rename to lib/chesstrainer/themes/theme.ex index 242df98..86dad1e 100644 --- a/lib/chesstrainer/tags/tag.ex +++ b/lib/chesstrainer/themes/theme.ex @@ -1,8 +1,8 @@ -defmodule Chesstrainer.Tags.Tag do +defmodule Chesstrainer.Themes.Theme do use Ecto.Schema import Ecto.Changeset - schema "tags" do + schema "themes" do field :name, :string field :category, Ecto.Enum, values: [endgame: "endgame", opening: "opening", tactic: "tactic"] @@ -10,8 +10,8 @@ defmodule Chesstrainer.Tags.Tag do end @doc false - def changeset(tag, attrs) do - tag + def changeset(theme, attrs) do + theme |> cast(attrs, [:name, :category]) |> update_change(:name, fn name when is_binary(name) -> String.trim(name) @@ -19,6 +19,9 @@ defmodule Chesstrainer.Tags.Tag do end) |> validate_required([:name, :category]) |> validate_length(:name, min: 1, max: 60) - |> unique_constraint(:name, name: :tags_name_category_lower_index, message: "already exists") + |> unique_constraint(:name, + name: :themes_name_category_lower_index, + message: "already exists" + ) end end diff --git a/lib/chesstrainer_web/live/endgame_live/form.ex b/lib/chesstrainer_web/live/endgame_live/form.ex index 4bd3181..3549af5 100644 --- a/lib/chesstrainer_web/live/endgame_live/form.ex +++ b/lib/chesstrainer_web/live/endgame_live/form.ex @@ -2,7 +2,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do use ChesstrainerWeb, :live_view alias Chesstrainer.Repo - alias Chesstrainer.Tags + alias Chesstrainer.Themes alias Chesstrainer.FEN alias Chesstrainer.Endgames alias Chesstrainer.Endgames.Endgame @@ -22,14 +22,14 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
- <%= for tag <- @selected_tags do %> + <%= for theme <- @selected_themes do %> - {tag.name} + {theme.name} @@ -39,13 +39,13 @@ defmodule ChesstrainerWeb.EndgameLive.Form do