Skip to content

Commit

Permalink
create groups_people schema+migration for #220
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 21, 2022
1 parent a94e0a2 commit 23d33e4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
84 changes: 83 additions & 1 deletion BUILDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,86 @@ Randomized with seed 796477

## 10.6 Group _Members_

Now that we have groups
Now that we have **`groups`**,
we need a way to add **`people`** (members)
to those **`groups`**.

Run the following command in your terminal:

```sh
mix phx.gen.schema GroupPeople group_people group_id:references:groups person_id:references:people people_role_id:references:people_roles
```

That will create two files:

`lib/auth/group_people.ex`
(schema)
and
`priv/repo/migrations/20221021213907_create_group_people.exs`
(migration)

For reference, this is the schema that is created:

```elixir
defmodule Auth.GroupPeople do
use Ecto.Schema
import Ecto.Changeset

schema "group_people" do

field :group_id, :id
field :person_id, :id
field :people_role_id, :id

timestamps()
end

@doc false
def changeset(group_people, attrs) do
group_people
|> cast(attrs, [])
|> validate_required([])
end
end
```

This schema is enough for us to achieve _everything_ we need/want.
By leveraging the previously created `roles` and `people_roles`
tables we have a built-in full-featured **`RBAC`** for `groups`.

> **Note**: If anything is unclear,
please keep reading for answers.
The UI/UX below will show how simple yet powerful this schema is.
But as always,
if anything is _still_ confusing
[***please ask questions***](https://github.com/dwyl/auth/issues/)
they **benefit _everyone_**! 🙏

Here's the migration:

```elixir
defmodule Auth.Repo.Migrations.CreateGroupPeople do
use Ecto.Migration

def change do
create table(:group_people) do
add :group_id, references(:groups, on_delete: :nothing)
add :person_id, references(:people, on_delete: :nothing)
add :people_role_id, references(:people_roles, on_delete: :nothing)

timestamps()
end

create index(:group_people, [:group_id])
create index(:group_people, [:person_id])
create index(:group_people, [:people_role_id])
end
end
```

Run the migration:

```sh
mix ecto.migrate
```

9 changes: 1 addition & 8 deletions assets/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ module.exports = {
'../lib/*_web/**/*.*ex',
"../deps/petal_components/**/*.*ex",
],
theme: {
extend: {
colors: {
primary: colors.blue,
secondary: colors.pink,
},
},
},
theme: {},
plugins: [
require('@tailwindcss/forms'),
plugin(({addVariant}) => addVariant('phx-no-feedback', ['&.phx-no-feedback', '.phx-no-feedback &'])),
Expand Down
20 changes: 20 additions & 0 deletions lib/auth/group_people.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Auth.GroupPeople do
use Ecto.Schema
import Ecto.Changeset

schema "group_people" do

field :group_id, :id
field :person_id, :id
field :people_role_id, :id

timestamps()
end

@doc false
def changeset(group_people, attrs) do
group_people
|> cast(attrs, [])
|> validate_required([])
end
end
4 changes: 2 additions & 2 deletions lib/auth_web/live/groups_live.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1 class="w-full h-full text-center text-7xl text-white font-bold
<h1 class="w-full text-center text-2xl text-white font-bold
bg-gradient-to-r from-green-400 to-blue-500 p-4">
Groups LiveView!
Groups
</h1>
1 change: 1 addition & 0 deletions priv/repo/migrations/20221017220747_create_groups.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Auth.Repo.Migrations.CreateGroups do
add :name, :binary
add :desc, :binary
add :kind, :integer
add :app_id, references(:apps, on_delete: :nothing)

timestamps()
end
Expand Down
17 changes: 17 additions & 0 deletions priv/repo/migrations/20221021211337_create_group_people.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Auth.Repo.Migrations.CreateGroupPeople do
use Ecto.Migration

def change do
create table(:group_people) do
add :group_id, references(:groups, on_delete: :nothing)
add :person_id, references(:people, on_delete: :nothing)
add :people_role_id, references(:people_roles, on_delete: :nothing)

timestamps()
end

create index(:group_people, [:group_id])
create index(:group_people, [:person_id])
create index(:group_people, [:people_role_id])
end
end

0 comments on commit 23d33e4

Please sign in to comment.