Skip to content

Instantly share code, notes, and snippets.

View chaadow's full-sized avatar
🎯
Focusing

Chedli Bourguiba chaadow

🎯
Focusing
View GitHub Profile
@owaiswiz
owaiswiz / print_redundant_indexes.rb
Last active April 23, 2024 16:36
Print redundant indexes in a Rails app.
# Unnecessary indexes slows down writes and consumes additional storage and memory.
# Just paste this snippet in your Rails console (bundle exec rails c).
# And it will print all redundant indexes that are already covered by another index on the table:
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug)
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active)
ActiveRecord::Base.connection.tables.map do |table|
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where)
@hschne
hschne / rate_limit.rb
Created January 11, 2024 17:02
Leaky Bucket Rate Limiter in Ruby
frozen_string_literal: true
# A leaky bucket rate limiter for Ruby
#
# @see https://www.mikeperham.com/2020/11/09/the-leaky-bucket-rate-limiter/
# @see https://en.wikipedia.org/wiki/Leaky_bucket
class RateLimit
class Error < StandardError
attr_accessor :retry_in
@markedmondson
markedmondson / faster_system_tests.rb
Last active August 10, 2023 07:53
Faster system test sign in
module SessionHelper
extend ActiveSupport::Concern
class ::SessionsBypassController < ActionController::Base
def show
session[:user_id] = params[:user_id]
session[:team_id] = params[:team_id]
session[:tenant] = Apartment::Tenant.current
@VeerpalBrar
VeerpalBrar / consistent_hashing.rb
Last active February 3, 2023 09:38
A consistent hashing implementation in ruby
require 'digest'
class ConsistentHashing
def initialize(nodes)
nodes.map { |node| add_node(node) }
end
def find_cache(key)
puts
hash = hash_value(key)
@Envek
Envek / login_helpers.rb
Created October 11, 2021 06:42
Signing-in user for integration tests via cookie-only session with Rails, Devise, Capybara, and Cuprite
# spec/system/support/login_helpers.rb
# See this blog post for setup guide: https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing
module LoginHelpers
def login_as(user)
# Craft session cookie to make request authenticated (to pass even routing constraints)
# Compilation of these:
# - https://dev.to/nejremeslnici/migrating-selenium-system-tests-to-cuprite-42ah#faster-signin-in-tests
# - https://turriate.com/articles/2011/feb/how-to-generate-signed-rails-session-cookie
# - https://github.com/rails/rails/blob/43e29f0f5d54294ed61c31ddecdf76c2e1a474f7/actionpack/test/dispatch/cookies_test.rb#L350

How to run examples

  1. Run $ createdb uniq-db-test to create DB
  2. Run example with Ruby (e.g., $ ruby 1_find_or_create_by_single_thread.rb)

Benchmark output

With many successful INSERTs

Warming up --------------------------------------
@reshleman
reshleman / bug.rb
Created March 11, 2021 19:26
ActiveStorage bug w/ multiple `#attach` calls in a transaction
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@DmitryTsepelev
DmitryTsepelev / 1_graphql_ruby_n_plus_one.md
Last active January 5, 2024 15:16
N+1 in graphql-ruby

How to run examples:

  1. Run $ createdb nplusonedb to create DB
  2. Run specs $ rspec demo.rb
@dhh
dhh / Gemfile
Created June 24, 2020 22:23
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@jesster2k10
jesster2k10 / README.md
Last active April 25, 2024 00:54
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using: