Skip to content

Instantly share code, notes, and snippets.

@pcreux
pcreux / Gemfile
Last active November 6, 2025 11:25
Fast Rails + Heroku Configuration
group :production do
gem 'unicorn'
# Enable gzip compression on heroku, but don't compress images.
gem 'heroku-deflater'
# Heroku injects it if it's not in there already
gem 'rails_12factor'
end
@pcreux
pcreux / turboframe_missing_handler.js
Created July 21, 2025 07:42
A developer friendly turboframe error handler
// Display a developer-friendly error message when Turbo Frame fails to load.
if (process.env.NODE_ENV === 'development') {
document.addEventListener('turbo:frame-missing', async function (event) {
event.preventDefault();
const frame = event.target;
const response = event.detail.response;
const frameId = frame.id;
const url = response?.url || frame.src;
@pcreux
pcreux / authorizations.rb
Last active September 8, 2025 12:50
All introspection of authorization rules to understand why users can/cannot perform an action. That helps with debugging and offers better error messages.
# I would really like to introspect complex authorization rules to understand why users can/cannot perform an action.
# That helps with debugging and offers better error messages.
# Sample:
#
# ```ruby
# auth = Authorize.new(CanUpdate, owner, message_1)
# pp auth.permitted? # => false
# pp auth.explain
# # => "(❌ IsAdmin) or (✅ IsPublisher and ❌ IsOwner)"
@pcreux
pcreux / foreign_key_helpers.rb
Created March 28, 2012 07:42
Foreign Key Helper for Rails Migrations
module ForeignKeyHelpers
# Add a foreign key constraint binding :table :column to the 'id' column of the target_table
#
# By default, a column called 'user_id' will be binded to the column 'id' of the
# table 'users' via a foreign key called 'fk_:table_:column_:target_table'.
# You can override the default behaviour passing extra attributes :target_table,
# :target_column and :constraint_name.
def add_foreign_key(table, column, args = {})
args = args.clone
target_table = args.delete(:target_table) || column.to_s.gsub('_id', '').pluralize
@pcreux
pcreux / dbt_to_dbdiagram.rb
Created May 3, 2021 16:15
Generate an ERD via dbdiagram.io from a dbt project.
#!/usr/bin/env ruby
# Generate a dbdiagram for dbdiagram.io from a dbt project.
#
# Usage:
# 1. Run `dbt docs generate` first.
# 2. Run `dbt_to_dbdiagram.rb`
# 3. Paste the output in https://dbdiagram.io/
require 'yaml'
require 'json'
@pcreux
pcreux / multi_command.rb
Created December 13, 2024 08:40
Ruby script to run commands in parallel, print the output with different colors, and provide a summary.
#!/usr/bin/env ruby
# Run commands in parallel, print the output with different colors,
# and provide a summary.
COMMANDS = [
"curl -v www.google.com",
"curl -v www.yahoo.com",
"dig www.google.com",
"hoo",
@pcreux
pcreux / clone-heroku-app
Last active July 26, 2024 17:19
Script to clone a heroku app (including buildpacks, users, add-ons, environment variables, lab features)
#!/usr/bin/env ruby
# Copy a heroku app (buildpacks, add-ons, labs, config, users).
# This script is idempotent so it can run against an existing app.
#
# Usage:
# $> clone-heroku-app source-app target-app
require 'json'
@pcreux
pcreux / Jabber-SH
Created December 17, 2009 06:17
Jabber-SH — SH console via XMPP/Jabber (GTalk) Jabber-SH allows to you to administrate a remote computer via a command line through a Jabber client. It’s like SSH via GoogleTalk! :)
#!/usr/bin/env ruby
# Jabber-SH — SH console via XMPP/Jabber (GTalk)
#
# Jabber-SH allows you to administrate a remote computer via a command line
# through a Jabber client. It’s like SSH via GoogleTalk! :)
# This is just a hack but it might be usefull sometime to run basic commands
# on a machine that is not accessible via ssh.
#
# Philippe Creux. pcreux/AT/gmail/DOT/com
@pcreux
pcreux / dbt-ctags.rb
Created December 4, 2023 16:51
Generate ctags for dbt models and macros
#!/usr/bin/env ruby
# Generate ctags for dbt models and macros.
sql_files = Dir['macros/**/*.sql'] + Dir['models/**/*.sql']
mappings = sql_files
.map { |path| [File.basename(path, '.sql'), path] }
.sort
File.open('tags', 'w') do |f|
@pcreux
pcreux / README.md
Last active July 3, 2023 09:46
A client for Basecamp 4