Skip to content

Instantly share code, notes, and snippets.

View paulcsmith's full-sized avatar

Paul Smith paulcsmith

View GitHub Profile
@adamloving
adamloving / watcher.js
Created October 13, 2011 20:43
Node.js CoffeeScript, Jade, and Stylus compiler (watches filesystem for changes)
var OUTPUT_JS_FILENAME = '../media/partner/js/widgets.js'
var OUTPUT_CSS_FILENAME = '../media/partner/css/widgets.css'
var OUTPUT_HTML_PATH = 'templates'
var fs = require('fs')
var jade = require('jade')
var stylus = require('stylus');
var cs = require('coffee-script');
var watch = require('watch');
@chrismccord
chrismccord / catch_all_action.ex
Created September 18, 2015 00:06
catch-all action
defmodule MyApp.Web do
def controller do
quote do
use Phoenix.Controller
use MyApp.CatchAllController
...
end
end
end
defmodule MyApp.AnnouncementTraits do
def with_subscriber(announcement, user) do
create(:subscription, announcement: announcement, user: user)
announcement
end
def tag_with_interest(announcement, interest) do
create(:announcement_interest, announcement: announcement, interest: interest)
announcement
end
@chrismccord
chrismccord / gist:cf51346c6636b5052885
Last active January 17, 2016 12:11
Phoenix 0.9 to 0.10.0 upgrade instructions

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:

  <%= form_tag("/hello", method: :post) %>
    ... your form stuff. csrf is inject for you
  </form>
@paulcsmith
paulcsmith / changed.ex
Last active February 2, 2016 17:33
Idea for check whether something changed from one value to another in a changeset
changeset |> changed?(:status, from: "pending", to: "canceled")
changeset |> changed?(:status, from: "attending") # for checking if it changed, but we don't care what it changed to
def changed?(changeset, field, from: from, to: to) do
Ecto.Changeset.get_field(changeset, field) == from
&& booking_changeset.changes[field] == to
end
@paulcsmith
paulcsmith / Filtering params.ex
Last active February 4, 2016 16:55
Building queries using plain functions
def index(conn, params) do
Post
|> filter_upcoming(params)
|> filter_attributes(params, whitelist: ~w(title body)
|> filter_timespan(params)
# Or simplify it if it's used in multiple controllers
Post |> filter_post_params(params)
end
@sorentwo
sorentwo / email.ex
Last active February 11, 2016 20:16
Normalize and Validate
def normalize(email) do
%{email | from: normalize(email.from, :from),
to: normalize(List.wrap(email.to), :to),
cc: normalize(List.wrap(email.cc), :cc),
bcc: normalize(List.wrap(email.bcc), :bcc)}
end
defp normalize(record, type) do
Formatter.format_email_address(record, %{type: type})
end
@seaneshbaugh
seaneshbaugh / user.ex
Last active February 23, 2016 10:02
user.ex
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :username, :string
field :email, :string
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
field :encrypted_password, :string
@cblavier
cblavier / time_helper.ex
Created March 5, 2015 17:55
TimeHelper
defmodule TimeHelper do
def wait_until(fun), do: wait_until(500, fun)
def wait_until(0, fun), do: fun.()
def wait_until(timeout, fun) defo
try do
fun.()
rescue
@chrismcg
chrismcg / example_form_model.ex
Created August 2, 2015 11:34
Example Phoenix / Ecto Form Model
defmodule Phlink.Form do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "non_db_form.user" do
field :name, :string
field :thing, :string
end
@required_fields ~w(name)