Slide 1

Slide 1 text

Phoenix & Ember Living Together Mass Hysteria!

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

@bcardarella

Slide 4

Slide 4 text

@DockYard

Slide 5

Slide 5 text

I run… Boston Elixir Boston Ember

Slide 6

Slide 6 text

I run… Boston.ex Boston Ember

Slide 7

Slide 7 text

I’m here to talk about Modern Full Stack Application Development

Slide 8

Slide 8 text

I’m here to talk about Modern Full Stack Application Development

Slide 9

Slide 9 text

The way it used to be

Slide 10

Slide 10 text

The way it used to be

Slide 11

Slide 11 text

The way it used to be

Slide 12

Slide 12 text

The way it used to be

Slide 13

Slide 13 text

The way it used to be

Slide 14

Slide 14 text

The way it used to be

Slide 15

Slide 15 text

The way it used to be

Slide 16

Slide 16 text

The way it used to be

Slide 17

Slide 17 text

The way it used to be

Slide 18

Slide 18 text

This talk isn’t client-side framework specific

Slide 19

Slide 19 text

This talk isn’t client-side framework specific (mostly)

Slide 20

Slide 20 text

This talk is about two apps

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

priv !"" static #"" css $ #"" app.css $ !"" app.css.map #"" images $ #"" favicon.ico $ !"" phoenix.png #"" js $ #"" app.js $ !"" app.js.map !"" robots.txt

Slide 24

Slide 24 text

priv !"" static #"" css $ #"" app.css $ !"" app.css.map #"" images $ #"" favicon.ico $ !"" phoenix.png #"" js $ #"" app.js $ !"" app.js.map !"" robots.txt

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

backend/ frontend/ foo-app/

Slide 30

Slide 30 text

foo-app-backend/ foo-app-frontend/ Two separate repos

Slide 31

Slide 31 text

ember serve —proxy http://localhost:4000

Slide 32

Slide 32 text

ember serve —proxy http://localhost:4000 requests to /api/*

Slide 33

Slide 33 text

ember serve —proxy http://localhost:4000 requests to /api/* data!

Slide 34

Slide 34 text

ember serve —proxy http://localhost:4000 response back data!

Slide 35

Slide 35 text

Talking with

Slide 36

Slide 36 text

* standardized schema for JSON based APIs * is very verbose * requires special MIME type

Slide 37

Slide 37 text

requires special MIME type application/vnd.api+json

Slide 38

Slide 38 text

defmodule MyApp.Router do scope “/api” MyApp do resources “/accounts”, AccountsController end end

Slide 39

Slide 39 text

defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] end scope “/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end

Slide 40

Slide 40 text

Copied from plug’s documentation # https://github.com/elixir-lang/plug/tree/master/lib/plug/mime.ex Maps MIME types to file extensions and vice versa. MIME types can be extended in your application configuration as follows: config :plug, :mimes, %{ "application/vnd.api+json" => ["json-api"] } After adding the configuration, Plug needs to be recompiled. If you are using mix, it can be done with: $ touch deps/plug/mix.exs $ mix deps.compile plug

Slide 41

Slide 41 text

Copied from plug’s documentation # config/config.exs config :plug, :mimes, %{ "application/vnd.api+json" => ["json-api"] } Add the MIME type to your Phoenix config

Slide 42

Slide 42 text

Copied from plug’s documentation $ touch deps/plug/mix.exs $ mix deps.compile plug Force plug to recompile

Slide 43

Slide 43 text

{ "data": { "type": "authors", "attributes": { "first-name": "Brian", "last-name": "Cardarella" } } }

Slide 44

Slide 44 text

{ "data": { "type": "authors", "attributes": { "first-name": "Brian", "last-name": "Cardarella" } } }

Slide 45

Slide 45 text

{ "data": { "type": "authors", "attributes": { "first-name": "Brian", "last-name": "Cardarella" } } } Deserialize into snake case

Slide 46

Slide 46 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 47

Slide 47 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 48

Slide 48 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 49

Slide 49 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 50

Slide 50 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 51

Slide 51 text

