Created
July 1, 2016 04:32
-
-
Save nguyenvinhlinh/e5b9b5258f32d4954a91cfd1c7bed36b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Plug.TeacherProfileControl do | |
import Plug.Conn | |
import Phoenix.Controller | |
alias Iea.User | |
use Plug.TeacherProfileControl | |
def init(opt) do | |
opt | |
end | |
defmacro is_edit_path(conn) do | |
conn.request_path == Iea.Router.Helpers.teacher_user_path(conn, :edit_profile) | |
end | |
def call(conn, _params) when is_edit_path(conn) do | |
conn | |
end | |
def call(conn, _params) do | |
user = conn.assigns.current_user | |
changeset = case user.contract_type do | |
"full time" -> | |
User.fulltime_teacher_update_changeset(user, %{}) | |
"part time" -> | |
User.parttime_teacher_update_changeset(user, %{}) | |
end | |
case changeset.errors do | |
[] -> conn | |
_ -> | |
conn | |
|> redirect(to: Iea.Router.Helpers.teacher_user_path(conn, :edit_profile)) | |
|> halt | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment