diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 00385fda..511d265e 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -101,4 +101,100 @@ defmodule Philomena.Tags do def change_tag(%Tag{} = tag) do Tag.changeset(tag, %{}) end + + alias Philomena.Tags.Implication + + @doc """ + Returns the list of tags_implied_tags. + + ## Examples + + iex> list_tags_implied_tags() + [%Implication{}, ...] + + """ + def list_tags_implied_tags do + Repo.all(Implication) + end + + @doc """ + Gets a single implication. + + Raises `Ecto.NoResultsError` if the Implication does not exist. + + ## Examples + + iex> get_implication!(123) + %Implication{} + + iex> get_implication!(456) + ** (Ecto.NoResultsError) + + """ + def get_implication!(id), do: Repo.get!(Implication, id) + + @doc """ + Creates a implication. + + ## Examples + + iex> create_implication(%{field: value}) + {:ok, %Implication{}} + + iex> create_implication(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_implication(attrs \\ %{}) do + %Implication{} + |> Implication.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a implication. + + ## Examples + + iex> update_implication(implication, %{field: new_value}) + {:ok, %Implication{}} + + iex> update_implication(implication, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_implication(%Implication{} = implication, attrs) do + implication + |> Implication.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Implication. + + ## Examples + + iex> delete_implication(implication) + {:ok, %Implication{}} + + iex> delete_implication(implication) + {:error, %Ecto.Changeset{}} + + """ + def delete_implication(%Implication{} = implication) do + Repo.delete(implication) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking implication changes. + + ## Examples + + iex> change_implication(implication) + %Ecto.Changeset{source: %Implication{}} + + """ + def change_implication(%Implication{} = implication) do + Implication.changeset(implication, %{}) + end end diff --git a/lib/philomena/tags/implication.ex b/lib/philomena/tags/implication.ex new file mode 100644 index 00000000..2cba7872 --- /dev/null +++ b/lib/philomena/tags/implication.ex @@ -0,0 +1,18 @@ +defmodule Philomena.Tags.Implication do + use Ecto.Schema + import Ecto.Changeset + + @primary_key false + + schema "tags_implied_tags" do + belongs_to :tag, Philomena.Tags.Tag, primary_key: true + belongs_to :implied_tag, Philomena.Tags.Tag, primary_key: true + end + + @doc false + def changeset(implication, attrs) do + implication + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/tags_test.exs b/test/philomena/tags_test.exs index b5b3f8b3..78b41e87 100644 --- a/test/philomena/tags_test.exs +++ b/test/philomena/tags_test.exs @@ -59,4 +59,61 @@ defmodule Philomena.TagsTest do assert %Ecto.Changeset{} = Tags.change_tag(tag) end end + + describe "tags_implied_tags" do + alias Philomena.Tags.Implication + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def implication_fixture(attrs \\ %{}) do + {:ok, implication} = + attrs + |> Enum.into(@valid_attrs) + |> Tags.create_implication() + + implication + end + + test "list_tags_implied_tags/0 returns all tags_implied_tags" do + implication = implication_fixture() + assert Tags.list_tags_implied_tags() == [implication] + end + + test "get_implication!/1 returns the implication with given id" do + implication = implication_fixture() + assert Tags.get_implication!(implication.id) == implication + end + + test "create_implication/1 with valid data creates a implication" do + assert {:ok, %Implication{} = implication} = Tags.create_implication(@valid_attrs) + end + + test "create_implication/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Tags.create_implication(@invalid_attrs) + end + + test "update_implication/2 with valid data updates the implication" do + implication = implication_fixture() + assert {:ok, %Implication{} = implication} = Tags.update_implication(implication, @update_attrs) + end + + test "update_implication/2 with invalid data returns error changeset" do + implication = implication_fixture() + assert {:error, %Ecto.Changeset{}} = Tags.update_implication(implication, @invalid_attrs) + assert implication == Tags.get_implication!(implication.id) + end + + test "delete_implication/1 deletes the implication" do + implication = implication_fixture() + assert {:ok, %Implication{}} = Tags.delete_implication(implication) + assert_raise Ecto.NoResultsError, fn -> Tags.get_implication!(implication.id) end + end + + test "change_implication/1 returns a implication changeset" do + implication = implication_fixture() + assert %Ecto.Changeset{} = Tags.change_implication(implication) + end + end end