Skip to content

Commit 488798d

Browse files
SteffenDEnelsonicrhcarvalho
authored
Split the generator command from the output to make it easier for beginners following the context guide (#6442)
* Split the generator command from the output to make it easier for beginners following the context guide. fixes #6441 * split commands from output in guides/data_modelling/in_context_relationships.md ref: #6442 * tidy console input sections in guides/data_modelling/in_context_relationships.md * continue splitting commands from output in guides/data_modelling/cross_context_boundaries.md * Update guides/data_modelling/in_context_relationships.md Co-authored-by: Rodolfo Carvalho <[email protected]> * Update guides/data_modelling/your_first_context.md --------- Co-authored-by: Nelson <[email protected]> Co-authored-by: n <[email protected]> Co-authored-by: Rodolfo Carvalho <[email protected]>
1 parent 6978284 commit 488798d

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

guides/data_modelling/cross_context_boundaries.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@ Most of the cart functionality is tied to a specific user. Therefore, in order t
1919

2020
```console
2121
mix phx.gen.auth Accounts User users
22+
```
23+
24+
You will see output similar to the following:
2225

26+
```console
2327
An authentication system can be created in two different ways:
2428
- Using Phoenix.LiveView (default)
2529
- Using Phoenix.Controller only
26-
Do you want to create a LiveView based authentication system? [Yn] n
30+
Do you want to create a LiveView based authentication system? [Yn]
31+
```
32+
33+
Type `n` followed by `Return` key,
34+
you will see output similar to:
2735

36+
```console
2837
...
2938
* creating lib/hello/accounts/scope.ex
3039
...

guides/data_modelling/in_context_relationships.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@ For now, categories will contain only textual information. Our first order of bu
77
> Sometimes it may be tricky to determine if two resources belong to the same context or not. In those cases, prefer distinct contexts per resource and refactor later if necessary. Otherwise you can easily end up with large contexts of loosely related entities. Also keep in mind that the fact two resources are related does not necessarily mean they belong to the same context, otherwise you would quickly end up with one large context, as the majority of resources in an application are connected to each other. To sum it up: if you are unsure, you should prefer separate modules (contexts).
88
99
```console
10-
$ mix phx.gen.context Catalog Category categories \
10+
mix phx.gen.context Catalog Category categories \
1111
title:string:unique --no-scope
12+
```
13+
14+
You will see the following output in your terminal:
1215

16+
```console
1317
You are generating into an existing context.
18+
19+
The Hello.Catalog context currently has 7 functions and 1 file in its directory.
20+
21+
* It's OK to have multiple resources in the same context as long as they are closely related.
22+
1423
...
15-
Would you like to proceed? [Yn] y
24+
25+
Would you like to proceed? [Yn]
26+
```
27+
28+
Type `y` followed by the `Return` key.
29+
You should see output similar to:
30+
31+
```console
1632
* creating lib/hello/catalog/category.ex
1733
* creating priv/repo/migrations/20250203192325_create_categories.exs
1834
* injecting lib/hello/catalog.ex
@@ -27,8 +43,12 @@ Remember to update your repository by running migrations:
2743
This time around, we used `mix phx.gen.context`, which is just like `mix phx.gen.html`, except it doesn't generate the web files for us. Since we already have controllers and templates for managing products, we can integrate the new category features into our existing web form and product show page. We can see we now have a new `Category` schema alongside our product schema at `lib/hello/catalog/category.ex`, and Phoenix told us it was *injecting* new functions in our existing Catalog context for the category functionality. The injected functions will look very familiar to our product functions, with new functions like `create_category`, `list_categories`, and so on. Before we migrate up, we need to do a second bit of code generation. Our category schema is great for representing an individual category in the system, but we need to support a many-to-many relationship between products and categories. Fortunately, ecto allows us to do this simply with a join table, so let's generate that now with the `ecto.gen.migration` command:
2844

2945
```console
30-
$ mix ecto.gen.migration create_product_categories
46+
mix ecto.gen.migration create_product_categories
47+
```
48+
49+
You will see output confirming the migration file was created:
3150

51+
```console
3252
* creating priv/repo/migrations/20250203192958_create_product_categories.exs
3353
```
3454

@@ -57,8 +77,12 @@ Next, we created indexes for our foreign keys, one of which is a unique index to
5777
With our migrations in place, we can migrate up.
5878

5979
```console
60-
$ mix ecto.migrate
80+
mix ecto.migrate
81+
```
6182

83+
You will see the following output confirming migration success:
84+
85+
```
6286
18:20:36.489 [info] == Running 20250222231834 Hello.Repo.Migrations.CreateCategories.change/0 forward
6387
6488
18:20:36.493 [info] create table categories
@@ -89,8 +113,12 @@ end
89113
We simply enumerate over a list of category titles and use the generated `create_category/1` function of our catalog context to persist the new records. We can run the seeds with `mix run`:
90114

91115
```console
92-
$ mix run priv/repo/seeds.exs
116+
mix run priv/repo/seeds.exs
117+
```
93118

119+
The output in the terminal confirms the `seeds.exs` executed successfully:
120+
121+
```console
94122
[debug] QUERY OK db=3.1ms decode=1.1ms queue=0.7ms idle=2.2ms
95123
INSERT INTO "categories" ("title","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["Home Improvement", ~N[2025-02-03 19:39:53], ~N[2025-02-03 19:39:53]]
96124
[debug] QUERY OK db=1.2ms queue=1.3ms idle=12.3ms

guides/data_modelling/your_first_context.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ To jump-start our catalog context, we'll use `mix phx.gen.html` which creates a
1515
```console
1616
$ mix phx.gen.html Catalog Product products title:string \
1717
description:string price:decimal views:integer
18+
```
19+
After executing the command, you should see output similar to the following:
1820

21+
```console
1922
* creating lib/hello_web/controllers/product_controller.ex
2023
* creating lib/hello_web/controllers/product_html/edit.html.heex
2124
* creating lib/hello_web/controllers/product_html/index.html.heex

0 commit comments

Comments
 (0)