Skip to content

Instantly share code, notes, and snippets.

@harlow
harlow / capybara_example.rb
Created February 26, 2012 18:58
Capybara Cheat Sheet
# Navigating
visit('/projects')
visit(post_comments_path(post))
# Clicking links and buttons
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@harlow
harlow / gist:2510198
Created April 27, 2012 15:32
Gitsucker
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
require 'json'
class Repo
def initialize(name)
@name = name
end
@harlow
harlow / merge_pull_requests.md
Created October 26, 2012 19:10
Steps to accepting pull requests from open source contributors

Add contributors pull requests to your origin remote

# .git/config
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
  url = git@github.com:thoughtbot/[repo].git

Fetch and checkout the remote branch. Run bundle to make sure any new dependencies are installed.

@harlow
harlow / factory_girl_example.rb
Created December 4, 2012 20:39
Factory Girl Cheat Sheet
# This will guess the User class
Factory.define :user do
first_name 'John'
last_name 'Doe'
admin false
end
# This will use the User class (Admin would have been guessed)
Factory.define :admin, :class => User do |u|
@harlow
harlow / rules.md
Created January 28, 2013 19:43 — forked from henrik/rules.md
  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.

You can break these rules if you can talk your pair into agreeing with you.

# app/models/customer.rb
class Customer
attr_reader :id, :country
def initialize(attrs = {})
@id = attrs[:id]
@country = attrs[:country]
end
def tax_code
@harlow
harlow / chat.rb
Created March 16, 2013 20:17 — forked from rkh/chat.rb
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@harlow
harlow / booking_payments_controller.rb
Last active December 15, 2015 17:09 — forked from jordelver/booking_payment_controller.rb
Use ActiveModel::Model for validations on non-AR objects
class BookingPaymentsController < ApplicationController
def create
booking_payment = BookingPayment.new(booking_payment_params)
booking_payment.pay
respond_with payment, location: balance_trip_path
end
private
@harlow
harlow / rspec_spies.rb
Created April 6, 2013 16:53
using spies with rspec-mocks
require 'spec_helper'
describe Importer, '#import' do
it 'creates a record for each row' do
contact = double(create: true)
csv_parser = double(rows: [:row1, :row2])
Importer.new(contact, csv_parser).import
expect(contact).to have_received(:create).with(:row1)
# ------------------------------------------------------------------
# Desigining "trending topics in 24 hours sliding window" with Redis
# ------------------------------------------------------------------
redis-cli del tophashes:2010-12-07-08-00
redis-cli del tophashes:2010-12-07-09-00
redis-cli del tophashes:current
echo '=== 8:00 AM ==='