Skip to content

Instantly share code, notes, and snippets.

@chrismccord
chrismccord / gist:8640170
Created January 26, 2014 22:15
Plug use
defmodule Router do
use Phoenix.Router, port: 4000
use Plug.Static, mount: "/public"
use Plug.EncryptedSession, secret: "203802482048204820"
get "pages/:page", PagesController, :show, as: :page
get "files/*path", FilesController, :show
get "profiles/user-:id", UsersController, :show
resources "users", UsersController
@chrismccord
chrismccord / .iex.exs
Created February 23, 2014 20:23
IExHelpers
# .iex.exs
defmodule IExHelpers do
def reload! do
Mix.Task.reenable "compile.elixir"
Mix.Task.run "compile.elixir"
end
end
iex = IExHelpers
@chrismccord
chrismccord / gist:42b85fc01164dc6ac94d
Created May 29, 2014 02:56
Elixir/Sidekiq Pseudo Example
class StatsController
  def update
    # call sidekiq worker as normal
    TwitterStatsWorker.perform_async(twitter_user.pin, twitter_user.token)
  end
end

class TwitterStatsWorker
 include Sidekiq::Worker
@chrismccord
chrismccord / gist:86b02961af24f227bf33
Last active August 29, 2015 14:05
JSON only Phoenix view
defmodule MyApp.UserController do
use Phoenix.Controller
def show(conn, %{"id" => id}) do
render conn, "show", user: Repo.get!(user, id)
end
def index(conn, _) do
render conn, "index", users: Repo.all(User)
end
setw -g mode-keys vi
set-option -g default-command "reattach-to-user-namespace -l zsh"
bind y run-shell "reattach-to-user-namespace -l zsh -c 'tmux show-buffer | pbcopy'"
unbind C-b
set -g prefix C-a
bind-key a send-prefix
# Add a binding to toggle status line
bind v set status
@chrismccord
chrismccord / gist:9abe57badf3e4c0be724
Created August 28, 2014 19:49
Elixir reduce while
defmodule Reduce do
def while(collection, initial, while_func, reduce_func) do
try do
Enum.reduce collection, initial, fn element, acc ->
if while_func.(element, acc) do
reduce_func.(element, acc)
else
throw {:halt, acc}
end
@chrismccord
chrismccord / gist:be5c3cf8f89b252122ba
Last active January 2, 2018 01:26
Phoenix Gulpfile.coffee (Sass/CoffeeScript)
# Gulpfile.js
# // Note the new way of requesting CoffeeScript since 1.7.x
# require('coffee-script/register');
# // This bootstraps your Gulp's main file
# require('./Gulpfile.coffee');
# assets
# ├── Gulpfile.coffee
# ├── Gulpfile.js
@chrismccord
chrismccord / gist:e774e6ab5220e6505a03
Last active August 29, 2015 14:10
Upgrading your Phoenix 0.5.x application to 0.6.0

Upgrading Phoenix 0.5.x to 0.6.0

If you're still on 0.4.x, follow [this 0.4x to 0.5.0 upgrade guide] (http://learnelixir.com/blog/2014/10/29/migrating-applications-from-phoenix-0-dot-4-1-to-phoenix-0-dot-5-0/), then head back over here.

Phoenix 0.6.0 brings some nice enhancements and a few backwards incompatible changes. The following steps should get your 0.5.x apps up and running on the latest and greatest.

Router

The Router no longer defines the default :browser and :api pipelines for you, but they remain the idiomatic defaults. You can copy the standard pipelines into your router:

@chrismccord
chrismccord / gist:c24b2b516066d987f4fe
Last active April 24, 2016 23:17
Phoenix 0.6.x to 0.7.0 Upgrade Instructions

How to upgrade to Phoenix from 0.6.x to 0.7.0.

  1. 0.7.0 depends on Plug HEAD, so you'll need to include plug in your mix deps until the next plug release:
{:plug, github: "elixir-lang/plug", ref: "7040c89cb4cf1f1c6afdee379e5982a07d77a6c3"}
```

  1. The `Pheonix.Controller` functions html/2, json/2, text/2, redirect/2 and render/3 no longer halt automatically. *Important*: You must now explicity call `halt/1` yourself if you want to stop invoking further plugs after calling these functions. This is improtant for auth filtering plugs, ie:

```elixir
@chrismccord
chrismccord / gist:31340f08d62de1457454
Last active August 8, 2021 17:10
Phoenix Content Negotiation
# Controller option 1, implicit render selected based on format
defmodule MyApp.UserController do
use Phoenix.Controller
def show(conn, %{"id" => id}) do
render conn, :show, user: Repo.get(User, id)
end
end
# Controller option 2, explicit render with format pattern match