Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is context in elixir phoenix html generator

I wanted to have a look at phoenix 1.3-dev. I clone phoenix from repository and followed the phoenix/installer/README.md to build archive.

phoenix/installer/README.md (for reference)

$ cd installer
$ MIX_ENV=prod mix archive.build
$ mix archive.install

I created a new phoenix app by mix phx.new blog but when i tried to create a simple html scaffold by mix phx.gen.html Post posts title body:text then i get following error message:

** (Mix) Expected the schema argument, ":\"Elixir.Company.companies\"", to be a valid module name

mix phx.gen.html and mix phx.gen.json expect a context module name,
followed by singular and plural names of the generated resource, ending
with any number of attributes:

    mix phx.gen.html Accounts User users name:string
    mix phx.gen.json Accounts User users name:string

The Accounts context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Blog.User).

My question is why should add a context module? how and where it helps? if you know something about this, please describe a usecase so i can understand.

like image 312
Murtza Avatar asked Feb 05 '23 00:02

Murtza


1 Answers

The context module allows us to remove Repo calls from the controller to their own module, so that there can be separation between concerns.

For more, try listening to this talk by Chris McCord:

https://www.youtube.com/watch?v=tMO28ar0lW8&index=1&list=PLE7tQUdRKcyaMEekS1T32hUw19UxzqBEo

and check out the comments in Phoenix code:

https://github.com/phoenixframework/phoenix/blob/07f1bbc7627117adc23ec8355a88465859e4302d/lib/mix/tasks/phx.gen.context.ex

like image 133
neon Avatar answered Feb 22 '23 06:02

neon