Skip to content

Instantly share code, notes, and snippets.

IRB.conf[:AUTO_INDENT]=true
require 'rubygems'
require 'pp'
require 'what_methods'
require 'wirble'
Wirble.init
Wirble.colorize
# Easily print methods local to an object's class
Link: {
Toggle: Behavior.create({
initialize: function(toggleElement) {
this.addText = this.element.innerHTML;
this.cancelText = 'cancel';
this.toggleElement = $(toggleElement);
},
onclick: function(event) {
var element = event.element();
@szimek
szimek / validates_existence.rb
Created January 5, 2010 20:33
Shoulda macro for validates_existence gem
# Copied from http://github.com/shuber/validates_existence,
# modified to be compatible with the latest (2.10.2) version of shoulda
class Test::Unit::TestCase
def self.should_validate_existence_of(*associations)
allow_nil = get_options!(associations, :allow_nil)
associations.each do |association|
should "require #{association} exists" do
reflection = subject.class.reflect_on_association(association)
object = subject
-# Don't display new assets on form redisplay
- unless (form.object.new_record? && form.object.data.dirty?)
%li
= form.object.name if form.object.data?
= form.file_field :data if form.object.new_record?
= form.text_field :title
= remove_link(form, t('assets.actions.remove')).html_safe!
@szimek
szimek / campfire.rb
Created March 22, 2010 11:54 — forked from rtomayko/notify.rb
Alert Campfire of a Capistrano deploy
require 'httparty'
require 'json'
class Campfire
include HTTParty
base_uri 'http://your_domain.campfirenow.com'
basic_auth 'your_auth_token', 'whatever'
headers "Content-Type" => "application/json"
@szimek
szimek / i18n_expirable_cache.rb
Created April 5, 2010 14:17
Expirable cache module for i18n gem
module I18n
module Backend
# Expirable cache module that stores unpluralized and uninterpolated translations
module ExpirableCache
include Cache
protected
def fetch(*args}, &block)
locale, key, options = *args
options ||= {}
# Based on I18n::Backend::ActiveRecord::Missing, but provides some additional features:
# - creates Translation object for each locale defined in I18n.available_locales
# - looks up the translation using Simple backend and sets it as Translation#value
#
# Important! Remember to provide i18n.plural.keys in your YAML files. For example:
#
# # en.yml
# en:
# i18n:
# plural:
@szimek
szimek / create_locations.rb
Created June 20, 2010 18:58
How to setup PostGIS with Rails
class CreateLocations < ActiveRecord::Migration
def self.up
create_table :locations do |t|
t.datetime :time
t.point :geom, :null => false, :srid => 4326, :with_z => true
t.float :speed
end
end
def self.down
class ApplicationController < ActionController::Base
prepend_before_filter :blitzkrieg
def blitzkrieg
redirect_to "/berlin" and return if request.path == root_path
end
end
@szimek
szimek / paperclip_url_tempfile.rb
Created July 9, 2010 09:37
Paperclip extension for fetching images from URL
# Paperclip compliant class for uploading files through URL
# Simplified version of http://github.com/chris/paperclip_url_support
require "open-uri"
module Paperclip
class UrlTempfile < Tempfile
attr :content_type
def initialize(url)