Skip to content

Instantly share code, notes, and snippets.

View rsutphin's full-sized avatar

Rhett Sutphin rsutphin

  • Human Practice, Inc.
  • Chicago, Illinois
View GitHub Profile
@rsutphin
rsutphin / exceed_query_limit.rb
Created July 7, 2014 14:40
RSpec 3 matcher for ActiveRecord query count limits. Based on http://stackoverflow.com/a/13423584/153896.
# Derived from http://stackoverflow.com/a/13423584/153896. Updated for RSpec 3.
RSpec::Matchers.define :exceed_query_limit do |expected|
supports_block_expectations
match do |block|
query_count(&block) > expected
end
failure_message_when_negated do |actual|
"Expected to run maximum #{expected} queries, got #{@counter.query_count}"
@rsutphin
rsutphin / clear_radio.html
Created March 14, 2013 00:18
Example of resetting an HTML radio button to unchecked using JavaScript.
<html>
<head>
<style type="text/css">
label { display: block; }
</style>
<body>
<form>
<label><input type="radio" name="q" id="radio-a" value="A"> A</label>
<label><input type="radio" name="q" id="radio-b" value="B"> B</label>
<label><input type="radio" name="q" id="radio-c" value="C"> C</label>
@rsutphin
rsutphin / tag_and_push_tag.cap
Last active April 18, 2018 13:08
A capistrano 3 task to tag the repo after every deploy
# Requires Capistrano 3.2 or later
namespace :deploy do
after :finishing, :tag_and_push_tag do
on roles(:app) do
within release_path do
set(:current_revision, capture(:cat, 'REVISION'))
# release path may be resolved already or not
resolved_release_path = capture(:pwd, "-P")
@rsutphin
rsutphin / repro
Last active December 30, 2015 12:49
Problem with spring and rails engine projects
#!/bin/bash -ex
gem list spring
gem list rails
rm -rf quux
rails plugin new quux --mountable
cd quux
@rsutphin
rsutphin / linode_dyndns.md
Created December 4, 2013 23:07
Use linode's DNS API for dynamic DNS with curl from cron

Use Linode's DNS Manager API for one-line dynamic DNS

This can be useful for cron, for instance.

Prereqs

  • You are using Linode's DNS Manager to manage a zone for your domain
  • You have already created an A record for the name you want to make dynamic
  • You have an API key for Linode's API
  • curl and jsonpp (jsonpp is not necessary but makes the manual steps easier)
@rsutphin
rsutphin / inline_representer.rb
Created August 6, 2013 14:54
Attempting to use an inline representer with Representable 1.6.0
require 'pp'
require 'representable/json'
class Album < Struct.new(:name, :songs); end
class Song < Struct.new(:title, :track); end
module AlbumPresenter
include Representable::JSON
property :name
@rsutphin
rsutphin / Gemfile
Created July 17, 2013 20:16
Bizarre Kernel.exec problem caused by "dropbox" gem
source 'https://rubygems.org'
gem 'dropbox'
@rsutphin
rsutphin / gist:5102133
Created March 6, 2013 19:14
Surprising each_with_object behavior
1.9.3p194 :001 > %w(a b c).each_with_object(['foo']) { nil }
=> ["foo"]
1.9.3p194 :002 > %w(a b c).each_with_object(['foo']) { break }
=> nil
@rsutphin
rsutphin / connection_method_available.rb
Created January 17, 2013 20:46
Demonstrates that version 1.2.5 of the jdbcpostgresql adapter does not load properly.
adapter_version = ARGV[0] || '1.2.5'
require 'rubygems'
require 'active_record'
gem 'activerecord-jdbcpostgresql-adapter', adapter_version
require 'active_record/connection_adapters/jdbcpostgresql_adapter'
if ActiveRecord::Base.respond_to?('jdbcpostgresql_connection')
puts "jdbcpostgresql_connection method available in #{adapter_version}"
@rsutphin
rsutphin / nokogiri-empty.rb
Created December 20, 2012 21:42
Demonstration for behavior difference between Nokogiri-C and Nokogiri-J for empty attributes in 1.5.6.
require 'rubygems'
gem 'nokogiri', ENV['NOKOGIRI_VERSION'] || '1.5.6'
require 'nokogiri'
n = Nokogiri::XML('<foo empty="" whitespace=" "/>')
puts " Ruby: #{RUBY_DESCRIPTION}"
puts "Nokogiri: #{Nokogiri::VERSION}"
puts
puts " Empty attribute value: #{n.root['empty'].inspect}"