Skip to content

Instantly share code, notes, and snippets.

View fractaledmind's full-sized avatar

Stephen Margheim fractaledmind

View GitHub Profile
@fractaledmind
fractaledmind / router.rb
Created February 19, 2024 09:12
A singleton class for Rails apps to generate URLs easily from anywhere in their app.
# frozen_string_literal: true
module Router
class << self
include Rails.application.routes.url_helpers
def default_url_options
Rails.application.config.action_controller.default_url_options || {}
end
@fractaledmind
fractaledmind / test_sqlite3_busy_timeouts.rb
Created January 4, 2024 21:05
This test script demonstrates how the busy_timeout holds the GVL while retrying, while a busy_handler timeout will release the GVL between retries
require 'sqlite3'
require 'minitest/autorun'
puts "info: gem version: #{SQLite3::VERSION}"
puts "info: sqlite version: #{SQLite3::SQLITE_VERSION}/#{SQLite3::SQLITE_LOADED_VERSION}"
puts "info: sqlcipher?: #{SQLite3.sqlcipher?}"
puts "info: threadsafe?: #{SQLite3.threadsafe?}"
class TestCase < Minitest::Test
def setup
@fractaledmind
fractaledmind / sqlite-busy-handler-benchmarks.rb
Last active December 20, 2023 16:50
A benchmarking script to compare different kinds of `busy_handler` implementations for a SQLite3::Database connection under concurrent load
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "sqlite3"
gem "enumerable-statistics"
end
require "benchmark"
<form action="#">
<section data-controller="passphrase">
<label for="passphrase-for-registration">Passphrase</label>
<input type="text"
id="passphrase-for-registration"
data-passphrase-registration-target="input"
data-action="passphrase#validate"
autocomplete="new-password"
required
minlength="12"
@fractaledmind
fractaledmind / has_many_attached.rb
Created October 25, 2023 20:21
Vanilla Rails isomorphic attachment validations
# /app/models/concerns/has_many_attached.rb
module HasManyAttached
extend ActiveSupport::Concern
class_methods do
def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false, **options)
super(name, dependent: :purge_later, service: nil, strict_loading: false)
if options[:file_types].any?
validate "validate_#{name}_file_types".to_sym
@fractaledmind
fractaledmind / activerecord-benchmark.rb
Last active January 30, 2024 20:33
A benchmarking script for ActiveRecord with the SQLite3 adapter
require 'benchmark'
require 'active_record'
require 'pg'
# -----------------------------------------------------------------------------
INPUT_COLUMNS = {
name: "Name of benchmark",
iters: "Number of iterations the block is run",
usr_time: "Amount of user CPU time",
@fractaledmind
fractaledmind / dbsnap.rake
Created September 14, 2023 11:18
A Rake namespace for working with local SQLite database snapshots
namespace :db do
namespace :snap do
task setup: :environment do
@snapshot_dir = Rails.root.join('storage/snapshots')
@db_path = ActiveRecord::Base.connection_db_config.database
@db_name = @db_path.rpartition('/').last.remove('.sqlite3')
end
task setup_snaps: :setup do
@snaps = Pathname(@snapshot_dir)
@fractaledmind
fractaledmind / omniauth_controller.rb
Last active September 12, 2023 11:18
A standard Rails controller to take ownership of the OAuth process from the OmniAuth gem
class Sessions::OmniauthController < ApplicationController
skip_before_action :verify_authenticity_token
skip_before_action :authenticate!
# GET|POST /auth/:provider
def passthru
render status: 404, plain: "Not found. Authentication passthru."
end
# GET|POST /auth/:provider/callback
module ArrayColumns
extend ActiveSupport::Concern
class_methods do
def array_columns_sanitize_list(values = [])
return [] if values.nil?
values.select(&:present?).map(&:to_s).uniq.sort
end
def array_columns(*column_names)
@fractaledmind
fractaledmind / install_sqlite.sh
Created September 10, 2023 09:04
A script to install and compile a custom SQLite installation
#!/usr/bin/env sh
cd ../vendor
mkdir -p sqlite/bin sqlite/lib sqlite/include
# ============================================================================================================
# Compile and install sqlite3 (for performance turning and build customizations)
# SEE: https://www.sqlite.org/compile.html
# NOTE: The sqlite3 Ruby gem will not work with the following compile time flags
# * -DSQLITE_OMIT_DEPRECATED