Skip to content

Instantly share code, notes, and snippets.

# when I call Thing.count, it works and DISTINCT records are selected
# when I call Thing.scoped.count (or any other query), it doesn't work
class Thing < ActiveRecord::Base
class << self
def count_with_distinct
count_without_distinct(:distinct => true)
end
alias_method_chain :count, :distinct
end
@balexand
balexand / pages_spec.rb
Created July 24, 2011 01:30
Test both Ajax and Javascript-disabled version with the same test using RSpec, Capybara, and Selenium
describe "Pages" do
# a trivial example that follows a link from "Page A" to "Page B"
shared_examples "an example page" do
it "should follow link from Page A to Page B" do
visit page_a_path
click_link "Link to B"
page.should have_content("Page B")
current_path.should == page_b_path
end
@balexand
balexand / Screenshot with phantomjs
Created April 23, 2012 17:02
screenshot.coffee
# install phantomjs using Homebrew (http://mxcl.github.com/homebrew/)
# by running `brew install phantomjs` then run this script with `phantomjs screenshot.coffee`
page = require('webpage').create()
page.viewportSize = { width: 768, height: 600 }
page.open 'http://bridgidgallagher.com/blog', (status) ->
console.log status
window.setTimeout ->
console.log "render"
@balexand
balexand / xip_hack.rb
Created June 8, 2012 06:23
xip.io request hack
if Rails.env.development?
class XipIoMiddleware
def initialize(app)
@app = app
end
def call(env)
%w{HTTP_HOST SERVER_NAME SERVER_ADDR HTTP_X_FORWARDED_HOST}.each do |key|
env[key] = env[key].sub(/myapp\.\d+\.\d+\.\d+\.\d+\.xip\.io/, "myapp.dev") if env[key].present?
end
@balexand
balexand / iptables.sh
Last active December 11, 2015 18:59
iptables
#!/bin/bash
### BEGIN INIT INFO
# Provides: iptables
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Firewall
### END INIT INFO
@balexand
balexand / gist:4958397
Created February 15, 2013 03:34
create cropped animated gif using image magick convert command
convert -delay 200 -loop 0 -crop 200x200+0+0 +repage *.png animated.gif
@balexand
balexand / gem-reset.rb
Last active December 14, 2015 07:49 — forked from nixpulvis/gem-reset
#!/usr/bin/env ruby
# Remove all gems EXCEPT defaults :)
`gem list -d`.split(/\n\n^(?=\w)/).each do |data|
match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
name = match[:name]
versions = match[:versions].split(', ')
if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
next if match[1].empty? # it's the only version if this match is empty
@balexand
balexand / sentry.cpp
Created March 23, 2013 19:52
Makes an Arduino robot pace back and forth.
int MOTOR_A_BRAKE = 9;
int MOTOR_A_SPEED = 3;
int MOTOR_A_DIR = 12;
int MOTOR_B_BRAKE = 8;
int MOTOR_B_SPEED = 11;
int MOTOR_B_DIR = 13;
void setup() {
pinMode(MOTOR_A_SPEED, OUTPUT);
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazon.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>https://tracker-dvd.ufcfit-one.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
@balexand
balexand / gist:6767504
Last active February 21, 2016 17:41
Load data onto Garmin from Google Earth KML file using gpsbabel
# add the -t option for tracks
gpsbabel -t -i kml -f Telluride.kml -o garmin -F usb:
# To get Caltopo tracks to show up in Backcountry Navigator App, convert the KML using gpsbabel
gpsbabel -i kml -f from_cal_topo.kml -o gpx -F to_app.gpx