Skip to content

Instantly share code, notes, and snippets.

@glaucocustodio
glaucocustodio / frequency_helpers.js
Last active February 10, 2026 17:13
Throttle & Debounce functions
/*
Throttle ensures fn runs at most once per delay period (default 1 second).
On the first call, it immediately invokes fn and sets a timeout.
Any subsequent calls during the delay window are ignored.
Once the timeout expires, timeoutId resets to null, allowing the next call through.
This is a "leading-edge" throttle — the function fires right away, then locks out for delay ms.
Note that calls made during the cooldown are silently dropped (not queued), so the last trailing call can be lost.
*/
export function throttle(fn, delay = 1000) {
let timeoutId = null
@glaucocustodio
glaucocustodio / form_submit_disabler_controller.js
Created October 23, 2025 14:13
Disable the submit button of a form (for turbo powered submits)
import { Controller } from "@hotwired/stimulus"
/*
Example usage:
<div data-controller="form-submit-disabler" data-form-submit-disabler-disable-with-value="Creating..."
<form data-form-submit-disabler-target="form">
<button data-form-submit-disabler-target="submitButton">Create</button>
</form>
@glaucocustodio
glaucocustodio / query_trackable.rb
Created June 12, 2025 14:58
A module to track/output Active Record queries
module QueryTrackable
extend ActiveSupport::Concern
#
# Prints the number of queries and the time spent in milliseconds
# Useful to track the performance of isolated classes
#
# Usage:
#
# class MyClass
@glaucocustodio
glaucocustodio / disable_with_controller.js
Last active October 23, 2025 10:26
a Stimulus controller to disable elements on click since Rails 7.0 has dropped UJS (so `data-disable-with` is no longer available)
import { Controller } from '@hotwired/stimulus'
/*
Disable a button when it is clicked
Ps: Rails 7.0 has dropped UJS, so `data-disable-with` is no longer available
Example usage:
<%= button_to 'Confirm', foo_path, method: :post, class: "btn btn-warning", data: {controller: 'disable-with', 'disable-with-content-value' => 'Confirming...'} %>
@glaucocustodio
glaucocustodio / short_variable_name.rb
Created July 10, 2024 14:20
custom rubocop cop for checking if variable names are too short
# frozen_string_literal: true
# put this file at .rubocop/cop/naming/short_variable_name.rb
# then ensure you have the following in .rubocop.yml:
#
# require:
# - .rubocop/cop/naming/short_variable_name.rb
#
# Naming/ShortVariableName:
# Enabled: true
@glaucocustodio
glaucocustodio / redundant_require_rails_helper.rb
Last active July 10, 2024 14:02
custom rubocop cop for checking if spec files include `require 'rails_helper'`
# frozen_string_literal: true
# put this file at .rubocop/cop/rspec/redundant_require_rails_helper.rb
# then ensure you have the following in .rubocop.yml:
#
# require:
# - .rubocop/cop/rspec/redundant_require_rails_helper.rb
#
# RSpec/RedundantRequireRailsHelper:
# Enabled: true
@glaucocustodio
glaucocustodio / job_debouncer.rb
Last active September 15, 2023 20:33
ActiveJob debouncer
# app/jobs/job_debouncer.rb
module JobDebouncer
extend ActiveSupport::Concern
class_methods do
def perform_later_once(*args, wait_time: 15.seconds)
job_key = "job_debouncer_#{self}_#{args}"
return if Rails.cache.exist?(job_key)
@glaucocustodio
glaucocustodio / auto_formatting_changed_ruby_files_with_standard.md
Last active August 8, 2021 19:29
Auto formatting changed Ruby files with Standard

Prerequisites

Create a git pre commit hook and give it permission to execute:

touch .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
@glaucocustodio
glaucocustodio / fixed_thread_pool_example.rb
Last active March 29, 2022 09:28
FixedThreadPool / Concurrent::Future usage example (concurrent-ruby gem)
# For more: https://github.com/ruby-concurrency/concurrent-ruby
num_threads = Concurrent.processor_count # or whatever you prefer like 4
thread_pool = Concurrent::FixedThreadPool.new(num_threads)
products = Product.all
executors = products.map { |product|
Concurrent::Future.execute({executor: thread_pool}) do
p "processing #{product.id}"
end
@glaucocustodio
glaucocustodio / normalization.rb
Last active June 11, 2021 16:57
Implements 3 types of normalization in ruby
class Array
# Few types of normalizations
# https://developers.google.com/machine-learning/data-prep/transform/normalization
def normalize
return if self.empty?
x_min, x_max = self.minmax
dx = (x_max-x_min).to_f