defmodule MyApp.Deserialize do def init(options) do options end def call(%Plug.Conn{method: "POST"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PUT"}=conn, _opts) do _deserialize(conn) end def call(%Plug.Conn{method: "PATCH"}=conn, _opts) do _deserialize(conn) end def call(conn, _opts), do: conn defp _deserialize(%Plug.Conn{}=conn) do Map.put(conn, :params, _deserialize(conn.params)) end defp _deserialize(%{}=params) do Enum.into(params, %{}, fn({key, value}) -> { _underscore(key), _deserialize(value) } end) end defp _deserialize(value), do: value defp _underscore(key), do: String.replace(key, "-", "_") end

Slide 52

Slide 52 text

defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] end scope “/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end

Slide 53

Slide 53 text

defmodule MyApp.Router do pipeline :api do accepts, [“json-api”] plug MyApp.Deserialize end scope “/api” MyApp do pipe_through :api resources “/accounts”, AccountsController end end

Slide 54

Slide 54 text

%{ “data" => %{ “type" => "authors", “attributes" => %{ “first_name” => "Brian", “last_name” => "Cardarella" } } }

Slide 55

Slide 55 text

Actions

Slide 56

Slide 56 text

Actions def create(conn, params)

Slide 57

Slide 57 text

%{ “data" => %{ “type" => "authors", “attributes" => %{ “first_name” => "Brian", “last_name” => "Cardarella" } } }

Slide 58

Slide 58 text

%{ “data" => %{ “type" => "authors", “attributes" => %{ “first_name” => "Brian", “last_name” => "Cardarella" } } }

Slide 59

Slide 59 text

%{ “data" => %{ “type" => "authors", “attributes" => %{ “first_name” => "Brian", “last_name” => "Cardarella" } } }

Slide 60

Slide 60 text

Actions def create(conn, %{“data” => %{“attributes” => data, “type” => “authors”}})

Slide 61

Slide 61 text

Actions def create(conn, %{“data” => %{“attributes” => data, “type” => “authors”}})

Slide 62

Slide 62 text

Actions def create(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}})

Slide 63

Slide 63 text

Actions def create(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}})

Slide 64

Slide 64 text

Actions def create(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def post(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def put(conn, %{“data” => %{“attributes” => data, “type” => “authors”}}) def delete(conn, %{“id” => id}) def show(conn, %{“id” => id}) def index(conn, _params)

Slide 65

Slide 65 text

Actions def show(conn, %{“id” => id}) do author = MyApp.Repo.get!(MyApp.Author, id) render conn, model: author end

Slide 66

Slide 66 text

View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes [:first_name, :last_name] end web/views/authors_view.ex

Slide 67

Slide 67 text

View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes [:first_name, :last_name] end web/views/authors_view.ex

Slide 68

Slide 68 text

View defmodule MyApp.AuthorsView do use PhoenixExample.Web, :view use JaSerializer.PhoenixView attributes [:first_name, :last_name] end web/views/authors_view.ex https://github.com/AgilionApps/ja_serializer

Slide 69

Slide 69 text

data! requests to /api/* response back

Slide 70

Slide 70 text

requests to /api/* response back Thanks brah!

Slide 71

Slide 71 text

requests to /api/* response back No problemo!

Slide 72

Slide 72 text

testing

Slide 73

Slide 73 text

Test each part in isolation

Slide 74

Slide 74 text

Test each part in isolation Test the complete system

Slide 75

Slide 75 text

Unit Tests Integration Tests

Slide 76

Slide 76 text

API Unit Tests test !"" api

Slide 77

Slide 77 text

API Unit Tests test "`create` insert into the database and return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end

Slide 78

Slide 78 text

API Unit Tests test "`create` insert into the database and return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end

Slide 79

Slide 79 text

%{ “data" => %{ “type" => "authors", “attributes" => %{ “first_name” => "Brian", “last_name” => "Cardarella" } } }

Slide 80

Slide 80 text

def json_for(type, attributes) do %{ "data" => %{ "type" => Atom.to_string(type) |> String.replace("_", "-"), "attributes" => attributes }, "format" => "json-api" } end def json_for(type, attributes, id) do json = json_for(type, attributes) put_in json["data"]["id"], id end

Slide 81

Slide 81 text

def json_for(type, attributes) do %{ "data" => %{ "type" => Atom.to_string(type) |> String.replace("_", "-"), "attributes" => attributes }, "format" => "json-api" } end def json_for(type, attributes, id) do json = json_for(type, attributes) put_in json["data"]["id"], id end

Slide 82

Slide 82 text

API Unit Tests test "`create` insert into the database and return payload" do count = MyApp.Repo.all(MyApp.Author) |> length post(conn, authors_path(conn, :create), json_for(:author, @author)) |> json_response(201) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Gizmo"}} }) assert count + 1 == MyApp.Repo.all(MyApp.Author) |> length end https://github.com/danmcclain/voorhees

Slide 83

Slide 83 text

API Unit Tests test "`show` an author" do %{authors: %{one: author}} = fixtures(:authors) get(conn, authors_path(conn, :show, author.id)) |> json_response(200) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Brian"}} }) end

Slide 84

Slide 84 text

API Unit Tests test "`show` an author" do %{authors: %{one: author}} = fixtures(:authors) get(conn, authors_path(conn, :show, author.id)) |> json_response(200) |> assert_payload_contains(%{ "authors": %{attributes: %{first-name: "Brian"}} }) end https://github.com/dockyard/ecto_fixtures

Slide 85

Slide 85 text

authors model: MyApp.Author, repo: MyApp.Repo do valid do first_name "George" last_name "Washington" email "[email protected]" date_of_birth Ecto.Date.from_erl({1990,1,1}) password_hash Comeonin.Bcrypt.hashpwsalt("password") end one inherits: valid do id 1 email "[email protected]" first_name "Leroy" last_name "Jenkins" end end test/fixtures/authors.exs

Slide 86

Slide 86 text

API Unit Tests

Slide 87

Slide 87 text

Integration Tests

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Setting Up Data Per Test if Mix.env == :test do post "/fixtures/start", FixturesController, :start post "/fixtures", FixturesController, :create end

Slide 93

Slide 93 text

Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web, :controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end

Slide 94

Slide 94 text

Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web, :controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end

Slide 95

Slide 95 text

Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web, :controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end

Slide 96

Slide 96 text

Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web, :controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end

Slide 97

Slide 97 text

Setting Up Data Per Test defmodule MyApp.FixturesController do use MyApp.Web, :controller import EctoFixtures, only: [fixtures: 1] def start(conn, _params) do Ecto.Adapters.SQL.begin_test_transaction(MyApp.Repo) send_resp(conn, 200, "") end def create(conn, %{"fixtures" => fixtures}) do Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo) Poison.decode!(fixtures) |> Enum.each(fn(name) -> fixtures(name) end) send_resp(conn, 200, "") end end test/support/fixtures_controller.ex

Slide 98

Slide 98 text

Calling the fixtures in your Ember Test Suite module(“my awesome test module”, { setup() { Ember.$.post(“api/fixtures/start”, { async: false }); } });

Slide 99

Slide 99 text

Calling the fixtures in your Ember Test Suite module(“my awesome test module”, { setup() { Ember.$.post(“api/fixtures/start”, { async: false }); } });

Slide 100

Slide 100 text

Calling the fixtures in your Ember Test Suite test(“my awesome test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); });

Slide 101

Slide 101 text

Calling the fixtures in your Ember Test Suite test(“my awesome test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); });

Slide 102

Slide 102 text

Calling the fixtures in your Ember Test Suite test(“my awesome test”, function(assert) { Ember.$.post(“api/fixtures”, { data: { fixtures: [“foo”, “bar”] }, async: false }); });

Slide 103

Slide 103 text

Other Topics Not Covered * Running with CI * Speeding up test suite * Deployment * Testing Email via Ember (that I will briefly cover right now)

Slide 104

Slide 104 text

Thank you!

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Talk to us about Phoenix and/or Ember development! @dockyard dockyard.com/contact