Skip to content

Instantly share code, notes, and snippets.

ryanb / application.html.erb
Created April 20, 2024 05:21
Simple GetText solution for JavaScript for use with
<%# app/views/layouts/application.html.erb %>
<%= javascript_include_tag "locales/#{I18n.locale}", nonce: true %>
# This should be triggered through a cron job at 6:15 PM on week days.
# Example cron: 15 18 * * 1,2,3,4,5 cd ~/code/jury_duty && ~/.rbenv/shims/ruby call.rb >> ~/code/jury_duty/call-log.txt 2>&1
# It uses Twilio to make a call based on TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE.
# It calls the JURY_PHONE, transcribes it, looks for JURER_NUMBER and texts the result to PERSONAL_PHONE.
require "rubygems"
require "bundler/setup"
ryanb / 1_create_nodes_migration.rb
Last active February 27, 2024 13:14
A variation of closure_tree with support for multiple parents
class CreateNodes < ActiveRecord::Migration[6.0]
def change
create_table :nodes do |t|
create_table :node_edges do |t|
t.integer :parent_id, null: false
t.integer :child_id, null: false
ryanb / shrine_storage.rb
Created January 31, 2018 20:25
Swap between file system and in-memory storage for Shrine file uploads with RSpec.
# This sets up both a memory store and file system store for Shrine file uploads.
# We use the memory store by default but in cases where you want to test actual
# file uploads you can enable it with `file_upload: true` on a per example basis
require "shrine/storage/memory"
require "shrine/storage/file_system"
# We use a delegator to swap out the storage dynamically
# since the storage hash is duplicated for each uploader
class Shrine::Storage::Dynamic < SimpleDelegator
def initialize(initial, storages)
ryanb / episode_search.rb
Created February 18, 2013 00:44
Class that handles the PostgreSQL full text search for
class EpisodeSearch
attr_reader :ability, :options
delegate :sanitize, to: Episode
def initialize(ability, options = {})
@ability = ability
@options = options.dup
def tag
cd ~/code/rails
railties/bin/rails new ~/code/hello --edge
cd ~/code/hello
echo "gem 'puma'" >> Gemfile
rails g controller hello index
# fill controller
# separate tab
curl localhost:9292/hello/index
ryanb /
Created December 6, 2012 01:04
Alternative expectation interface for MiniTest and RSpec


I took the ideas presented here and built a gem called Mustard. Check it out!

There are several expectation/assertion interfaces available for writing tests/specs. Here are some issues I have with them.


  • The order of assert_equals feels backwards
  • Oh wait, that should be assert_equal (that too)
ryanb /
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
ryanb / development.rb
Created November 27, 2012 21:38
Some rack middleware to add headers to asset pipeline.
class AssetHeaders
def initialize(app)
@app = app
def call(env)
request =
response =
if request.path =~ /^\/assets\//
# there maybe a better way to add headers
ryanb / tasks_controller_refactoring.rb
Created November 9, 2012 23:07
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
class TasksController < ApplicationController
def update
tracker =
if @task.update_attributes(params[:task]), socket_id).push_changes, current_user).deliver_email
# success response
# failure respond