From 4ef32da966a88254cd50bbc287509eb856cb0f78 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sun, 6 Mar 2022 21:29:28 +0000 Subject: [PATCH] add context for "App" Namespace to tutorial.md #53 --- tutorial.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/tutorial.md b/tutorial.md index f7150fa..2554632 100644 --- a/tutorial.md +++ b/tutorial.md @@ -87,4 +87,99 @@ you should see see something similar to the following: \ No newline at end of file +--> + +So far so good. + + +#### Quick Note on App Naming Conventions + +You will often see Phoenix apps with the name of the app in the project files +e.g: in the **`Chat`** example +[github.com/dwyl/phoenix-chat-example](https://github.com/dwyl/phoenix-chat-example) + +[lib/chat/repo.ex#L1](https://github.com/dwyl/phoenix-chat-example/blob/main/lib/chat/repo.ex#L1) + +```elixir +defmodule Chat.Repo do + use Ecto.Repo, + otp_app: :chat, + adapter: Ecto.Adapters.Postgres +end +``` + +[lib/chat_web/router.ex#L1](https://github.com/dwyl/phoenix-chat-example/blob/main/lib/chat_web/router.ex#L1) +```elixir +defmodule ChatWeb.Router do + use ChatWeb, :router + +... etc. +``` + +Having `Chat` or `ChatWeb` namespace can be useful +if you're working on multiple Phoenix apps simultaneously +and need to context switch. +That's why we use the `Auth` namespace +in our Authentication App: +[github.com/dwyl/auth](https://github.com/dwyl/auth) + +e.g: +[lib/auth_web/controllers/auth_controller.ex#L1](https://github.com/dwyl/auth/blob/main/lib/auth_web/controllers/auth_controller.ex#L1) + +```elixir +defmodule AuthWeb.AuthController do +... +``` + +The reasons why _this_ app is namespaced **`App`** are:
+**a)** It's less to type and still provides clarity/context.
+**b)**
+**c)** If we _succeed_ in building a sleep-tracking app, +we will re-use some of the code in our "main" App. +[github.com/dwyl/app](https://github.com/dwyl/app) +This means it's easy to +"lift and shift" the code & tests +without needing to waste time with "find & replace". +
+ +If you prefer to namespace your app differently, go for it!s + +## Show Me the Code! 👩‍💻 + +Open the project in your editor/IDE of choice. + +### Create `/live` Directory + +Since we are using Phoenix LiveView for this App, +we need to create a new `live` directory +with the following path: +`lib/app_web/live` + +### Create `app_live.ex` File + +Inside that newly created `/live` directory, +create a new file called +`app_live.ex` +with the path: +`lib/app_web/live/app_live.ex` + +```elixir +defmodule App.AppLive do + use AppWeb, :live_view + + def mount(_params, _session, socket) do + {:ok, socket} + end + + def render(assigns) do + AppWeb.AppView.render("messages.html", assigns) + end +end +``` + + +### Update `router.ex` + +and navigate to the +`lib/app_web/router.ex` file. +