Skip to content

Commit

Permalink
add context for "App" Namespace to tutorial.md #53
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Mar 6, 2022
1 parent 6fb3a05 commit 4ef32da
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,99 @@ you should see see something similar to the following:

<!-- uncomment when ready for review
[![HitCount](http://hits.dwyl.com/dwyl/sleep_tutorial.svg?style=flat-square)](http://hits.dwyl.com/dwyl/sleep)
-->
-->

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: <br />
**a)** It's less to type and still provides clarity/context.<br />
**b)** <br />
**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".
<br />

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.

0 comments on commit 4ef32da

Please sign in to comment.