Skip to content

Instantly share code, notes, and snippets.

Ryan Bates ryanb

Block or report user

Report or block ryanb

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
ryanb / shrine_storage.rb
Created Jan 31, 2018
Swap between file system and in-memory storage for Shrine file uploads with RSpec.
View shrine_storage.rb
# 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 Feb 18, 2013
Class that handles the PostgreSQL full text search for
View episode_search.rb
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 Dec 6, 2012
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 Nov 29, 2012
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 < ActiveRecord::Base
ryanb / development.rb
Created Nov 27, 2012
Some rack middleware to add headers to asset pipeline.
View development.rb
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 Nov 9, 2012
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
View tasks_controller_refactoring.rb
class TasksController < ApplicationController
def update
tracker =
if @task.update_attributes(params[:task]), socket_id).push_changes, current_user).deliver_email
# success response
# failure respond
ryanb / abilities.rb
Created Sep 15, 2012
How you can break up large Ability class in CanCan
View abilities.rb
module Abilities
def self.ability_for(user)
if user.admin?
else user
ryanb / RailsCasts.tmtheme
Created Jun 29, 2012
RailsCasts TextMate Theme
View RailsCasts.tmtheme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
View Default.sublime-keymap
{ "keys": ["ctrl+shift+."], "command": "erb" },
{ "keys": ["ctrl+shift+,"], "command": "insert_snippet", "args": { "name": "Packages/XML/long-tag.sublime-snippet" } },
{ "keys": ["ctrl+super+r"], "command": "reveal_in_side_bar" }
You can’t perform that action at this time.