Skip to content

Instantly share code, notes, and snippets.

View agraves's full-sized avatar

Aaron Graves agraves

  • New York, NY
View GitHub Profile
@agraves
agraves / pages_controller_test.rb
Created October 9, 2012 21:13
Minitest controller test with clearance
require 'test_helper'
describe PagesController do
before{ sign_in }
subject{ PagesController.new }
%w{some_page another_page}.each do |page|
describe "rendering the #{page} page" do
it 'should render successfully' do
@agraves
agraves / test_helper.rb
Created October 4, 2012 15:49
Allow Minitest and Rspec to coexist
# Minitest and Rspec will need separate environments because both gems define 'describe'
# 'm' is a nice test runner gem. It lets you run 'm test/models/foo.rb:21'
# Gemfile
group :minitest do
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'factory_girl_rails'
gem 'm'
@agraves
agraves / Rakefile
Created October 1, 2012 17:21
Regain control over default Rake task from Rspec
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
require 'rake/testtask'
Web::Application.load_tasks
Rake::TestTask.new do |t|
@agraves
agraves / .vimrc
Created July 17, 2012 16:02
.vimrc
set tabstop=2
set shiftwidth=2
set expandtab
set ai
syntax on
set ruler
set smarttab
nnoremap <F7> :call TogglePasteMode()<CR>
inoremap <F7> <ESC><F7>
function TogglePasteMode ()
@agraves
agraves / date_ranges.sql
Created May 30, 2012 21:45
Date range overlaps
-- Calculate the number of days within a range overlapped by an arbitrary number of other ranges.
SELECT
COUNT(active_day),
action_id
FROM (
SELECT
DISTINCT(
GENERATE_SERIES(
start_at,
@agraves
agraves / foos_controller.rb
Created May 29, 2012 21:05
Controller with & without before filters
# With before filter
class FoosController < ApplicationController
before_filter :find_foo, :only => [:edit, :show, :update]
def create
end
def edit
end
@agraves
agraves / application_controller.rb
Created May 25, 2012 22:40
Profile a controller action
# ApplicationController
# Source: http://stackoverflow.com/questions/31320/how-to-profile-a-rails-controller-action
around_filter :profile if Rails.env.development?
def profile
if params[:profile] && result = RubyProf.profile { yield }
out = StringIO.new
RubyProf::GraphHtmlPrinter.new(result).print out, :min_percent => 0
self.response_body = out.string
@agraves
agraves / divio_test.py
Created April 20, 2012 21:56
optimized back-end for divio test
#!/usr/bin/python
def is_user_good_enough(username):
return False
@agraves
agraves / squelch_asset_pipeline.rb
Created April 16, 2012 18:13
Squelch Asset Pipeline
# config/initializers/squelch_asset_pipeline.rb
if Rails.env.development?
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
@agraves
agraves / example.rb
Created April 10, 2012 03:32
Blitz.io confirmation page
# Assuming you have high-voltage installed
# routes.rb
match 'long-blitz-string' => 'pages#forty_two'
# app/controllers/pages_controller.rb
def forty_two
'42'
end