Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Smith paulcsmith

View GitHub Profile
View page_with_slang.cr
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 Mar 8, 2018
For sign up and sign in (authentication)
View an_action_that_requires_sign_in.cr
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 Dec 4, 2017
Lucky hello world benchmark 0.6.1
View hello world benchmark
% 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 Nov 21, 2017
Testing Lucky apps
View app_conn.cr
# 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 Jul 13, 2016
ExMachina 2.0?
View test.exs
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 Jun 22, 2016
How to get mime type for a file
View mime type of file
$ file -I Makefile
Makefile: text/x-makefile; charset=us-ascii
@paulcsmith
paulcsmith / brunch-config.js
Created Apr 27, 2016
Phoenix brunch set up
View brunch-config.js
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 Feb 13, 2016
Tracing function calls in Elixir with dbg
View gist:41f22e8210efa028dda2
:dbg.tracer
:dbg.p :all, :c
:dbg.tpl ModuleToTrace, :x
View changeset.ex
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 Feb 4, 2016
Casting/Converting time zones
View time.ex
"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