Skip to content

Instantly share code, notes, and snippets.

View paulcsmith's full-sized avatar

Paul Smith paulcsmith

View GitHub Profile
class Home::IndexPage < MainLayout
def content
Kilt.embed "my_page.slang", io_name: @view
end
end
@paulcsmith
paulcsmith / an_action_that_requires_sign_in.cr
Last active March 8, 2018 21:22
For sign up and sign in (authentication)
class Somethings::Index < BrowserAction
before require_sign_in
action { "do stuff" }
# You can extract all this to a module since you'll likely use it in multiple places
# Probably in src/pipes/authentication.cr or something
private def require_sign_in
if signed_in?
@paulcsmith
paulcsmith / hello world benchmark
Last active December 4, 2017 13:28
Lucky hello world benchmark 0.6.1
% wrk -t4 -c100 -d30S --timeout 2000 http://localhost:8080
Running 30s test @ http://localhost:8080
4 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.89ms 481.91us 14.54ms 93.89%
Req/Sec 13.41k 1.90k 14.70k 90.08%
1601002 requests in 30.00s, 299.26MB read
Requests/sec: 53358.67
Transfer/sec: 9.97MB
@paulcsmith
paulcsmith / app_conn.cr
Last active November 21, 2017 21:16
Testing Lucky apps
# spec/support/app_conn.cr
require "http/client"
class AppConn
getter! response
@response : HTTP::Client::Response? = nil
module Matchers
def contain_html(html : String)
ContainHtmlExpectation.new(html)
@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
@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 / 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 / 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
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 / 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