Skip to content

Instantly share code, notes, and snippets.

ryanb / 1_create_nodes_migration.rb
Last active May 18, 2021
A variation of closure_tree with support for multiple parents
View 1_create_nodes_migration.rb
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 / 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 / index.js.erb
Created Dec 16, 2011
Infinite scrolling solution covered in revised episode #114:
View index.js.erb
$('#products').append('<%= j render(@products) %>');
<% if @products.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@products) %>');
<% else %>
<% end %>
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 / application.html.erb
Created Jan 19, 2011
Example of very simple password authentication.
View application.html.erb
<!-- layout file -->
<% if current_user %>
Welcome <%= current_user.username %>. Not you? <%= link_to "Log out", logout_path %>
<% else %>
<%= link_to "Sign up", signup_path %> or <%= link_to "log in", login_path %>.
<% end %>
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
View nesting_association_attributes_examples.rb
# Examples of Nesting Associated Attributes
# This is a look at the various ways to set associated attributes through
# nested parameters. For example, let's say Project has_many :tasks and
# we want to update the tasks the same time we edit a project. There are
# several ways this interface could be handled, and here are a few.
# For these examples, we're trying to create two new tasks and update an
# existing task (with id 3) through a project.
# Approach 1
ryanb / github_tree_slider.js
Created Dec 6, 2010
This is how GitHub's new AJAX file browser works.
View github_tree_slider.js
GitHub.TreeSlider = function () {
if (window.history && window.history.pushState) {
function a() {
if (e.sliding) {
e.sliding = false;
if (!($("#slider").length == 0 || !GitHub.shouldSlide)) if (!navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
#!/usr/bin/env bash
apt-get -y update
apt-get -y install build-essential zlib1g-dev libssl-dev libreadline5-dev libyaml-dev
cd /tmp
tar -xvzf ruby-1.9.3-p125.tar.gz
cd ruby-1.9.3-p125/
./configure --prefix=/usr/local
make install
ryanb / railscasts_episodes.rb
Created Jun 4, 2012
Download source code for all RailsCasts episodes. You may want to cd into an empty directory first.
View railscasts_episodes.rb
require "rubygems"
require "octokit" # gem install octokit
1.upto(5) do |page|
Octokit.repositories("railscasts", page: page, per_page: 100).each do |repo|
system "git clone git://{}"