I am just not sure how controllers should be scoped now (and views as well). By default generators output looks like controllers/order_controller.ex. But for example if you have 2 contexts - Sales and Support - should I create 2 scopes like controllers/sales/order_controller and controllers/support/order_controller, right?
Anyway this question is not about --no-schema flag. It is more about scoping. Now I have about 50 schemas connected to database and about 20 embedded schemas for forms. And how I working on it:
generate controller for web ui in main scope: Proj.OrderController
generate controller for admin ui: Proj.Admin.OrderController
generate controller for API: Proj.API.OrderController
So I have this separation but models/* looks messy now. And idea of per-context separations looks great for me. I am come from Django where re-usable apps is used and each have it’s own schemas, controllers (views in terms of django), templates, related code, etc.
But I am not sure how to split this code now. If I will have Admin context with all this models - I will end with same schemas mess. Should I have nested contexts like lib/admin/orders/product.ex then?
This looks great. I would say the only change you need is that instead of creating a schema, you will now create a context. It is likely that you want all of those controllers to consume from the same context and it is OK to add functions to the context that is needed for just one of those controllers.
The context is module and functions that are meant to satisfy the needs of your “web” context and other contexts. That’s it. There is no guideline that you need a context per controller, a context per namespace and what not. That’s exactly what we want to avoid.
This is true from implementation perspective, but if you change the standard in Phoenix, people will start adopting it everywhere. This is how it usually works. So the right question to ask is why is that change introduced and is the idea transferable to whole ecosystem? I believe you guys have some pretty good reasons to do it this way, but since it’s new, it would be useful to talk about those reasons.
You’re doing amazing work with the framework and I believe Phoenix is kind of driving the whole ecosystem(along with other important projects, like Nerves). That means Phoenix is a poster boy of Elixir and Phoenix conventions will spread, even if you do not intend it too.
It is not new. Phoenix projects already do not follow the path conventions on entries such as web/controllers/user_controller.ex (which would have required its module to be named MyApp.Web.Controllers.UserController).
Mix itself keeps Mix.Tasks.Escript.Build in lib/mix/tasks/escript.build.ex instead of lib/mix/tasks/escript/build.ex. We do it so you can quickly glance into lib/mix/tasks and see all available tasks.
Not caring about the directory structure has been an Elixir feature from before 1.0. Different projects have leveraged it to provide better code organization under circumstances they find it worth.
In this case, we find that colocating the context file with other files that belong to the same context is going to help exploration. Contrast how it works now:
We find the first easier to explore if the files are all in one place. Chris even had to “cheat” in his presentation by making those files visually together while most tree viewers wouldn’t put them side by side. And yes, the idea is transferable to the whole ecosystem.
Therefore it is totally fine if you follow Phoenix conventions elsewhere, especially if you believe they lead to a better code structure. And if you want to try other approaches than the ones mentioned above, then discuss it with your team and go ahead with your changes if you are in agreement.
Just tried out the generators with a small project. One thing that I was confused by, was prefixing the table names with the bounded context name. I have the following migration which was created by: mix phx.gen.html Builder Repository repositories url:string ssg:string
defmodule Staticrail.Repo.Migrations.CreateStaticrail.Builder.Repository do
def change do
create table(:builder_repositories) do
add :url, :string
add :ssg, :string
I wasn’t expecting the table names to be prefixed as these are the concrete version of the entities. We could have different views of these entities through different bounded contexts. I am not completely sure if it is a bug or if the table was intentionally named this way. The fix for this for me was simple, I just removed the :builder prefix in the migration. Not sure how others think about this.
It is intentionally named this way, as mentioned in mix help phx.gen.html, to drive the point home they belong to a particular context. You can customize it from the generators with the --table switch as well.
You need to run it from the _web app in your umbrella. However it will put the context in the wrong place, there is an open issue to fix that.
I like the old way much more but I think I will put all new files inside Services directory and keep old style or rename accounts.ex to service.ex, from my point of view accounts/accounts.ex should stay outside the accounts folder, you can consider folders as namespaces.
I think one of the reasons people are so thrilled about this release because it shows just how self-assured Phoenix is and how it does not feel the need to ‘take over’ your entire app (as many other frameworks do). Instead it is doing everything it can to complement, support and strengthen Elixir. This kind of cohesion is just awesome and I think it really resonates with people
In my experience I have found that Pragprog offer free upgrades unless it is a major rewrite of the book (usually in accordance with a major new version of the language/framework). But even that isn’t always strictly true (and is just a guess) - when I got Programming Ruby 1.9 they gave me PR2.0 free, when I got Agile Web Dev with Rails 3, they gave AWDWR 4 free. I don’t think I have ever paid for an upgrade tbh and I think this is one of the reasons why they sell so many more books than other publishers - many of us buy them knowing we want to read them at some point, rather than immediately, and are happy in the knowledge that we’ll probably get the latest version when we are ready for it (within reason).
Having said that, this is just a guess based on my experience - if in doubt email Pragprog.