Skip to content

Instantly share code, notes, and snippets.


Justin Searls searls

View GitHub Profile
View Gemfile
source ''
git_source(:github) { |repo| "{repo}.git" }
ruby '2.7.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails'
# Use Puma as the app server
gem 'puma'
# Build JSON APIs with ease. Read more:
searls / index.html
Last active Nov 23, 2022
Mobile styling overrides for NetlifyCMS to make it more responsive-ish.
View index.html
<!doctype html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Content Manager</title>
searls / .solargraph.yml
Last active Nov 29, 2022 — forked from DRBragg/.solargraph.yml
My config with steps to use solargraph for Rails projects in VS Code (WIP)
View .solargraph.yml
- ".solargraph_definitions.rb"
- "app/**/*.rb"
- "config/**/*.rb"
- "lib/**/*.rb"
- test/**/*
- vendor/**/*
- ".bundle/**/*"
searls / organization.rb
Created Nov 4, 2022
This is easily my least favorite Rails model validation scenario. An association exists and at least one associated item must conform to a specific condition for the owning side of the relationship to be considered valid. In this case, to avoid orphaning organizations
View organization.rb
class Organization < ApplicationRecord
has_many :users
searls / form.html.erb
Last active Oct 21, 2022
Example Tailwind form builder for Rails
View form.html.erb
<%= form_with model: @user, url: account_path do |f| %>
<%= f.text_field :name %>
<%= f.email_field :email, disabled: true %>
<%= f.submit "Save" %>
<% end %>
<%= form_with url: login_email_path, method: :delete do |f| %>
<%= f.submit "Log out", variant: :reset %>
<% end %>
View runs_sales_report_test.rb
require "date"
require "mocktail"
require "minitest/autorun"
class FetchesOrdersAndItems
Result =, :items, keyword_init: true)
def fetch(start_date, end_date)
searls / executes_command.rb
Created Apr 3, 2022
Silly little class for when you want the combined stdout/stderr output of a command and its success status
View executes_command.rb
require "open3"
class ExecutesCommand
Result =, :output, keyword_init: true)
def call(command)
stdin, stdout_and_stderr, wait_thr = Open3.popen2e(command)
result =
success: wait_thr.value == 0,
searls / whoops.rb
Created Jan 11, 2022
I mistakenly thought that re-assigning all entries on an association would cascade a save! on the owner of the relationship to the depended models if they were dirty. Turns out, nope!
View whoops.rb
class CountsInventory
def count_inventory(store)
Inventory.find_or_initialize_by(store: store).tap do |inventory|
inventory.items = count_items(inventory) # Updates which items are associated, does not cascade save to them!
def count_items(inventory)

I was having issues with getting my Web Audio effects in KameSame loud enough to be audible when an iOS device was simultaneously playing music in the background. Because I like to listen to music while I study, here's how I increased the volume of the sound effect relative to the music in iOS.

Here's the original function in my app for playing audio in iOS (all other platforms work fine with new window.Audio(url).play()):

function playIos (url) {
  const audioContext = new AudioContext()
  const source = audioContext.createBufferSource()
  const request = new window.XMLHttpRequest()'GET', url, true)
searls / upsert_all_or_else_delete.rb
Created Dec 24, 2021
This is an attempt to efficiently upsert/overwrite a large chunk of records' (items) associated rows (terms).
View upsert_all_or_else_delete.rb
Item.where("updated_at > ?", after).includes(:english_terms).find_in_batches(batch_size: 5000).with_index do |items, i|
good_term_attrs = []
bad_term_ids = []
items.each do |item|
# This just gathers all the potential valid english terms associated with a dictionary entry:
term_texts = { |s|