Skip to content

Instantly share code, notes, and snippets.

Avatar
💚

Justin Searls searls

💚
View GitHub Profile
View config.yml
version: 2
jobs:
build:
docker:
- image: cimg/ruby:3.0.1-browsers
environment:
PAGER: cat # https://stackoverflow.com/questions/53055044/rails-rake-dbstructureload-times-out-on-circleci-2-0
SKIP_SCHEMA_DUMP: true # pg_dump: error: server version: 13.2; pg_dump version: 12.6
CYPRESS_RAILS_CYPRESS_OPTS: "--record --key ace11e57-d9d5-443d-8882-b2ecf0488a6d"
@searls
searls / attestation_test.rb
Last active Mar 23, 2021
Approach to testing validations simply
View attestation_test.rb
require "test_helper"
class AttestationTest < ActiveSupport::TestCase
def test_validate_not_already_attested
user = User.new
Attestation.create!(user: user, date: Date.civil(2021, 2, 28))
assert_validation :already_attested, Attestation.new(user: user, date: Date.civil(2021, 2, 16))
assert_validation :already_attested, Attestation.new(user: user, date: Date.civil(2021, 2, 28))
refute_validation :already_attested, Attestation.new(user: user, date: Date.civil(2021, 3, 15))
@searls
searls / 001_functions_and_views.sql
Created Dec 29, 2020
KameSame's December 2020 search overhaul
View 001_functions_and_views.sql
-- This all starts with some functions and a *materialized* postgres view that unnests several
-- arrays of strings of definitions into flattened rows that are easier to search. Fun fact:
-- you can even create indexes on materialized views' columns! They'll refresh whenever the view
-- is refreshed (which in my case is every time that we pull new dictionary data from WaniKani or JMDICT
-- This function will take an array of strings and convert all the double-width alphanumeric characters
-- and normalize them as half-width. That way a search query can be massaged from "OK" to "ok" easily
CREATE OR REPLACE FUNCTION array_hankakufy_romaji(character varying[])
RETURNS character varying[]
AS
View test_search.rb
#!/usr/bin/env ruby
require_relative "../config/environment"
require "minitest/autorun"
# Written as a one-off script because it only works against prod data
module Minitest::Assertions
def assert_time_limit time_in_ms
flunk "assert_time_limit requires a block to measure" unless block_given?
View backup-production.rb
#!/usr/bin/env ruby
# This blog post has helped a lot of folks figure out how to pull down their production database
# from Heroku without bringing their application to a screeching halt.
# https://blog.testdouble.com/posts/2019-11-12-downloading-large-heroku-postgres-backups/
#
# Here's a silly little script that will do it all for you. (YMMV, don't actually use this, etc)
require "date"
View sigh.md

SOLVED. Problem:

Rails 6.1 deprecates this:

errors[:base] << "Assignment #{active_assignment.id} is already active for this agent and this project")

And says to call add, so I did:

errors.add("Assignment #{active_assignment.id} is already active for this agent and this project")
View wat.md

Debugging a CORS header issue

Just wasted a bunch of time thinking that our static-rails gem was messing up CORS headers. Turned out that AWS Cloudfront just randomly choked on a handful of assets across our properties and the only way to fix it was to manually invalidate (read: cache-bust) them and force it to go and refetch them.

This particular font served by AWS cloudfront, when requested by Chrome, is not returning the CORS Access-Control-Allow-Origin header.

The bad request:

This is the cURL command provided by right-click copy in Chrome devtools with --verbose tacked on to see the header:

@searls
searls / do-this.js
Created May 24, 2020
How to play web audio that won't interrupt background system-wide audio playback in iOS
View do-this.js
let audioContext
export function play (url) {
const AudioContext = window.AudioContext || window.webkitAudioContext
if (!audioContext) audioContext = new AudioContext()
const request = new window.XMLHttpRequest()
request.open('GET', url, true)
request.responseType = 'arraybuffer'
request.addEventListener('load', function (e) {
const source = audioContext.createBufferSource()
source.buffer = audioContext.createBuffer(request.response, false)
View sets_csrf_token.rb
module StaticRails
class SetsCsrfToken
def call(req, res)
res.set_cookie("_csrf_token",
value: masked_authenticity_token(req.session),
path: "/",
expires: Time.now + 24 * 60 * 60)
end
private
@searls
searls / reset_fixtures
Created Mar 3, 2020
Manually your Rails test fixtures whenever you want
View reset_fixtures
#!/usr/bin/env ruby
ENV["RAILS_ENV"] = "test"
require_relative "../config/environment"
ResetsFixtures.new.call