Skip to content

Instantly share code, notes, and snippets.

View gbuesing's full-sized avatar

Geoff Buesing gbuesing

View GitHub Profile

Declared ETags, together with Russian Doll caching, can be used to automatically mix your template and asset versions into the ETags set in your controllers. This avoids the need to blow all browser caches on each deploy and neatly contains the scope of "freshness fallout" when you tweak a view.

To include the template's version in the ETag:

  # Incorporate the cache version for this action into our ETag.
  # This allows template changes to bubble up into HTTP cache
  # freshness and bust browser caches when we make changes.
  etag do
    begin
@gbuesing
gbuesing / ad_hoc.rb
Created June 20, 2013 04:53
Rack::AdHoc
# Allows you to write one-off middleware directly in config.ru. Good for prototyping.
#
# Example:
#
# use Rack::AdHoc do |app, env|
# status, headers, body = app.call(env)
# headers['X-Foo'] = 'bar'
# [status, headers, body]
# end
#
# Middleware to tap into request so that you can provide an alternate response depending upon request conditions.
#
# Supplied block should return a Rack-compatible response, or nil/false to pass on handling of the request.
#
# Example:
#
# use Rack::Tap do |env|
# if Rack::Request.new(env).host == 'admin.myapp.com'
# [301, {'Location' => 'http://www.myapp.com/admin'}, []]
# end
@gbuesing
gbuesing / index.html
Last active December 13, 2015 20:18
Processing Test
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Processing Test</title>
<script type="text/javascript" src="http://cloud.github.com/downloads/processing-js/processing-js/processing-1.4.1.js"></script>
</head>
<body>
<canvas id="mysketch" data-processing-sources="sketch.pde"></canvas>
</body>
@gbuesing
gbuesing / gist:4532326
Created January 14, 2013 18:51
Helper to set HTTP Basic Auth in functional/controller tests
# Helper to set HTTP Basic Auth in functional/controller tests
# Put this in test/test_helper.rb:
class ActionController::TestCase
def login_via_http_basic_auth username, password
@request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(username, password)
end
end
module Positioned
extend ActiveSupport::Concern
included do
before_save :set_position, unless: :position
end
module ClassMethods
def positioned
order(:position, :id)
@gbuesing
gbuesing / superconsole.rb
Last active December 10, 2015 02:09
Rails commands multi-process demo
#!/usr/bin/env ruby
# Rails commands multi-process demo
#
# For use with https://github.com/rails/commands
#
# Put this file in an app in script/superconsole (or whatever name) and chmod +x
# or just put this in lib/superconsole.rb.
#
# This script opens the console in the development env, and forks a child process
require 'active_model'
class ActiveCouchModel
extend ActiveModel::Callbacks
include ActiveModel::Conversion
include ActiveModel::Naming
include ActiveModel::Validations
define_model_callbacks :save, :create
require 'date'
class Article < Hash
def initialize(hsh={})
replace(hsh)
end
def ==(other)
if other.is_a?(Date)
self[:date] == other
20_000 times
user system total real
Time.parse: 3.810000 0.020000 3.830000 ( 3.870988)
Time.parse_gm: 0.420000 0.190000 0.610000 ( 0.596905)
Time.parse_mktime: 0.520000 0.180000 0.700000 ( 0.707689)
Time.parse_maketime: 0.700000 0.190000 0.890000 ( 0.899610)
Time.parse_utc: 0.390000 0.180000 0.570000 ( 0.575244)
Time::mktime_with_offset: 0.760000 0.210000 0.970000 ( 0.965841)