Skip to content

Instantly share code, notes, and snippets.

@outoftime
outoftime / collection.rb
Created February 11, 2011 15:02
Add NewRelic execution traces to Mongoid
if defined? ::NewRelic
module Mongoid
class Collection
(%w(find find_one map_reduce) + Collections::Operations::PROXIED - ['<<']).uniq.each do |method|
add_method_tracer method, "MongoDB/\#{@klass}##{method}"
end
end
end
end
@outoftime
outoftime / controller.rb
Created February 12, 2011 01:31
Mixins for simple user authentication
module Authenticated
module Controller
extend ActiveSupport::Concern
included do
helper_method :current_user, :current_user?, :no_current_user?
end
module ClassMethods
def require_login(options = {})
@outoftime
outoftime / config.rb
Created March 28, 2011 14:31
Support for Heroku pgbackups in Backup library
require './lib/backup/database/heroku_pgbackups.rb'
Backup::Model.new(:heroku, 'Heroku hosted data') do
database Backup::Database::HerokuPgbackups do |db|
db.name = 'my-heroku-app-name'
end
# Followed by other databases, storage, compression, etc.
end
@outoftime
outoftime / runtime_callback.rb
Created April 5, 2011 17:47
Add support for OmniAuth callbacks defined at runtime
#
# Including this module in an OmniAuth strategy implementation allows it to have
# a runtime-defined callback path extension. The callback path always begins
# with the predefined callback path, but may extend it. Specify a callback path
# extension at runtime by passing the 'callback' parameter to the request-phase
# auth URL. For example, the below URL:
#
# http://your.host/auth/facebook?callback=users/create
#
# Will issue a callback to:
@outoftime
outoftime / after_commit_with_transactional_fixtures.rb
Created April 20, 2011 15:22
Patch ActiveRecord to fire after_commit callbacks at the appropriate time during tests with transactional fixtures.
module ActiveRecord
module ConnectionAdapters
module DatabaseStatements
#
# Run the normal transaction method; when it's done, check to see if there
# is exactly one open transaction. If so, that's the transactional
# fixtures transaction; from the model's standpoint, the completed
# transaction is the real deal. Send commit callbacks to models.
#
# If the transaction block raises a Rollback, we need to know, so we don't
@outoftime
outoftime / unobservant.rb
Created July 25, 2011 11:01
Disable observers by default in tests (inspired by No Peeping Toms)
module Unobservant
mattr_accessor :enabled_observers
mattr_accessor :all_observers_enabled
self.enabled_observers = Set[]
self.all_observers_enabled = true
class <<self
def enable_all
self.all_observers_enabled = true
@outoftime
outoftime / top_children.sh
Created February 16, 2012 16:23
Reduction: top_children query returns no results when child field has the same name as a nested field as a nested field in the parent mapping
curl -XDELETE localhost:9200/test?pretty=true
echo ""
curl -XPUT localhost:9200/test?pretty=true
echo ""
curl -XPUT localhost:9200/test/parent/_mapping?pretty=true -d '{"parent":{"properties":{"name":{"type":"string","index":"analyzed"},"notes":{"properties":{"body":{"type":"string"}}}}}}'
echo ""
curl -XPUT localhost:9200/test/child/_mapping?pretty=true -d '{"child":{"_parent":{"type":"parent"}, "properties":{"body":{"type":"string","index":"analyzed"}}}}'
echo ""
curl -XPUT localhost:9200/test/parent/1?pretty=true -d "{\"name\":\"parent document\"}"
echo ""
@outoftime
outoftime / remap-readonly.sh
Created March 6, 2012 17:00
Reduction: error when attempting to re-map alias that points to read-only index
curl -XDELETE http://localhost:9200/test1?pretty=true
echo ''
curl -XDELETE http://localhost:9200/test2?pretty=true
echo ''
curl -XPUT http://localhost:9200/test1?pretty=true -d '{}'
echo ''
curl -XPOST http://localhost:9200/_aliases?pretty=true -d '{"actions":[{"add":{"index":"test1","alias":"test"}}]}'
echo ''
curl -XPUT http://localhost:9200/test2?pretty=true -d '{}'
echo ''
@outoftime
outoftime / analyzer-error.sh
Created March 8, 2012 15:38
Reduction for error on _analyze endpoint when routed to a specific index
pkill -f $(pwd)
for i in {0..2}
do
bin/elasticsearch
done
echo -n "waiting for cluster to come up"
until curl -s localhost:9200/_cluster/health?wait_for_status=green > /dev/null
do
echo -n '.'
sleep 1
@outoftime
outoftime / xscreensaver-xchat.rb
Created July 18, 2012 14:34
Set away status in xchat on screensaver events
#!/usr/bin/env ruby
IO.popen('/usr/bin/xscreensaver-command -watch') do |io|
while line = io.gets
case line
when /^BLANK\b/, /^LOCK\b/
system('xchat', '-e', '--command=away')
when /^UNBLANK\b/
system('xchat', '-e', '--command=back')
end