Skip to content

Instantly share code, notes, and snippets.

@mikezter
mikezter / remote_link_renderer.rb
Created May 29, 2009 09:37
AJAX link renderer for WillPaginate
@mikezter
mikezter / application.rb
Created November 16, 2009 12:06
GZip Compression using Rails
after_filter :compress
def compress
if self.request.env['HTTP_ACCEPT_ENCODING'] and self.request.env['HTTP_ACCEPT_ENCODING'].match(/gzip/)
if self.response.headers["Content-Transfer-Encoding"] != 'binary'
begin
ostream = StringIO.new
gz = Zlib::GzipWriter.new(ostream)
gz.write(self.response.body)
self.response.body = ostream.string
self.response.headers['Content-Encoding'] = 'gzip'
@mikezter
mikezter / template_identification.rb
Created January 20, 2010 11:52
Add Template Identification HTML Comments to ERB output in Rails
# Add Template Identification HTML Comments to ERB output
module ::ActionView
module TemplateHandlers
class ERB < TemplateHandler
include Compilable
cattr_accessor :erb_trim_mode
self.erb_trim_mode = '-'
def compile(template)
buf = <<-HTML
<% unless request and request.xhr? %>
@mikezter
mikezter / standard_brief_b.rb
Created February 22, 2010 17:05
Briefblatt nach DIN 676 - Formblatt B
require 'prawn/measurement_extensions.rb'
class StandardBriefB < Prawn::Document
def initialize(*args)
super(
:page_size => 'A4',
:page_layout => :portrait,
:margin => [107.4.mm, 8.1.mm, 30.mm, 24.1.mm]
)
options = args.last.is_a?(Hash) ? args.last : {}
@mikezter
mikezter / update_single_attribute.rb
Created February 22, 2010 17:40
update a single attribute on dirty ActiveRecords
module ActiveRecord
class Base
# updates a single attribute on the record directly to the database using
# ActiveRecord::Base.update_all
def update_single_attribute(name, value)
write_attribute(name, value)
self.class.update_all({name => value}, {self.class.primary_key => read_attribute(self.class.primary_key)}, {:limit => 1})
end
end
end
@mikezter
mikezter / gist:403561
Created May 17, 2010 09:12 — forked from tpitale/gist:162954
mongodb init script
#! /bin/sh
### BEGIN INIT INFO
# Provides: mongodb
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the mongodb data-store
# Description: starts mongodb using start-stop-daemon
@mikezter
mikezter / .bashrc
Created August 13, 2010 10:33
my .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# ANSI color codes
@mikezter
mikezter / caller_method_name.rb
Created August 20, 2010 11:44
Get calling method name in Ruby
module CallerMethodName
extend self
def caller_method_name
parse_caller(caller(2).first).last
end
def parse_caller(at)
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
file = Regexp.last_match[1]
line = Regexp.last_match[2].to_i
@mikezter
mikezter / dynamic_attributes.rb
Created August 31, 2010 09:00
Allow an ActiveRecord to behave like a schemaless document
module DynamicAttributes
class DynamicAttributesError < StandardError; end;
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end
module ClassMethods
@mikezter
mikezter / r_open_struct.rb
Created September 3, 2010 17:47
Recursive OpenStruct
require 'ostruct'
class ROpenStruct < OpenStruct
def table
@table ||= {}
end
def method_missing(mid, *args)
mname = mid.id2name
len = args.length