Skip to content

Instantly share code, notes, and snippets.

@ryanb
ryanb / abilities.rb
Created September 15, 2012 19:23
How you can break up large Ability class in CanCan
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 June 29, 2012 18:23
RailsCasts TextMate Theme
<?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>
@ryanb
ryanb / Default.sublime-keymap
Created June 29, 2012 18:16
Sublime Settings
[
{ "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" }
]
@ryanb
ryanb / railscasts_episodes.rb
Created June 4, 2012 06:01
Download source code for all RailsCasts episodes. You may want to cd into an empty directory first.
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://github.com/railscasts/#{repo.name}"
end
end
@ryanb
ryanb / chef_solo_bootstrap.sh
Created April 5, 2012 04:35
Bootstrap Chef Solo
#!/usr/bin/env bash
apt-get -y update
apt-get -y install build-essential zlib1g-dev libssl-dev libreadline5-dev libyaml-dev
cd /tmp
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p125.tar.gz
tar -xvzf ruby-1.9.3-p125.tar.gz
cd ruby-1.9.3-p125/
./configure --prefix=/usr/local
make
make install
@ryanb
ryanb / 1_let.rb
Created March 29, 2012 23:26
let vs def
desc "A user's comment" do
let(:user) { User.create! name: "John" }
let(:comment) { user.comments.create! }
it "delegates to user's name" do
comment.name.should eq(user.name)
end
end
@ryanb
ryanb / 0_the_explanation.md
Created December 22, 2011 06:23 — forked from jonsmock/0_the_explanation.txt
Oh the conditionals!

This example is not my real app, but it's modeled pretty closely to it.

My mom schedules a couple hundred employees at a local warehouse. In their system, a worker can earn "points" for being late or missing shifts. Here we have a sort of summary screen for my mom, who may need to follow up with new employees or discipline employees with concerning levels of points.

Questions that arise as I code something like this:

  • Which objects deserve presenters?
@ryanb
ryanb / index.js.erb
Created December 16, 2011 23:22
Infinite scrolling solution covered in revised episode #114: http://railscasts.com/episodes/114-endless-page-revised
$('#products').append('<%= j render(@products) %>');
<% if @products.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@products) %>');
<% else %>
$('.pagination').remove();
<% end %>
@ryanb
ryanb / employees_controller_test.rb
Created September 30, 2011 20:48 — forked from noahhendrix/employees_controller_test.rb
Testing a regular user can not create a user
require 'test_helper'
require 'clearance/testing'
class EmployeesControllerTest < ActionController::TestCase
test "an ASM can not create a user" do
sign_in
assert_raise CanCan::AccessDenied do
post :create, :employee => { :name => 'Feed Store' }
end
@ryanb
ryanb / spec_helper.rb
Created September 12, 2011 21:37
Use RSpec tags to add behavior around specs.
# Add this to your spec_helper.rb
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.around(:each, :vcr) do |example|
name = example.metadata[:full_description].downcase.gsub(/\W+/, "_").split("_", 2).join("/")
VCR.use_cassette(name, :record => :new_episodes) do
example.call
end
end
end