Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Commit

Permalink
replace validate_unique to unique_constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa committed Aug 16, 2015
1 parent 81798f9 commit ff53f15
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ defmodule WebQaVote.Mixfile do
[
{:comeonin, "~>1.0.5"},
{:phoenix, "~> 0.16"},
{:phoenix_ecto, "~> 0.8.0"},
{:phoenix_ecto, "~> 1.1"},
{:postgrex, ">= 0.9.1"},
{:phoenix_html, "~> 1.4"},
{:phoenix_html, "~> 2.1"},
{:phoenix_live_reload, "~> 0.5", only: :dev},
{:cowboy, "~> 1.0"},
{:guardian, "~> 0.4.0"},
Expand Down
8 changes: 4 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"cowlib": {:hex, :cowlib, "1.0.1"},
"decimal": {:hex, :decimal, "1.1.0"},
"earmark": {:hex, :earmark, "0.1.17"},
"ecto": {:hex, :ecto, "0.15.0"},
"ecto": {:hex, :ecto, "0.16.0"},
"ex_doc": {:hex, :ex_doc, "0.8.2"},
"fs": {:hex, :fs, "0.9.2"},
"guardian": {:hex, :guardian, "0.4.1"},
"joken": {:hex, :joken, "0.14.1"},
"phoenix": {:hex, :phoenix, "0.16.1"},
"phoenix_ecto": {:hex, :phoenix_ecto, "0.8.1"},
"phoenix_html": {:hex, :phoenix_html, "1.4.0"},
"phoenix_ecto": {:hex, :phoenix_ecto, "1.1.0"},
"phoenix_html": {:hex, :phoenix_html, "2.1.1"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "0.6.0"},
"plug": {:hex, :plug, "0.14.0"},
"plug": {:hex, :plug, "1.0.0"},
"poison": {:hex, :poison, "1.4.0"},
"poolboy": {:hex, :poolboy, "1.5.1"},
"postgrex": {:hex, :postgrex, "0.9.1"},
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20150730140656_create_user.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule WebQaVote.Repo.Migrations.CreateUser do

timestamps
end
create index(:users, [:email])
create unique_index(:users, [:email])

end
end
6 changes: 4 additions & 2 deletions test/models/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ defmodule WebQaVote.UserTest do
end

test "should email is unique" do
changeset = User.create_changeset(%User{}, @valid_attrs)
# assert {:message, "Already anyone use same email."} in errors_on(%Device{}, attrs)
User.create_changeset(%User{}, @valid_attrs)
|> Repo.insert!
{:error, changeset} = Repo.insert User.create_changeset(%User{}, @valid_attrs)
assert changeset.errors == [email: "Already anyone use same email."]
end

test "should true if User have a user" do
Expand Down
29 changes: 18 additions & 11 deletions web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ defmodule WebQaVote.UserController do
changeset = User.create_changeset(%User{}, user_params)

if changeset.valid? do
user = Repo.insert!(changeset)

conn
|> put_flash(:info, "User created successfully.")
|> Guardian.Plug.sign_in(user, :token, perms: %{ default: Guardian.Permissions.max })
|> redirect(to: user_path(conn, :index))
case Repo.insert(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "User created successfully.")
|> Guardian.Plug.sign_in(user, :token, perms: %{ default: Guardian.Permissions.max })
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
else
render(conn, "new.html", changeset: changeset)
end
Expand All @@ -62,11 +65,15 @@ defmodule WebQaVote.UserController do
changeset = User.update_changeset(user, user_params)

if changeset.valid? do
Repo.update!(changeset)

conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :index))
case Repo.update(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
put_flash(conn, :error, "failed to update")
|> render("edit.html", user: user, changeset: changeset)
end
else
put_flash(conn, :error, "failed to update")
|> render("edit.html", user: user, changeset: changeset)
Expand Down
4 changes: 2 additions & 2 deletions web/models/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ defmodule WebQaVote.User do
def create_changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields)
|> validate_unique(:email, [on: Repo, message: "Already anyone use same email."])
|> unique_constraint(:email, [message: "Already anyone use same email."])
end

def update_changeset(model, params \\ :empty) do
model
|> cast(params, ~w(), @required_fields)
|> validate_unique(:email, [on: Repo, message: "Already anyone use same email."])
|> unique_constraint(:email, [message: "Already anyone use same email."])
end

def login_changeset(model), do: model |> cast(%{}, ~w(), @login_field)
Expand Down

0 comments on commit ff53f15

Please sign in to comment.