{"_id":"5583388081672a3900bb5109","__v":0,"user":{"_id":"5435b410495d5d0800f3a603","username":"","name":"Lance Halvorsen"},"initVersion":{"_id":"5558c642eb56ae2f00f714fc","version":"0.13.1"},"tags":[],"project":"54348ec95b10711400c6c445","createdAt":"2015-06-18T21:30:40.491Z","changelog":[],"body":"https://gist.github.com/chrismccord/cf51346c6636b5052885\n\n### `form_tag`, `link`, CSRF changes\n\nPlug 0.10.0 moves CSRF tokens from cookies back to sessions. To avoid future bumps on the road, a `get_csrf_token/0` function has been added to controllers and imported into views. Update all your csrf token reference code to use the new function. Additionally, `form_tag` and `link` helpers have been added that will inject the csrf token for you automatically. You should transition to these new functions where possible, ie:\n\n```erb\n  <%= form_tag(\"/hello\", method: :post) %>\n    ... your form stuff. csrf is inject for you\n  </form>\n```\n\nAdditionally, a `link` helper has been added that can send POST/PUT/DELETE requests with csrf inclusion, ie:\n\n```erb\n<%= link \"Remove\", to: user_path(@conn, :delete, @user), method: :delete) %>\n```\n\n### view.ex, using\n`web/view.ex` has been removed, as well as the `using` macro that allowed you to inject shared aliases/imports across other views. It has been replace by a `web/web.ex` file that serves as a centralized place to group model, view, and controller shared code injection blocks.\n\n1) Save the following block of code as `web/web.ex` (replace `MyApp` with your application module), and copy your `web/view.ex` contents as indicated in the block below. After the new file is saved, delete your `web/view.ex` file.\n\n```elixir\ndefmodule MyApp.Web do\n  @moduledoc \"\"\"\n  A module that keeps using definitions for controllers,\n  views and so on.\n\n  This can be used in your application as:\n\n      use MyApp.Web, :controller\n      use MyApp.Web, :view\n\n  Keep the definitions in this module short and clean,\n  mostly focused on imports, uses and aliases.\n  \"\"\"\n\n  def view do\n    quote do\n      use Phoenix.View, root: \"web/templates\"\n\n      # Import URL helpers from the router\n      import MyApp.Router.Helpers\n\n      # Import all HTML functions (forms, tags, etc)\n      use Phoenix.HTML\n      \n      # *****\n      # Copy your old `web/view.ex` using block contents here\n      # *****\n    end\n  end\n\n  def controller do\n    quote do\n      use Phoenix.Controller\n\n      # Import URL helpers from the router\n      import MyApp.Router.Helpers\n    end\n  end\n  \n  def model do\n    quote do\n    end\n  end\n\n  @doc \"\"\"\n  When used, dispatch to the appropriate controller/view/etc.\n  \"\"\"\n  defmacro __using__(which) when is_atom(which) do\n    apply(__MODULE__, which, [])\n  end\nend\n```\n\n2) Update all your views to use the new `Web` module, ie:\n\n```elixir\n# 0.9.x\ndefmodule MyApp.PageView do\n  use MyApp.View\nend\n\n# 0.10.0\ndefmodule MyApp.PageView do\n  use MyApp.Web, :view\nend\n```\n\n3) Update all your controller use declrations, ie:\n\n```elixir\n# 0.9.x\ndefmodule MyApp.UserController do\n  use Phoenix.Controller\nend\n\n# 0.10.0\ndefmodule MyApp.UserController do\n  use MyApp.Web, :controller\nend\n```\n\n\n### Endpoint PubSub Config\n\n- The `:options` key has been removed from the `:pubsub` keyword list. Just define the options alongside the pubsub configuration.\n- The :name option when configuring a pubsub adapter is now required instead of inflected\n\n```elixir\n\n# 0.9.x\nconfig :my_app, MyApp.Endpoint,\n  pubsub: [adapter: Phoenix.PubSub.PG2]\n           \n# 0.10.0\nconfig :my_app, MyApp.Endpoint,\n  pubsub: [name: MyApp.PubSub,\n           adapter: Phoenix.PubSub.PG2]\n\n```\n\n### Redis PubSub Adapter\nThe `:eredis` based adapter has been replaced with `:redo` for propper remote connections. Update your configuration and mix deps like so:\n\n```elixir\n# config.exs\n# 0.9.x\nconfig :my_app, MyApp.Endpoint,\n  pubsub: [adapter: Phoenix.PubSub.Redis]\n           \n# 0.10.0\nconfig :my_app, MyApp.Endpoint,\n  pubsub: [name: MyApp.PubSub,\n           adapter: Phoenix.PubSub.Redis,\n           host: \"127.0.0.1\",\n           ...]\n\n# mix.exs\n# 0.9.x\n\n      defp deps do\n        [{:eredis, github: ...\n         {:poolboy, \"~> 1.4.2\"},\n        ...]\n      end\n\n# 0.10.0\n\n      defp deps do\n        [{:redo, github: \"heroku/redo\"},\n         {:poolboy, \"~> 1.4.2\"},\n        ...]\n      end\n```\n\n### Setting up the new Brunch asset system (optional)\n\nPhoenix 0.10.0 includes [brunch](http://brunch.io) for static asset compilation. Brunch is a nodejs project, so node is required. See [node's website](https://nodejs.org/) for installation instructions. Use the following steps to get your existing application setup:\n\n1. Copy `package.json` to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/package.json\n2. Copy `brunch-config.js` to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/brunch-config.js\n3. Ensure node is installed\n4. `project_root$ npm install`\n6. `project_root$ mkdir -p web/static/js`\n7. `project_root$ mkdir -p web/static/css`\n8. `project_root$ touch web/static/js/app.js`\n9. `project_root$ touch web/static/css/app.scss`\n10. copy `phoenix.js` to `web/static/vendor/phoenix.js` https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/phoenix.js\n10. Move/copy your application javascript and app css/sass to the former files\n12. Update your `web/templates/layouts/application.html.eex` (and others if exist) to use the new brunch built css/js, ie: `<script src=\"<%= static_path(@conn, \"/js/app.js\") %>\"></script>` `<link rel=\"stylesheet\" href=\"<%= static_path(@conn, \"/css/app.css\") %>\">`\n13. Add this to load your main js module in your layout(s) just before `</body>`: `<script>require(\"web/static/js/app\")</script>`\n14. Add the following Endpoint `:watchers` and `:live_reload` configuration to `config/dev.exs`:\n\n```elixir\nconfig :my_app, MyAPp.Endpoint,\n  ...\n  watchers: [{Path.expand(\"node_modules/brunch/bin/brunch\"), [\"watch\"]}]\n\n# Watch static and templates for browser reloading.\n# *Note*: Be careful with wildcards. Larger projects\n# will use higher CPU in dev as the number of files\n# grow. Adjust as necessary.\nconfig :my_app, MyApp.Endpoint,\n  live_reload: [Path.expand(\"priv/static/js/app.js\"),\n                Path.expand(\"priv/static/css/app.css\"),\n                Path.expand(\"web/templates/**/*.eex\")]\n\n\n```\n\nNow start your server with `mix phoenix.server` and observe brunch compiling your assets in `web/static/css` and `web/static/js` and live-reloading the browser on changes.\n\n### `phoenix_ecto`\n\nIf you are using Ecto, add `phoenix_ecto` to your mix deps to enjoy Ecto form/link builder support:\n\n```elixir\n  defp deps do\n    [...,\n     {:phoenix, \"~> 0.10.0\"},\n     {:phoenix_ecto, \"~> 0.1\"},\n     {:postgrex, \">= 0.0.0\"}]\n  end\n\n```\n\nHappy coding!","slug":"upgrading-from-v090-to-v0100","title":"Upgrading from v0.9.0 to v0.10.0"}

