Skip to content

Instantly share code, notes, and snippets.

@talum
Last active August 2, 2017 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talum/8fee91f1a460af66485a195d1da797dc to your computer and use it in GitHub Desktop.
Save talum/8fee91f1a460af66485a195d1da797dc to your computer and use it in GitHub Desktop.
programming phoenix ch3/4 notes

Programming Phoenix Ch 3: Controllers & Views

connection the whole universe of things we need to know about the user's request (a struct, map with known set of fields)

connection
|> endpoint
|> router
|> browser_pipeline
|> UsersController

Structs

struct Elixir's main abstraction for working with structured data

  • built on top of maps
  • provide protection for bad keys at compile time (instead of runtime like maps)
  • struct is a map with a __struct__ key

Repository

repository an API for holding things

Controllers

view module containing rendering functions that convert data into a format the end data will consume (HTML/JSON)

template function on that module compiled from a file containing a raw makrup language and embedded Elixir code

Templates

  • helpers exist
  • after compilation, templates are functions
  • Phoenix builds templates using linked lists (no copies of strings)
  • cache templates at the hardware level
  • similar to partials, can render another template from a template
  • Phoenix.View.render("template_name", _assigns, {locals: locals})

Layouts

  • Controller renders layout view, which renders actual template
  • each template receives @view_module and @view_template

Questions

  • Can we talk more about caching at the hardware level?

Programming Phoenix Ch 4: Ecto & Changesets

Ecto

  • the Elixir framework for persistence
  • changesets hold all changes you want to perform on the database
  • migrations used to make the database reflect structure of app

Models

  • "Just as a controller is a layer to transform requests and responses according to a communication protocol, the model is nothing more than a group of functions to transform data according to our business requirements" (57)
  • Models can manipulate other structures such as changesets and queries
  • changesets let Ecto manage record changes, cast parameters, and perform validations
  • cast on changeset checks for required fields and casts required and optional values to schema types and rejects everything else
  • Ecto.Changeset defines cast and validate_length
  • Why changesets? One size does not fit all for update strategies.
  • Changesets let you decouple update policy from schema
  • changeset had validation errors because Ecto changeset carries validations and stores info for later use. Changesets track changes.
  • changeset as bucket to hold everything related to db change, before and after persistence
  • "If no parameters are specified, we can’t just default to an empty map, because that would be indistinguishable from a blank form submission. Instead, we default params to the :empty atom. By convention, Ecto will produce an invalid changeset, with empty parameters."
  • pass a changeset into the form helper rather than a model.

Updating Data

  • able to update data via Repo.insert e.g. Repo.insert(%User{ ...> name: "José", username: "josevalim", password_hash: "<3<3elixir" ...> })
  • but should prefer to use a changeset e.g. changeset = User.changeset(%User{}, user_params) {:ok, user} = Repo.insert(changeset)
  • validation errors stored in @changeset.action
  • example of multiple changesets?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment