Skip to content

Instantly share code, notes, and snippets.

@defunkt
defunkt / connection_fix.rb
Created November 19, 2009 20:00
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
require 'openssl'
class Encryptor
def initialize(key_pass, key_size = 1024, key_name = 'rsakey.sec', cipher = OpenSSL::Cipher.new('aes-256-cbc'))
@key_size = key_size
@cipher = cipher
@key_name = key_name
if( File.exists?(@key_name) )
require 'openssl'
require 'yaml'
class Encryptor
def initialize(key_pass, key_size = 2048, key_name = 'rsakey.sec', cipher = OpenSSL::Cipher.new('aes-256-cbc'))
@key_size = key_size
@cipher = cipher
@key_name = key_name
@kaelig
kaelig / gist:457517
Created June 29, 2010 17:25
Export csv avec sqlite
## Output csv from sqlite
$ sqlite3 db.sqlite
sqlite> .mode csv
sqlite> .header on
sqlite> .output db.csv
sqlite> .separator ,
sqlite> select * from table;
sqlite> .exit
@wincent
wincent / gist:659344
Created November 2, 2010 07:13
mass_assignment.rb
module RSpec
module Matchers
class AllowMassAssignmentOf # :nodoc:
def initialize hash = nil
raise if hash.nil?
raise unless hash.kind_of? Hash
raise unless hash.length > 0
@attributes = hash
end
@denmarkin
denmarkin / will_paginate_array_helper.rb
Created February 8, 2011 21:48
How to extend array to support .paginate with will_paginate
# See http://rubydoc.info/gems/will_paginate/2.3.15/WillPaginate/Collection.create
# See https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb
# Do in application_helper.rb or application_controller.rb (or somewhere else application-wide)
require 'will_paginate/collection'
Array.class_eval do
def paginate(options = {})
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options
WillPaginate::Collection.create(
options[:page] || 1,
@rke
rke / devise_patch.rb
Created July 15, 2011 01:49
email change notification for devise based on https://github.com/Mandaryn's solution
module Devise
module Models
# This just allows valid_password to be called if encrypted_password is blank.
# The code exists in devise-1.3 and later, this just backports it to out 1.2.1
module DatabaseAuthenticatable
def valid_password?(password)
return false if self.encrypted_password.blank?
bcrypt = ::BCrypt::Password.new(self.encrypted_password)
password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
Devise.secure_compare(password, self.encrypted_password)
@kaelig
kaelig / application.js
Created January 10, 2012 10:43
Open external links and PDFs in a new window or tab
$(document).ready(function() {
// Open external links in a new window or tab
$(document).on('click', 'a[rel$="external"]', function() {
$(this).attr('target', "_blank");
});
$(document).on('click', 'a[href$=".pdf"]', function() {
$(this).attr('target', "_blank");
});
// Open all urls that don't belong to our domain in a new window or tab
$(document).on('click', "a[href^='http:']:not([href*='" + window.location.host + "'])", function() {
@acwright
acwright / gist:1944639
Created February 29, 2012 21:40
Sinatra / ActionMailer / Sendgrid / Heroku
require 'sinatra'
require 'action_mailer'
class Mailer < ActionMailer::Base
def contact
mail(
:to => "test@example.com",
:from => "test@example.com",
:subject => "Test") do |format|
format.text
@erikh
erikh / hack.sh
Created March 31, 2012 07:02 — forked from DAddYE/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#