Skip to content

Instantly share code, notes, and snippets.

View searls's full-sized avatar
💚

Justin Searls searls

💚
View GitHub Profile
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem "rspec", "~> 3.10"
end
require "rspec/autorun"
RSpec.configure do |config|
@searls
searls / whereable.rb
Created September 4, 2021 16:06
The initial implementation of a Whereable query filter for KameSame.
class Whereable
def initialize(where:, model: Item, ranking_conditions: [], valid: true, data_source: nil)
@model = model
@where = where
@data_source = data_source
@ranking_conditions = ranking_conditions
@valid = valid
end
def valid?
@searls
searls / searches_stuff.rb
Last active September 1, 2021 14:24
Whereable Query Pattern. A design pattern for combining numerous types of queries only if the query is relevant into a single query using ARel
# I heard Whereables were having a moment.
#
# More on the real version of this feature here:
# https://community.wanikani.com/t/kamesame-a-fast-feature-rich-japanese-memorization-webapp/31319/1575?u=searls
#
# Below is simplified but basic usage:
#
# SearchesStuff.new.call("arigatou") # finds ありがとう
# SearchesStuff.new.call("にほんご") # finds 日本語
# SearchesStuff.new.call("探して") # finds 探す
@searls
searls / 1-without-lifecycle-hooks.txt
Created July 30, 2021 15:41
Speed of test_data with and without using the lifecycle hooks introduced in 0.2.1
$ time bin/rails test
Run options: --seed 6029
# Running:
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Finished in 22.966130s, 20.7697 runs/s, 19.7682 assertions/s.
477 runs, 454 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Minitest to /Users/justin/code/testdouble/present/coverage. 2793 / 3633 LOC (76.88%) covered.
@searls
searls / 1-slow_tests.rb
Last active July 20, 2021 15:00
An example of using the time_up gem to investigate performance issues by timing different types of activities. Repo: https://github.com/testdouble/time_up
class ActiveSupport::TestCase
def self.test_data_source(source)
case source
when :test_data
setup do
TestData.load
RefreshesMaterializedViews.new.call
end
teardown do
@searls
searls / 1-hmm_test.rb
Created June 23, 2021 12:50
Setup/Teardown behavior in Rails tests
class ActiveSupport::TestCase
def setup
$rails_test_def_setup = true
end
setup do
$rails_test_setup_do = true
end
def teardown
@searls
searls / Extract Flashcard Text.kmmacros
Created May 30, 2021 15:03
A Keyboard Maestro macro for extracting flashcards from the Kindle app for macOS. Start the macro when you're at the beginning of a flashcard deck
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Activate</key>
<string>Normal</string>
<key>CreationDate</key>
<real>640009344.41797602</real>
<key>Macros</key>
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 March 23, 2021 14:06
Approach to testing validations simply
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 December 29, 2020 13:32
KameSame's December 2020 search overhaul
-- 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