Today I Learned

TIL, 2018-02-21, Programming Phoenix Day 3.

Musings

  • Ruby help Reference Reference
    • Within irb, help String.
    • In the shell, $ ri String.
    • gem rdoc --all --ri --no-rdoc to generate documentation.
    • $ri ActiveRecord::Base.
    • $ ri -i: Interactive gem.
    • pry-doc, then show-doc, or in Pry, you can do > ri String to get the documentation without Internet.
  • On why JS is called JavaScript: Reference
    • Mocha, then LiveScript, then it became JavaScript when Netscape/Sun got together. It’s like a licensing deal that can complement Java.
  • Looked into Netlify, it seems nice re: static sites, I’ll look into it.
  • Vim digraphs:
    • Ctrl+K in insert mode. <- ← , -> →, -! ↑, -v ↓.
  • Looking through gems:
    • isolator: This looks good, I’ll test drive it out in daryllxd.
    • uniform_notifier: customized logger!
  • Looked into ActiveRecord/ActiveModel attributes.
  • Git extras. Reference TJ Holowaychuk is such a baller.
  • Watched some RubyTapas videos:
    • 42: Streaming. I did not know about Enumerable each inside of a block. Reference
    • 45: Default values. I sort of know about this. Reference
    • 52: The end of mocking. Do not mock stuff in an adapter. I completely agree. Reference
  • Elixir static code analysis: Credo. Reference
  • Elixir casts: Plugs. Reference
  • Elixir packages: Awesome Elixir. Reference

On installing a package: I can’t take HTML anymore! Lol

  • $ mix deps.get.
  • “Gemfile” is in mix.exs.
  • I installed phoenix-slime Reference. Turns out I have to explicitly set the versions, which is cool!
# This is what the Gemfile looks like
defp deps do
  [
    {:phoenix, "~> 1.3.0-rc"},
    {:phoenix_pubsub, "~> 1.0"},
    {:phoenix_ecto, "~> 3.2"},
    {:postgrex, ">= 0.0.0"},
    {:phoenix_html, "~> 2.10"},
    {:phoenix_live_reload, "~> 1.0", only: :dev},
    {:phoenix_slime, "~> 0.9.0"},
    {:gettext, "~> 0.11"},
    {:cowboy, "~> 1.0"}
  ]
end

Programming Phoenix

Forms

defmodule User do
  def changeset(model, params \\ :invalid) do
    model
    |> cast(params, ~w(name username), [])
    |> validate_length(:username, min: 1, max: 20)
  end
end
  • Accept a User struct/parameters.
  • Tell Ecto that name and username are required. cast
  • No optional fields.
  • Pipe cast to validate_length.
  • In Rails, validations are directly in the schema. Why not in Elixir?
    • Different contexts of updating. Ex: Log in via Facebook.
    • Decouple update policy from the schema, because one size fits all does not fit in this context.
  • Helper function form_for takes care of security, UTF-8 encoding.
  • This is how a basic new form is supposed to look like:
h1 New User
= if @changeset.action do
  .alert.alert-danger
    p Oopsies

= form_for @changeset, user_path(@conn, :create), fn f ->
  .form-group
    = text_input f, :name, placeholder: "Name", class: "form-control"
    = error_tag f, :name
  .form-group
    = text_input f, :email, placeholder: "Email", class: "form-control"
    = error_tag f, :email
  .form-group
    = password_input f, :password, placeholder: "Password", class: "form-control"
    = error_tag f, :password
  = submit "Create User", class: "btn btn-primary"

This project is maintained by daryllxd