Skip to content

Instantly share code, notes, and snippets.

Avatar

Rhett Sutphin rsutphin

View GitHub Profile
@rsutphin
rsutphin / exceed_query_limit.rb
Created Jul 7, 2014
RSpec 3 matcher for ActiveRecord query count limits. Based on http://stackoverflow.com/a/13423584/153896.
View exceed_query_limit.rb
# 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 Mar 14, 2013
Example of resetting an HTML radio button to unchecked using JavaScript.
View clear_radio.html
<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 Apr 18, 2018
A capistrano 3 task to tag the repo after every deploy
View tag_and_push_tag.cap
# 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 Dec 30, 2015
Problem with spring and rails engine projects
View repro
#!/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 Dec 4, 2013
Use linode's DNS API for dynamic DNS with curl from cron
View linode_dyndns.md

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 Aug 6, 2013
Attempting to use an inline representer with Representable 1.6.0
View inline_representer.rb
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 Jul 17, 2013
Bizarre Kernel.exec problem caused by "dropbox" gem
View Gemfile
source 'https://rubygems.org'
gem 'dropbox'
@rsutphin
rsutphin / gist:5102133
Created Mar 6, 2013
Surprising each_with_object behavior
View gist:5102133
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 Jan 17, 2013
Demonstrates that version 1.2.5 of the jdbcpostgresql adapter does not load properly.
View connection_method_available.rb
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 Dec 20, 2012
Demonstration for behavior difference between Nokogiri-C and Nokogiri-J for empty attributes in 1.5.6.
View nokogiri-empty.rb
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}"
You can’t perform that action at this time.