Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound = Class.new(ActiveRecord::ActiveRecordError)
# Like +first!+, but will also fail if the query would match more than
# one record.
PS1='\[\e[1m\]\[\e[34m\]\u@\h$([ "$DOCKER_MACHINE_NAME" = "" ] || echo " \[\e[33m\][docker-machine:$DOCKER_MACHINE_NAME]\[\e[34m\]"): \[\e[35m\]\w\[\e[33m\]$(__git_ps1) \[\e[34m\]$\[\e[0m\] '
docker-machine-auth () {
MACHINE_NAME=$1
if [ "$MACHINE_NAME" = "" ]
then
eval "$(docker-machine env -u)"
else
eval "$(docker-machine env $MACHINE_NAME)"
fi
}
@troelskn
troelskn / db.rake
Last active January 28, 2019 12:40
Wait for db. Useful in a dockerized setup
namespace :db do
desc "Wait for db. Used for initial boot up"
task wait: :environment do
puts "*** Giving DB time to boot"
catch :done do
10.times do
Timeout.timeout(1) do
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SELECT NOW()")
@troelskn
troelskn / raven.rb
Last active November 21, 2018 09:06
config/initializers/raven.rb
require 'raven'
require 'rake/task'
if Rails.env.production?
Raven.configure do |config|
config.dsn = 'http://xxx:xxx@app.getsentry.com/xxxxx'
end
module Rake
class Task
@troelskn
troelskn / shell_execute.rb
Created October 24, 2018 14:11
Running a shell subprocess is surprisingly hard in ruby
require "open3"
def execute_shell(command, verbose: false)
Rails.logger.info "[SHELL] #{command}"
# see: https://nickcharlton.net/posts/ruby-subprocesses-with-stdout-stderr-streams.html
# see: http://stackoverflow.com/a/1162850/83386
output = []
Open3.popen3(command) do |stdin, stdout, stderr, thread|
# read each stream from a new thread
@troelskn
troelskn / gist:1294011
Created October 17, 2011 22:15
iban checksum validation in php
<?php
function is_valid_iban($str) {
static $charmap = array (
'A' => 10, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35,
);
if (!preg_match("/\A[A-Z]{2}\d{2} ?[A-Z\d]{4}( ?\d{4}){1,} ?\d{1,4}\z/", $str)) {
return false;
}
$iban = str_replace(' ', '', $str);
$iban = substr($iban, 4) . substr($iban, 0, 4);
import sys
import os
import subprocess
def git(args, **kwargs):
environ = os.environ.copy()
if 'repo' in kwargs:
environ['GIT_DIR'] = kwargs['repo']
if 'work' in kwargs:
environ['GIT_WORK_TREE'] = kwargs['work']
@troelskn
troelskn / install_sphinxsearch_2.1.4.sh
Created March 27, 2014 11:57
Installing sphinxsearch 2.1.4 on Ubuntu 13.10
# Install dependencies manually
apt-get install odbcinst1debian2 libodbc1 unixodbc libpq5
# Fetch deb package
mkdir -p /usr/local/build ; cd /usr/local/build && [-f sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb ] || wget http://sphinxsearch.com/files/sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb
# Install package
dpkg --install /usr/local/build/sphinxsearch_2.1.4-release-0ubuntu11~precise_amd64.deb
@troelskn
troelskn / link_user.html.erb
Last active November 13, 2017 11:06
Extend http://railscasts.com/episodes/235-devise-and-omniauth-revised to support scenario where a user first creates account without Facebook login, then later authenticates with Facebook.
@troelskn
troelskn / sprockets.rb
Created November 1, 2017 14:00
Use sprockets-es6 to handle jsx
# config/initializers/sprockets.rb
module Sprockets
if respond_to?(:register_transformer)
register_mime_type 'text/jsx', extensions: ['.jsx'], charset: :unicode
register_transformer 'text/jsx', 'application/javascript', ::BabelTransformer
register_preprocessor 'text/jsx', DirectiveProcessor
end
if respond_to?(:register_engine)
args = ['.jsx', ::BabelTransformer]