Skip to content

Instantly share code, notes, and snippets.

View .iex.exs
# .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
Elixir/Sidekiq Pseudo Example
View gist:42b85fc01164dc6ac94d
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 Aug 29, 2015
JSON only Phoenix view
View gist:86b02961af24f227bf33
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
View gist:98cbdad7c8ace12dc135
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
View gist:9abe57badf3e4c0be724
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:e774e6ab5220e6505a03
Last active Aug 29, 2015
Upgrading your Phoenix 0.5.x application to 0.6.0
View gist:e774e6ab5220e6505a03

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:

View client.js
var socket = new Phoenix.Socket(PewMiddle.config.socketPath);
PewMiddle.requestGame = function(username, callback) {
socket.join(PewMiddle.config.channel, "lobby", {username: username}, function(channel) {
var player = null;
channel.on("player:created", function(message) {
player = message;
});
channel.on("game:created", function(game) {
callback(game.id, player);
@chrismccord
chrismccord / gist:9434b8fa208b3aae22b6
Last active Aug 29, 2015
Phoenix Upgrade Instructions 0.7.x to 0.8.0
View gist:9434b8fa208b3aae22b6

Channel Changes

The channel layer received significant features and an overhaul of the topic abstraction. Upgrade your 0.7.x channels should only require a few simple steps.

Notable changes:

  • An updated version of phoenix.js is required, replace your priv/static/js/phoenix.js with https://github.com/phoenixframework/phoenix/blob/v0.8.0/priv/static/js/phoenix.js
  • "topic" is now just an identifier. You join topics, broadcast on topics, etc. Channels are are dispatched to based on topic patterns in the router.
  • Channel callbacks in 0.8.0 introduce the concept of outgoing events. Prior to 0.8.0, chanenls only processed incoming events via the event/3 callbacks. In 0.8.0, event/3 has been renamed to handle_in/3, and outgoing events callbacks can be defined via handle_out/3
  • All channel callbacks, such as join/3, leave/2, handle_in/3, and handle_out/3 now accept the socket as the last argument. This mimicks GenServer APIs
  • The return signature of handle_in, `handle_
@chrismccord
chrismccord / gist:def6f4dc444b6a8f8d8b
Last active Aug 29, 2015
Phoenix Upgrade Instructions 0.8.x to 0.9.0
View gist:def6f4dc444b6a8f8d8b

Endpoint

Plug.Static added an :only option which greatly improves performance by skipping disk reads at runtime. Replace your Plug.Static lines in endpoint.ex with the following configuration:

  plug Plug.Static,
    at: "/", from: :my_app,
    only: ~w(css images js favicon.ico robots.txt)

Router / Controller

@chrismccord
chrismccord / gist:4a62780056b08c60542d
Last active Aug 29, 2015
Phoenix Upgrade Instructions 0.13.0 to 0.13.1
View gist:4a62780056b08c60542d