Skip to content

Instantly share code, notes, and snippets.

View fractaledmind's full-sized avatar

Stephen Margheim fractaledmind

View GitHub Profile
@fractaledmind
fractaledmind / app__controllers__concerns__oauth.rb
Last active May 27, 2024 15:34
Basic OAuth implementation for a Rails app
module OAuth
extend ActiveSupport::Concern
SIGN_UP = "sign_up"
SIGN_IN = "sign_in"
DESTINATION_PARAMS_KEY = :destination
DESTINATION_SESSION_KEY = "oauth.destination"
ORIGIN_PARAMS_KEY = :origin
ORIGIN_SESSION_KEY = "oauth.origin"
@fractaledmind
fractaledmind / seeds.rb
Created May 14, 2024 16:14
Example of simple file to centralize User management
users_file = Rails.root.join('storage', 'users.yml.erb')
if File.exist? users_file
users = YAML.load(ERB.new(File.read(users_file)).result)
User.insert_all(
users.map { |user| user.except("password") },
unique_by: :username
)
end
@fractaledmind
fractaledmind / application_controller.rb
Created May 14, 2024 15:37
Example of minimal authentication requirements for Rails app
ass ApplicationController < ActionController::Base
before_action :authenticate!
helper_method :current_user
helper_method :user_signed_in?
private
def authenticate
@current_user = nil
@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)