Skip to content

Instantly share code, notes, and snippets.

View paulcsmith's full-sized avatar

Paul Smith paulcsmith

View GitHub Profile
@paulcsmith
paulcsmith / bamboo_assertions.ex
Created January 23, 2016 03:15
Bamboo assert_email_delivered/1 function
# Something like this might make testing simpler
def assert_email_delivered(email) do
email = email |> Bamboo.Mailer.normalize_addresses
assert_received({:delivered_bamboo_email, ^email})
end
@paulcsmith
paulcsmith / layout.html.eex
Last active January 29, 2016 03:59
Render title and meta tags in Phoenix
<head>
<!-- you could put a helper in the Layout View but it's probably not necessary -->
<%= render_existing(@view_module, "seo." <> @view_template, assigns) || render("default_seo.html") %>
</head>
@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
@paulcsmith
paulcsmith / time.ex
Last active February 4, 2016 20:32
Casting/Converting time zones
"timestamp_with_timezone"
|> Timex.DateTime.parse("{ISO}")
|> Timex.DateConvert.to_erlang_datetime
# You probably don't need this next line for most things in Ecto.
# Changesets and queries automatically cast erlang timestamps
|> Ecto.DateTime.from_erl
defmodule MyApp.Changeset do
def cast(model_or_changeset, :empty, required, optional) do
Ecto.Changeset.cast(model_or_changeset, :empty, required, optional)
end
def cast(model_or_changeset, params, required, optional) do
param_keys = params |> Map.keys |> Enum.map(&to_string/1)
allowed_params = (required ++ optional) |> Enum.map(&to_string/1)
disallowed_params = param_keys -- allowed_params
@paulcsmith
paulcsmith / gist:41f22e8210efa028dda2
Created February 13, 2016 18:54
Tracing function calls in Elixir with dbg
:dbg.tracer
:dbg.p :all, :c
:dbg.tpl ModuleToTrace, :x
@paulcsmith
paulcsmith / brunch-config.js
Created April 27, 2016 03:42
Phoenix brunch set up
var bourbonPath = require("bourbon").includePaths[0];
exports.config = {
// See http://brunch.io/#documentation for docs.
files: {
javascripts: {
joinTo: "js/app.js"
// To use a separate vendor.js bundle, specify two files path
// https://github.com/brunch/brunch/blob/stable/docs/config.md#files
@paulcsmith
paulcsmith / mime type of file
Created June 22, 2016 16:54
How to get mime type for a file
$ file -I Makefile
Makefile: text/x-makefile; charset=us-ascii
@paulcsmith
paulcsmith / test.exs
Last active July 13, 2016 16:39
ExMachina 2.0?
defmodule MyApp.Factory do
def user_factory do
%User{
email: "foo@gmail.com",
name: "Paul"
}
end
end
# Makes it easy to organize factories however you want