Skip to content

Commit

Permalink
Phoenix: add User model
Browse files Browse the repository at this point in the history
  • Loading branch information
williamn committed Nov 29, 2015
1 parent 4177422 commit 22c623f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
@@ -0,0 +1,13 @@
defmodule Notejam.Repo.Migrations.CreateUser do
use Ecto.Migration

def change do
create table(:users) do
add :email, :string
add :password, :string

timestamps
end

end
end
18 changes: 18 additions & 0 deletions phoenix/notejam/test/models/user_test.exs
@@ -0,0 +1,18 @@
defmodule Notejam.UserTest do
use Notejam.ModelCase

alias Notejam.User

@valid_attrs %{email: "some content", password: "some content"}
@invalid_attrs %{}

test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
end
24 changes: 24 additions & 0 deletions phoenix/notejam/web/models/user.ex
@@ -0,0 +1,24 @@
defmodule Notejam.User do
use Notejam.Web, :model

schema "users" do
field :email, :string
field :password, :string

timestamps
end

@required_fields ~w(email password)
@optional_fields ~w()

@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end

0 comments on commit 22c623f

Please sign in to comment.