Skip to content

Instantly share code, notes, and snippets.

# Sprockets no longer creates undigested assets, so we have to emulate the old behavior to keep vendored JS libraries happy.
# This automatically uses the newest asset in cases where the same file exists multiple times (like with Capistrano).
#
# https://github.com/rails/sprockets-rails/issues/49
#
# digested: 4-star-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg
# undigested: 4-star.jpg
assets_without_digest = -> do
digest = /(-{1}[a-z0-9]{64}*\.{1}){1}/
assets = Dir.glob File.join Rails.root, 'public/assets/**/*'
# written for https://github.com/rails/rails/pull/30919
# 1. find all gems that depend on a particular gem
# 2. download them all
# 3. search through their source!
auth = 'Authorization:YOUR_API_KEY'
gems = JSON.parse(`curl -H '#{auth}' https://rubygems.org/api/v1/gems/activerecord/reverse_dependencies.json`); nil
gems.size
@seanlinsley
seanlinsley / log_formatting.rb
Last active April 14, 2017 16:07
proper logging -- with color :)
# Define a setter to pass in a custom log formatter
class ActiveSupport::BufferedLogger
def formatter=(formatter)
@log.formatter = formatter
end
end
# Defines a custom log format (time, severity, message, PID, backtrace)... all with color!
class Formatter
SEVERITY_TO_TAG = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'}
# This asks the user if they want a read-only or read-write PSQL session. PSQL
# provides `\prompt`, but that doesn't provide a way to conditionally prompt
# based on whether it was being run interactively, or from bash-completion.
#
# Save this file to ~/.psqlprompt and add the below to ~/.psqlrc:
#
# \set read_only `bash ~/.psqlprompt`
# set default_transaction_read_only = :read_only;
#
function psql_prompt() {
,.---._
,,,, / `,
\\\\ / '\_ ;
|||| /\/``-.__\;'
::::/\/_
{{`-.__.-'(`(^^(^^^(^ 9 `.========='
{{{{{{ { ( ( ( ( (-----:=
{{.-'~~'-.(,(,,(,,,(__6_.'=========.
::::\/\
|||| \/\ ,-'/,
@seanlinsley
seanlinsley / def!.rb
Last active January 12, 2016 23:56
For slightly more future-proof monkey patches, raise an error if a method name starts being used upstream
# class SomethingYouDontControl
# def! :something_you_expect_not_to_exist do |arg|
# arg + 1
# end
# end
#
# I wanted to provide built-in memoization, but it's not currenlty possible
# while preserving strict argument checking. https://bugs.ruby-lang.org/issues/9777
#
module Kernel
@seanlinsley
seanlinsley / application_helper.rb
Created January 12, 2014 23:45
Use Arbre in a helper!
module ApplicationHelper
def arbre(&block)
Arbre::Context.new(&block).to_s
end
end
begin
# your code
rescue
tries ||= 0
(tries += 1) < 3 ? retry : raise
end
@seanlinsley
seanlinsley / wire.rb
Created June 5, 2013 19:28
Ever wonder what a block is doing?
# This class lets you evaluate any block
# and record any methods it calls.
#
# Wire.listen { foo + bar }
# # => [:foo, :bar, :+]
#
# You're also able to chain method calls!
#
# Wire.listen { foo + bar.baz }
# # => [:foo, :bar, :baz, :+]
@seanlinsley
seanlinsley / ransack_form.rb
Created April 17, 2013 23:06
Debugging for github.com/ernie/ransack/issues/173 and github.com/gregbell/active_admin/pull/1979
gem 'rails', '3.2.13'
gem 'ransack'
require 'rails/all'
require 'ransack'
ActiveRecord::Base.establish_connection adapter: 'sqlite3',
database: ':memory:'
ActiveRecord::Schema.define do
create_table :users, force: true do |t|