Created
November 29, 2015 02:28
-
-
Save tispratik/0e990bc5e3ec796b36ff to your computer and use it in GitHub Desktop.
Revisions
-
tispratik created this gist
Nov 29, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ defmodule Myapp.Domain do import Plug.Conn import Ecto.Model @doc false def init(opts), do: opts @doc false def call(conn, _opts) do IO.inspect conn site_obj = Myapp.Repo.get_by(Myapp.Site, domain: conn.host) conn |> put_private(:site_obj, site_obj) end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ defmodule Myapp.Endpoint do ... ... plug Myapp.Domain plug Myapp.Router end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ defmodule Myapp.RegistrationController do use Myapp.Web, :controller alias Myapp.User alias Myapp.UserSite # plug :scrub_params, "user" when action in [:create] def new(conn, _params) do changeset = User.changeset(%User{}) render conn, changeset: changeset end def create(conn, %{"user" => user_params}) do user_changeset = User.changeset(%User{}, user_params) if user_changeset.valid? do Repo.transaction fn -> user = Repo.insert!(user_changeset) user_site = Ecto.Model.build(user, :user_sites, site: site_id(conn)) Repo.insert!(user_site) conn |> put_flash(:info, "Your account was created") |> put_session(:current_user, user) |> redirect(to: "/") end else conn |> render("new.html", changeset: user_changeset) end end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ defmodule Myapp.RegistrationControllerTest do use Myapp.ConnCase alias Myapp.Site @valid_attrs %{email: "[email protected]", passwd: "Abcdef123", passwd_confirmation: "Abcdef123"} @invalid_attrs %{} setup do Repo.insert!(%Site{name: "Abc", slug: "abc", domain: "abc.com", license: "123", address: "Abc", email: "[email protected]", phone1: "123-456-7890"}) conn = %{conn() | host: "abc.com"} {:ok, conn: conn} end test "creates resource and redirects when data is valid", %{conn: conn} do # conn = post conn(), "abc.com/register", user: @valid_attrs conn = post conn, registration_path(conn, :create), user: @valid_attrs assert redirected_to(conn) == "/" # assert Repo.get_by(User, @valid_attrs) end end