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 / 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 / .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 / 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 / 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 / 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
class ImplementMongo < ActiveRecord::Migration
def change
create_table :mongo do |t|
t.string :key
t.text :value
end
end
end
1.9.3p327 :048 > t = User.new
=> #<User id: nil, name: nil, some_bool: nil, created_at: nil, updated_at: nil>
1.9.3p327 :049 > t.some_bool = 'a string'
=> "a string"
1.9.3p327 :050 > t.some_bool
=> false
@agraves
agraves / user_search.rb
Created February 11, 2013 03:05
UserSearch refactor
class UserSearch
def self.search(term, topic_id = nil)
scope = User.scoped
if topic_id
scope = scope.joins("
LEFT JOIN (
SELECT DISTINCT(posts.user_id)
FROM posts
WHERE topic_id = #{topic_id.to_i}
@agraves
agraves / gist:4772621
Created February 12, 2013 19:30
Trying to write platform-independent case-insensitive email search (without wildcards). Is this really the best way to do this?
email = '[email protected]'.gsub('%', '')
users = User.arel_table
User.find_by_sql(
users.
project(:id).
where(users[:email].matches email).
to_sql
).first
@agraves
agraves / gist:5086657
Last active December 14, 2015 12:28
Gemfile
ruby '1.9.3'
def local_group(*g, &block)
on_heroku = (ENV['HOME'].gsub('/','') == 'app')
if on_heroku
group(:test, &block)
else
group(*g, &block)
end