📎 Webclip
Phoenix, to the basics and beyond.
The post is a brief guide to practical Phoenix topics that come after the default generators. It assumes prior knowledge of Elixir, Phoenix, and the command line, then moves from development environments to models, custom primary keys, validators, and the Repository Pattern.
Reading notes#
- It assumes previous knowledge of Elixir, Phoenix, and the command line tools.
- Emacs with Alchemist is presented as a strong environment for Elixir and Phoenix development, especially for users who want test running and code evaluation from the editor.
- SublimeText is the author’s preferred editor on OS X, with the Elixir bundle and SublimeREPL providing syntax highlighting, snippets, test execution, and an IEx shell.
- Atom is described as uneven, with useful packages but weaker defaults and less convincing syntax highlighting.
- Phoenix models are described as a thin layer around Ecto models, with a lightweight schema DSL.
use App.Web, :modelimports code defined inweb/web.ex, and Phoenix defaults toEcto.modelplus imports fromEcto.ChangesetandEcto.Query.- Custom primary keys are introduced by changing the migration and model so the key is not autogenerated and uses a
:uuidor:binary_idformat. - Validators are presented as functions that take a changeset and return a changeset, and the default validators listed by Ecto are shown as building blocks.
- The example validation code for an email model chains
cast,validate_name, andvalidate_address, and can be reused in other modules or applications. - Phoenix does not provide an ORM-style layer and instead relies on the Repository Pattern together with
Ecto.Query. - The author shows basic
Repo.all,Repo.get,Repo.get_by, andRepo.insertcalls. - An Active Record-like wrapper can be added to models with normal functions, and the same pattern can be generated for multiple models.
- Elixir macros are used to inject that wrapper at compile time through a
__using__macro. - The post ends by saying the next topic will be the assets pipeline and long running processes.
