Skip to content

Instantly share code, notes, and snippets.

@pch
pch / typeerror.md
Created February 2, 2024 07:51
Rails, Turbo, esbuild: TypeError: map.get is not a function

(This applies to Rails 7.1, Turbo 8.0 with esbuild and propshaft)

If you're getting a "TypeError: map.get is not a function" when navigating between pages:

Uncaught (in promise) TypeError: map.get is not a function
    at fetch (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:6485:20)
    at fetchWithTurboHeaders (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1153:10)
    at FetchRequest.perform (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1263:25)
fetch @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:6485
@pch
pch / settings.json
Created January 27, 2024 13:59
vscode settings for eslint, prettier, ruby-lsp
{
"editor.overviewRulerBorder": false,
"[typescript, javascript]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "always"
},
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[ruby]": {
@pch
pch / base32.rb
Last active December 18, 2023 08:21
Base32 encoding in Ruby (RFC 4648)
# Base32 (RFC 4648) implemented in Ruby
#
# Source: https://ptrchm.com/posts/base32-explained/
class Base32
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".split("")
PADDING_CHAR = "="
BITS_PER_BYTE = 8 # 1 byte = 8 bits
BITS_PER_CHAR = Math.log2(ALPHABET.length).round # 5 = 32 chars = 2^5 number of bits encoded into a single character in the ALPHABET
BITS_PER_CHUNK = BITS_PER_CHAR.lcm(BITS_PER_BYTE) # 40 (least common mutliple of 5 and 8)
function initComponent(root, goDeep = true) {
const component = root.dataset.component;
const targets = {};
const state = {};
root.querySelectorAll(`[data-${component}-target]`).forEach((attr) => {
targets[attr.getAttribute(`data-${component}-target`)] = attr;
});
Array.from(root.attributes).forEach((attr) => {
#!/bin/bash
#
# Example pre-commit hook: triggers rubocop & eslint on staged files.
#
# To use it, create a symlink in your .git/hooks directory:
#
# $ ln -s $(pwd)/bin/pre-commit .git/hooks
set -e
# Ruby
#!/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
@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)
@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:

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 / 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