Skip to content

Instantly share code, notes, and snippets.

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