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
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
ryanb / episode_search.rb
Created Feb 18, 2013
Class that handles the PostgreSQL full text search for railscasts.com
View episode_search.rb
class EpisodeSearch
attr_reader :ability, :options
delegate :sanitize, to: Episode
def initialize(ability, options = {})
@ability = ability
@options = options.dup
end
def tag
View commands.sh
cd ~/code/rails
railties/bin/rails new ~/code/hello --edge
cd ~/code/hello
echo "gem 'puma'" >> Gemfile
bundle
rails g controller hello index
# fill controller
puma
# separate tab
curl localhost:9292/hello/index
@ryanb
ryanb / expectations.md
Created Dec 6, 2012
Alternative expectation interface for MiniTest and RSpec
View expectations.md

Expectations

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.

Test::Unit/MiniTest

  • The order of assert_equals feels backwards
  • Oh wait, that should be assert_equal (that too)
@ryanb
ryanb / issues_with_modules.md
Created Nov 29, 2012
Points on how modules can make code difficult to read.
View issues_with_modules.md

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
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
end
def call(env)
request = Rack::Request.new(env)
response = @app.call(env)
if request.path =~ /^\/assets\//
# there maybe a better way to add headers
@ryanb
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 = TaskTracker.new(@task)
if @task.update_attributes(params[:task])
TaskPusher.new(tracker, socket_id).push_changes
TaskMailSender.new(tracker, current_user).deliver_email
# success response
else
# failure respond
end
@ryanb
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?
AdminAbility.new(user)
else user
MemberAbility.new(user)
else
GuestAbility.new
end
end
@ryanb
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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>RailsCasts</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
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.