Every web application is organized around users: admins and end-users and other roles.
I like to create a document for every time I design a context and schemas.
# Users
mix phx.gen.html Accounts User users \
username:string \
first_name:string \
last_name:string \
slug:string \
email:string \
bio:text \
location:string \
website:string \
profile_image:string \
banner_image:string \
tagline:string \
snowflake:string \
admin:boolean \
editor:boolean \
host:boolean \
seller:boolean
This makes a documentary record of the code that is created by generators. After running above we get a migration which I now change to add a CITEXT extension for case insensitive email etc.
defmodule Folkbot.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
execute "CREATE EXTENSION IF NOT EXISTS citext"
create table(:users) do
add :username, :citext, null: false
add :first_name, :citext
add :last_name, :citext
add :slug, :string
add :email, :citext, null: false
add :bio, :text
add :location, :string
add :website, :string
add :profile_image, :string
add :banner_image, :string
add :tagline, :string
add :snowflake, :string
add :admin, :boolean, default: false, null: false
add :editor, :boolean, default: false, null: false
add :host, :boolean, default: false, null: false
add :seller, :boolean, default: false, null: false
timestamps()
end
end
end
We now have a working route for users and a schema.
defmodule Folkbot.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :admin, :boolean, default: false
field :banner_image, :string
field :bio, :string
field :editor, :boolean, default: false
field :email, :string
field :first_name, :string
field :host, :boolean, default: false
field :last_name, :string
field :location, :string
field :profile_image, :string
field :seller, :boolean, default: false
field :slug, :string
field :snowflake, :string
field :tagline, :string
field :username, :string
field :website, :string
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:username, :first_name, :last_name, :slug, :email, :bio, :location, :website, :profile_image, :banner_image, :tagline, :snowflake, :admin, :editor, :host, :seller])
|> validate_required([:username, :first_name, :last_name, :slug, :email, :bio, :location, :website, :profile_image, :banner_image, :tagline, :snowflake, :admin, :editor, :host, :seller])
end
end
Notice also I added a Snowflake ID field and that will be the next post.
I've skipped the steps from the Phoenix framework docs.
You can see the commit https://github.com/FolkBot/folkbot/commit/a1b10db81c6b6becd8edcf9c9e7c9c350b73729a
Image Credit: https://elements.envato.com/fighter-with-phoenix-fire-bird-on-background-ZS2QPSF