Skip to content

Instantly share code, notes, and snippets.

@pch
pch / trimws.sh
Last active May 17, 2016 09:31
Nukes all trailing whitespace in the current project (rails specific).
find . -not \( -name .git -prune -o -name images -prune -o -name GeoLiteCity.dat -prune \) -type f -print0 | xargs -0 perl -pi -e 's/ +$//'
@pch
pch / git-sync
Last active August 30, 2021 08:57
Git sync - light version
#!/bin/sh
git_branch() {
echo $(git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
}
git_dirty() {
st=$(git status 2>/dev/null | tail -n 1)
if [[ $st == "" ]]
then
@pch
pch / assert_queries.rb
Last active October 29, 2022 21:13
assert_queries - rails 4
# Assertion for checking the number of queries executed within the &block
def assert_queries(num = 1, &block)
queries = []
callback = lambda { |name, start, finish, id, payload|
queries << payload[:sql] if payload[:sql] =~ /^SELECT|UPDATE|INSERT/
}
ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
ensure
assert_equal num, queries.size, "#{queries.size} instead of #{num} queries were executed.#{queries.size == 0 ? '' : "\nQueries:\n#{queries.join("\n")}"}"
@pch
pch / discipline
Created February 13, 2014 11:49
Tricking myself into writing.
#!/usr/bin/env ruby
#
# I use the following script to trick myself into writing every day.
#
# All it does is create a directory for the current month (YYYY/MM),
# create an empty markdown file for today and annoy the hell out of
# me until I put something in that file.
#
# I run the script with GeekTool every 30 minutes:
# http://projects.tynsoe.org/en/geektool/
@pch
pch / Gemfile
Last active August 29, 2015 14:04
# rspec
group :test do
gem 'rspec-rails'
gem 'launchy'
gem 'capybara'
gem 'webrat'
gem 'database_cleaner'
gem 'email_spec'
gem 'factory_girl_rails'
end
@pch
pch / mac-vagrant-unison.md
Last active February 18, 2019 08:57
How to install Unison on Mac OS El Capitan & Vagrant

How to install Unison on Mac OS El Capitan & Vagrant - for Docker Sync

You can install Unison on the Mac via homebrew (brew install unison), but it's very likely that it won't work properly, resulting in errors like:

Unison failed: Uncaught exception Failure("input_value: bad bigarray kind")
Fatal error: Lost connection with the server

Keybase proof

I hereby claim:

  • I am pch on github.
  • I am pchm (https://keybase.io/pchm) on keybase.
  • I have a public key ASDohLNPHrR-IY6_I7lzw1SiENKSNNIKkmT9-BQIpRQoxQo

To claim this, I am signing this object:

@pch
pch / finder-macvim-file-drawer.md
Created April 23, 2020 08:47
How to Use macOS Finder as a MacVim File Drawer

How to Use macOS Finder as a MacVim File Drawer

When I switched to Vim from TextMate 2, the feature I missed the most was a decent file drawer (no, NERDTree doesn't cut it). I'm happy with [fzf.vim][fzf], but having the project tree in front of you can be really useful from time to time, e.g. when working with unfamiliar codebase, a new project etc.

The solution for it is surprisingly simple (although not without flaws): use MacVim with a Finder window in Split View mode:

@pch
pch / encrypted-tokens.md
Created June 1, 2020 10:17
Encrypted Tokens Between Rails and Node

Encrypted Tokens Between Rails and Node

If you need to pass data safely between Rails and Node, you can use the built-in Rails encryption.

The script below wraps ActiveSupport::MessageEncryptor, allowing to create expiring JSON tokens that can be decoded in Node using a shared secret.

secret = "866b914a169d3969849966febafe8057bec6b82ea477e64682a11a2e61096797"
payload = { user_id: 1, name: "John Doe", role: "admin" }
EncryptedToken.encode(payload: payload, expires_at: 10.minutes.from_now, secret: secret)
#!/usr/bin/env ruby
#
# Recreate Lightroom catalog by replacing corrupt files with recovered copies
#
# Keeps the directory structure and creates the new catalog in a new path,
# which means you'll need at least 3x the size of your photos of free space.
#
# Dependencies:
# - ruby
# - exiftool