Skip to content

Instantly share code, notes, and snippets.

@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: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:

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 August 29, 2015 14:13
Phoenix Upgrade Instructions 0.7.x to 0.8.0

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 August 29, 2015 14:14
Phoenix Upgrade Instructions 0.8.x to 0.9.0

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 August 29, 2015 14:21
Phoenix Upgrade Instructions 0.13.0 to 0.13.1