Upgrading from v0.9.0 to v0.10.0


https://gist.github.com/chrismccord/cf51346c6636b5052885 ### `form_tag`, `link`, CSRF changes Plug 0.10.0 moves CSRF tokens from cookies back to sessions. To avoid future bumps on the road, a `get_csrf_token/0` function has been added to controllers and imported into views. Update all your csrf token reference code to use the new function. Additionally, `form_tag` and `link` helpers have been added that will inject the csrf token for you automatically. You should transition to these new functions where possible, ie: ```erb <%= form_tag("/hello", method: :post) %> ... your form stuff. csrf is inject for you </form> ``` Additionally, a `link` helper has been added that can send POST/PUT/DELETE requests with csrf inclusion, ie: ```erb <%= link "Remove", to: user_path(@conn, :delete, @user), method: :delete) %> ``` ### view.ex, using `web/view.ex` has been removed, as well as the `using` macro that allowed you to inject shared aliases/imports across other views. It has been replace by a `web/web.ex` file that serves as a centralized place to group model, view, and controller shared code injection blocks. 1) Save the following block of code as `web/web.ex` (replace `MyApp` with your application module), and copy your `web/view.ex` contents as indicated in the block below. After the new file is saved, delete your `web/view.ex` file. ```elixir defmodule MyApp.Web do @moduledoc """ A module that keeps using definitions for controllers, views and so on. This can be used in your application as: use MyApp.Web, :controller use MyApp.Web, :view Keep the definitions in this module short and clean, mostly focused on imports, uses and aliases. """ def view do quote do use Phoenix.View, root: "web/templates" # Import URL helpers from the router import MyApp.Router.Helpers # Import all HTML functions (forms, tags, etc) use Phoenix.HTML # ***** # Copy your old `web/view.ex` using block contents here # ***** end end def controller do quote do use Phoenix.Controller # Import URL helpers from the router import MyApp.Router.Helpers end end def model do quote do end end @doc """ When used, dispatch to the appropriate controller/view/etc. """ defmacro __using__(which) when is_atom(which) do apply(__MODULE__, which, []) end end ``` 2) Update all your views to use the new `Web` module, ie: ```elixir # 0.9.x defmodule MyApp.PageView do use MyApp.View end # 0.10.0 defmodule MyApp.PageView do use MyApp.Web, :view end ``` 3) Update all your controller use declrations, ie: ```elixir # 0.9.x defmodule MyApp.UserController do use Phoenix.Controller end # 0.10.0 defmodule MyApp.UserController do use MyApp.Web, :controller end ``` ### Endpoint PubSub Config - The `:options` key has been removed from the `:pubsub` keyword list. Just define the options alongside the pubsub configuration. - The :name option when configuring a pubsub adapter is now required instead of inflected ```elixir # 0.9.x config :my_app, MyApp.Endpoint, pubsub: [adapter: Phoenix.PubSub.PG2] # 0.10.0 config :my_app, MyApp.Endpoint, pubsub: [name: MyApp.PubSub, adapter: Phoenix.PubSub.PG2] ``` ### Redis PubSub Adapter The `:eredis` based adapter has been replaced with `:redo` for propper remote connections. Update your configuration and mix deps like so: ```elixir # config.exs # 0.9.x config :my_app, MyApp.Endpoint, pubsub: [adapter: Phoenix.PubSub.Redis] # 0.10.0 config :my_app, MyApp.Endpoint, pubsub: [name: MyApp.PubSub, adapter: Phoenix.PubSub.Redis, host: "127.0.0.1", ...] # mix.exs # 0.9.x defp deps do [{:eredis, github: ... {:poolboy, "~> 1.4.2"}, ...] end # 0.10.0 defp deps do [{:redo, github: "heroku/redo"}, {:poolboy, "~> 1.4.2"}, ...] end ``` ### Setting up the new Brunch asset system (optional) Phoenix 0.10.0 includes [brunch](http://brunch.io) for static asset compilation. Brunch is a nodejs project, so node is required. See [node's website](https://nodejs.org/) for installation instructions. Use the following steps to get your existing application setup: 1. Copy `package.json` to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/package.json 2. Copy `brunch-config.js` to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/brunch-config.js 3. Ensure node is installed 4. `project_root$ npm install` 6. `project_root$ mkdir -p web/static/js` 7. `project_root$ mkdir -p web/static/css` 8. `project_root$ touch web/static/js/app.js` 9. `project_root$ touch web/static/css/app.scss` 10. copy `phoenix.js` to `web/static/vendor/phoenix.js` https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/phoenix.js 10. Move/copy your application javascript and app css/sass to the former files 12. Update your `web/templates/layouts/application.html.eex` (and others if exist) to use the new brunch built css/js, ie: `<script src="<%= static_path(@conn, "/js/app.js") %>"></script>` `<link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">` 13. Add this to load your main js module in your layout(s) just before `</body>`: `<script>require("web/static/js/app")</script>` 14. Add the following Endpoint `:watchers` and `:live_reload` configuration to `config/dev.exs`: ```elixir config :my_app, MyAPp.Endpoint, ... watchers: [{Path.expand("node_modules/brunch/bin/brunch"), ["watch"]}] # Watch static and templates for browser reloading. # *Note*: Be careful with wildcards. Larger projects # will use higher CPU in dev as the number of files # grow. Adjust as necessary. config :my_app, MyApp.Endpoint, live_reload: [Path.expand("priv/static/js/app.js"), Path.expand("priv/static/css/app.css"), Path.expand("web/templates/**/*.eex")] ``` Now start your server with `mix phoenix.server` and observe brunch compiling your assets in `web/static/css` and `web/static/js` and live-reloading the browser on changes. ### `phoenix_ecto` If you are using Ecto, add `phoenix_ecto` to your mix deps to enjoy Ecto form/link builder support: ```elixir defp deps do [..., {:phoenix, "~> 0.10.0"}, {:phoenix_ecto, "~> 0.1"}, {:postgrex, ">= 0.0.0"}] end ``` Happy coding!