context: roles

This commit is contained in:
Liam P. White 2019-08-28 13:34:36 -04:00
parent fb9aa9a1f1
commit 282957bef5
3 changed files with 187 additions and 0 deletions

104
lib/philomena/roles.ex Normal file
View file

@ -0,0 +1,104 @@
defmodule Philomena.Roles do
@moduledoc """
The Roles context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Roles.Role
@doc """
Returns the list of roles.
## Examples
iex> list_roles()
[%Role{}, ...]
"""
def list_roles do
Repo.all(Role)
end
@doc """
Gets a single role.
Raises `Ecto.NoResultsError` if the Role does not exist.
## Examples
iex> get_role!(123)
%Role{}
iex> get_role!(456)
** (Ecto.NoResultsError)
"""
def get_role!(id), do: Repo.get!(Role, id)
@doc """
Creates a role.
## Examples
iex> create_role(%{field: value})
{:ok, %Role{}}
iex> create_role(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_role(attrs \\ %{}) do
%Role{}
|> Role.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a role.
## Examples
iex> update_role(role, %{field: new_value})
{:ok, %Role{}}
iex> update_role(role, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_role(%Role{} = role, attrs) do
role
|> Role.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Role.
## Examples
iex> delete_role(role)
{:ok, %Role{}}
iex> delete_role(role)
{:error, %Ecto.Changeset{}}
"""
def delete_role(%Role{} = role) do
Repo.delete(role)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking role changes.
## Examples
iex> change_role(role)
%Ecto.Changeset{source: %Role{}}
"""
def change_role(%Role{} = role) do
Role.changeset(role, %{})
end
end

View file

@ -0,0 +1,21 @@
defmodule Philomena.Roles.Role do
use Ecto.Schema
import Ecto.Changeset
schema "roles" do
field :name, :string
# fixme: rails polymorphic relation
field :resource_id, :integer
field :resource_type, :string
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(role, attrs) do
role
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,62 @@
defmodule Philomena.RolesTest do
use Philomena.DataCase
alias Philomena.Roles
describe "roles" do
alias Philomena.Roles.Role
@valid_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
def role_fixture(attrs \\ %{}) do
{:ok, role} =
attrs
|> Enum.into(@valid_attrs)
|> Roles.create_role()
role
end
test "list_roles/0 returns all roles" do
role = role_fixture()
assert Roles.list_roles() == [role]
end
test "get_role!/1 returns the role with given id" do
role = role_fixture()
assert Roles.get_role!(role.id) == role
end
test "create_role/1 with valid data creates a role" do
assert {:ok, %Role{} = role} = Roles.create_role(@valid_attrs)
end
test "create_role/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Roles.create_role(@invalid_attrs)
end
test "update_role/2 with valid data updates the role" do
role = role_fixture()
assert {:ok, %Role{} = role} = Roles.update_role(role, @update_attrs)
end
test "update_role/2 with invalid data returns error changeset" do
role = role_fixture()
assert {:error, %Ecto.Changeset{}} = Roles.update_role(role, @invalid_attrs)
assert role == Roles.get_role!(role.id)
end
test "delete_role/1 deletes the role" do
role = role_fixture()
assert {:ok, %Role{}} = Roles.delete_role(role)
assert_raise Ecto.NoResultsError, fn -> Roles.get_role!(role.id) end
end
test "change_role/1 returns a role changeset" do
role = role_fixture()
assert %Ecto.Changeset{} = Roles.change_role(role)
end
end
end