Skip to content

Instantly share code, notes, and snippets.

pool = FeaturePool.find_by_name('discussions/primary')
position = 1
previous = pool.feature_appearances.all(:conditions => 'featured_at IS NOT NULL', :order => 'featured_at')
previous.each do |appearance|
appearance.update_attribute(:position, position)
position += 1
end
pool.update_attribute(:head, previous_and_current.last(pool.rotate_count).to_a.last.position + 1)
@dandorman
dandorman / gist:934697
Created April 21, 2011 14:57
SSH Tunneling in Ruby by John Nunemaker
# from http://railstips.org/blog/archives/2011/04/19/ssh-tunneling-in-ruby/
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new('<myremotehostorip.com>', '<remote_user>')
# Open port 27018 to forward to 127.0.0.1:27017
# on the remote host provided above
gateway.open('127.0.0.1', 27017, 27018)
@dandorman
dandorman / fabrication_feature_pool_spec.rb
Created August 9, 2011 23:00
FactoryGirl vs. Fabrication
require 'spec_helper'
describe FeaturePool do
it "creates a new instance given valid attributes" do
Fabricate :feature_pool
end
it "is not valid without a name" do
Fabricate.build(:feature_pool, :name => "").should_not be_valid
end
@dandorman
dandorman / empty_array_minitest.rb
Created December 12, 2011 03:22
EmptyArray: an array variant of the null object pattern
require 'minitest/autorun'
class EmptyArray
def self.new
@instance ||= super
end
private :initialize
def method_missing(*args, &block)
require 'erb'
def interpolate(template, values = {})
template.gsub(/\{\{(.+?)\}\}/) { |token| values.fetch($1) { ERB.new("<%= #{token} %>").result(binding) } }
end
@dandorman
dandorman / pre-commit
Created March 18, 2013 22:38
A pre-commit hook for compiling health-tracking JS.
#!/bin/sh
#
# Compile health-tracking javascript if necessary.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@dandorman
dandorman / route.js
Last active August 29, 2015 14:01 — forked from ryanflorence/route.js
var Route = React.createClass({ /* magic */ }); // not necessary, handled my Routed's magic
var Link = React.createClass({ /* magic */ });
var Routed = { /* magic */ };
var App = React.createClass({
mixins: [Routed],
render: function() {
return (
<Root path="/">
@dandorman
dandorman / blurgh.rb
Created March 13, 2015 22:29
dat constant lookup tho
BLURGH = "outer blurgh"
module Foo
BLURGH = "foo blurgh"
class Bar
def blurgh
BLURGH
end
end
@dandorman
dandorman / map_into.rb
Created March 13, 2015 23:27
A fun li'l Ruby experiment.
module Enumerable
def map_into(callable, *args)
map { |value|
value, args = yield(value, *args) if block_given?
callable.call value, *args
}
end
end
require "uri"
@dandorman
dandorman / SassMeister-input.scss
Created April 16, 2015 15:42
Generated by SassMeister.com.
// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
.btn {
font-weight: bold;
&:focus {
font-style: italic;
}