Skip to content

Instantly share code, notes, and snippets.

View samgaw's full-sized avatar
🇪🇺
¯\_(ツ)_/¯

Sam Gaw samgaw

🇪🇺
¯\_(ツ)_/¯
View GitHub Profile
@romannurik
romannurik / .gitconfig
Last active February 2, 2023 18:38
Cheap Sketch file version control with Git + Kaleidoscope
# Add this to your .git/config file
[diff]
tool = SketchKaleidoscope
[difftool "SketchKaleidoscope"]
cmd = ./util-sketch-kaleidoscope-diff.bash \"$MERGED\" \"$LOCAL\" \"$REMOTE\"
@wosephjeber
wosephjeber / instructions.md
Last active May 7, 2024 13:31
Ecto migration for renaming table with indexes and constraints

Renaming table in Ecto migration

I recently wanted to rename a model and its postgres table in a Phoenix app. Renaming the table was simple and documented, but the table also had constraints, sequences, and indexes that needed to be updated in order for the Ecto model to be able to rely on default naming conventions. I couldn't find any examples of what this would look like but was eventually able to figure it out. For anyone else in the same situation, hopefully this example helps.

In the example below, I'm renaming the Permission model to Membership. This model belongs to a User and an Account, so it has foreign key constraints that need to be renamed.

defmodule MyApp.Repo.Migrations.RenamePermissionsToMemberships do
  use Ecto.Migration
defmodule MyAppWeb.InputHelper do
use Phoenix.HTML
def input(form, field, opts \\ []) do
type = Phoenix.HTML.Form.input_type(form, field)
wrapper_classes = Keyword.get_values(opts, :wrapper_class) ++ ["row", "form-group"]
inputs_classes = Keyword.get_values(opts, :input_class) ++ ["form-control"]
if form.errors[field], do: inputs_classes = inputs_classes ++ ["form-control-danger"]
@mgwidmann
mgwidmann / ecto_polymorphism.ex
Last active September 29, 2020 14:45
Ecto Polymorphism
defmodule Ecto.Polymorphic do
defmacro __using__(_) do
quote do
require Ecto.Schema
import Ecto.Schema, except: [belongs_to: 2, belongs_to: 3]
import unquote(__MODULE__)
end
end

The Complete Guide to Nested Forms in Phoenix

I recently spent some time dealing with nested forms in Phoenix. Nested forms are great when you want to create multiple database records in a single transaction and associate them with each other. I am new to Phoenix and really struggled to find any resources that helped me with my specific problem. I decided to document what I learned in the process in hopes of helping others that are new to Elixir and Phoenix.

Here is my attempt at a one stop shop to learn everything you will need to know about nested forms. If you would like to view the GitHub repo you can check it out here.

Thanks to Heartbeat and Jose for excellent blog posts on nested forms. Also shoutout to Josh for showing me some examples at Ruby

@ahupowerdns
ahupowerdns / lua.md
Last active January 31, 2024 15:01
The LUA record type for Lua-powered DNS records

LUA Record Type for Lua-powered DNS records for standards based fail-over and geographical load balancing

GitHub branch -> https://github.com/ahupowerdns/pdns/tree/luarec

Recently, many zone owners could not migrate away from Dyn since they were benefiting from non-standardised DNS-based failover and geographical loadbalancing features. What you see below is an attempt to get standards based but flexible equivalents of these currently proprietary features. Here is a zone:

$TTL 60
lua.br. IN      SOA     a.lua.br. nstld.verisign-grs.com. (
        2016032300      ; Serial
        14400           ; Refresh
@ipbastola
ipbastola / jq to filter by value.md
Last active April 25, 2024 17:14
JQ to filter JSON by value

JQ to filter JSON by value

Syntax: cat <filename> | jq -c '.[] | select( .<key> | contains("<value>"))'

Example: To get json record having _id equal 611

cat my.json | jq -c '.[] | select( ._id | contains(611))'

Remember: if JSON value has no double quotes (eg. for numeric) to do not supply in filter i.e. in contains(611)

@madsen
madsen / README.md
Last active June 18, 2019 06:33
Patch Asterisk to allow verifying Twilio's secure SIP trunking

Twilio Secure Trunking with Asterisk

Twilio now allows TLS & SRTP connections with their Elastic SIP Trunking. However, there are some issues with the way they've done it that prevent Asterisk from being able to verify their server's certificate.

First of all, their documentation tells you to download the Thawte Primary Root CA, when the certificate I saw was signed with the

@omegahm
omegahm / create_labels.sh
Created April 7, 2015 19:00
Create Gtihub labels from Bash
#!/usr/bin/env bash
# Colours picked from https://robinpowered.com/blog/best-practice-system-for-organizing-and-tagging-github-issues/
###
# Label definitions
###
declare -A LABELS
# Platform
anonymous
anonymous / gist:402ab895a5410e4cd250
Created February 10, 2015 10:37
twist box · http://drbl.in/nJje
int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);