Skip to content

Instantly share code, notes, and snippets.

# This pattern avoids serializing a large object (the Task) to Redis, whilst
# avoiding passing a low-level data type (the id) into the constructor.
# You might choose to call TaskJob.new(task) directly elsewhere (e.g. tests)
class TaskJob
def self.perform(id)
new(Task.find(id)).perform
end
def initialize(task)
# This hack is designed to prevent a thread from implicitly checking out an AR connection by
# just accessing ActiveRecord::Base.connection. The point of doing this is to ensure that threads
# don't tie up connections that they are not using, by making sure we're explicit about where we
# need to use an AR connection.
#
# See also http://tenderlovemaking.com/2011/10/20/connection-management-in-activerecord.html
ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters::ConnectionPool # ensure loaded
(function() {
function m() {
n = location.protocol + "//" + location.host + "/";
r = t.ExtGuid;
i = t.Version;
s = t.PartnerId;
o = t.NameShort4;
c = t.AdServer;
u = t.AdCacheBuster;
f = t.cacheVersion || f;
_
/(|
( :
__\ \ _____
(____) `|
(____)| |
(____).__|
(___)__.|_____
SSt
class MyTestController < ApplicationController
def test_stuff
# bla
end
end
# Add to config/routes.rb...
#
# if Rails.env.test?
# map.connect '/:controller/:action/:id'
@jonleighton
jonleighton / output.txt
Created January 24, 2011 14:02
Rack test problem
$ ruby rack_proxy.rb
Loaded suite rack_proxy
Started
E
Finished in 0.004785 seconds.
1) Error:
test_foo(AppTest):
NoMethodError: undefined method `closed?' for nil:NilClass
/home/turnip/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/http.rb:1069:in `begin_transport'
From 42a20533837e925030b07132eaf0a2a6534ad235 Mon Sep 17 00:00:00 2001
From: Jon Leighton <j@jonathanleighton.com>
Date: Fri, 13 May 2011 20:03:57 +0100
Subject: [PATCH] Unfailing test case
---
.../has_many_through_associations_test.rb | 21 ++++++++++++++++++++
activerecord/test/schema/schema.rb | 11 ++++++++++
2 files changed, 32 insertions(+), 0 deletions(-)
@jonleighton
jonleighton / public_send_benchmark.rb
Created July 18, 2011 14:34
A benchmark of the speed of two options for backporting Ruby 1.9's public_send method to 1.8.7. The first one wins by far.
require 'benchmark'
class Object
def public_send_public_method_defined(method, *args, &block)
singleton_class = (class << self; self; end)
if singleton_class.public_method_defined?(method)
send(method, *args, &block)
else
raise # this doesn't matter, as it's not the path we're benchmarking
@jonleighton
jonleighton / results.txt
Created July 18, 2011 15:04
Proving to myself that it really is faster to avoid using send...
# 1.9.2
Rehearsal -------------------------------------------------------
no-send 1.950000 0.000000 1.950000 ( 1.961587)
symbol-send 2.190000 0.000000 2.190000 ( 2.192600)
string-send 4.790000 0.000000 4.790000 ( 4.812341)
---------------------------------------------- total: 8.930000sec
user system total real
no-send 2.330000 0.000000 2.330000 ( 2.332294)
require 'active_record'
require 'logger'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:user => 'root',
:database => 'test'
)