Skip to content

Instantly share code, notes, and snippets.

View inopinatus's full-sized avatar
fuzzy logic

Josh Goodall inopinatus

fuzzy logic
View GitHub Profile
inopinatus / castaway.rb
Created April 23, 2021 02:17
dangerous polymorphic fun with primary key casts (nsfw)
class CastIdsToText < ActiveRecord::Migration[6.0]
def up
add_cast :text, :uuid, function: :inout, context: :implicit
add_cast :text, :bigint, function: :inout, context: :implicit
def down
remove_cast :text, :uuid
remove_cast :text, :bigint
inopinatus / bouquet-controller.js
Created January 8, 2021 22:08
Proof-of-concept, using a Stimulus wrapper for IntersectionObserver to drive Turbo lazy-loading via click events, with graceful fallback to plain HTML.
import { Controller } from "stimulus"
// Proof of concept for lazy loaded turbo frames
export default class extends Controller {
static targets = ["click", "events", "root"]
static values = {
rootMargin: String,
threshold: Number,
appearEvent: String,
disappearEvent: String
inopinatus / worry.rb
Last active January 8, 2021 02:16
Abusing pattern matching as a proc factory and subsequent invocation
# frozen_string_literal: true
require 'json'
module Proxy
refine Object do
def deconstruct_keys(methods)
methods.to_h { |m| [m, ->(*a) { throw m, public_send(m, *a) }] }
inopinatus / route-authenticators.rb
Created December 22, 2020 09:33
Injecting route-specific dependencies to avoid controller duplication.
# config/routes.rb
defaults authenticator: UserAuthenticator do
resources :todos
scope :api, defaults: { authenticator: APIAuthenticator, format: :json } do
resources :todos
# app/lib/user_authenticator.rb
inopinatus / organisation.rb
Created December 14, 2020 22:57
Value object that replaced an enum
# app/models/organisation.rb
# schema like:
# create_table "organisations", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
# t.string "transponder", default: "my_laps", null: false
class Organisation < ApplicationRecord
attribute :transponder,
inopinatus / uu58.rb
Last active October 11, 2023 20:21
Convert UUIDs to/from base58
module Uu58
# Convert a string UUID to a base58 string representation.
# Output will be padded up to 22 digits if necessary.
# Behaviour is undefined if passing something other than a 128-bit
# hex string in network order with optional interstitial hyphens.
def self.uuid_to_base58(uuid, alphabet = SecureRandom::BASE58_ALPHABET)
ary = uuid.delete("-").to_i(16).digits(58).reverse
ary.unshift(0) while ary.length < 22
inopinatus / application_record.rb
Created October 14, 2020 00:53
values_at for active record models
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
unless instance_methods.include?(:values_at)
def values_at(*methods)! { |method| public_send(method) }
inopinatus / latest_association_by_distinct_on.rb
Last active September 19, 2020 05:09
preloading the latest of an associated collection
class Post
has_many :comments
has_one :latest_comment, -> { latest_by :post_id }, class_name: 'Comment'
class Comment
scope :latest_by, -> column {
order(column, created_at: :desc).arel
inopinatus / matches_any.rb
Created September 16, 2020 18:02
programmatic LIKE in Arel
scope :search_uid, ->(*terms) {
where(arel_attribute(:uid).matches_any( {|term| "%#{sanitize_sql_like term}%" }, nil, true))
inopinatus / callback_registration_tracing.rb
Created September 14, 2020 22:42
initializer for determining where callbacks were registered
# frozen_string_literal: true
module CallbackRegistrationTracing
module Capture
def normalize_callback_params(*args)
super.tap do |_, _, options|
options[:_caller] = caller(3)