Skip to content

Instantly share code, notes, and snippets.

@calas
calas / firefox_emacs_handler.rb
Created September 11, 2009 09:51 — forked from diasjorge/FirefoxEmacsHandler
Firefox emacs handler for textmate txmt:// protocol
#!/usr/bin/env ruby
# To install:
# about:config
# create a new boolean network.protocol-handler.external.txmt with value true
# create a new string network.protocol-handler.app.txmt with value path to the script
require 'rubygems'
require 'cgi'
require 'uri'
@jcf
jcf / Moving ActiveRecord Errors.rb
Created September 17, 2009 10:52
Copy errors on an ActiveRecord object in to another instance's errors
# cat, dog = Cat.new, Dog.new
# cat.friends << dog
# cat.save
# => false
# cat.errors
# #<ActiveRecord::Errors ... @errors={"dog" => "is not valid"}>
# cat.errors << dog.errors
# cat.errors
# #<ActiveRecord::Errors ... @errors={"dog" => "is not valid", "bark" => "is not bigger than bite"}
@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'
@fnichol
fnichol / README.md
Created March 12, 2011 20:52
Download a cacert.pem for RailsInstaller

Why?

There is a long standing issue in Ruby where the net/http library by default does not check the validity of an SSL certificate during a TLS handshake. Rather than deal with the underlying problem (a missing certificate authority, a self-signed certificate, etc.) one tends to see bad hacks everywhere. This can lead to problems down the road.

From what I can see the OpenSSL library that Rails Installer delivers has no certificate authorities defined. So, let's go fetch some from the curl website. And since this is for ruby, why don't we download and install the file with a ruby script?

Installation

The Ruby Way! (Fun)

@postpostmodern
postpostmodern / rails_bootstrap_delete_confirmation_modal.md
Created February 19, 2012 07:38 — forked from trey/rails_bootstrap_delete_confirmation_modal.md
A nice delete confirmation modal in Rails courtesy of Bootstrap

Here's what you get.

Some CoffeeScript (verbosely commented for clarity)

# Override Rails handling of confirmation

$.rails.allowAction = (element) ->
  # The message is something like "Are you sure?"
  message = element.data('confirm')
@willurd
willurd / web-servers.md
Last active July 6, 2024 23:56
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@MrFlick
MrFlick / table.shingle.R
Last active August 29, 2015 13:58
table.shingle.R: allows for creating tables with shingles (lattice) and factors
table.shingle<-function(..., as.data.frame=F) {
dots<-list(...)
if(is.logical(as.data.frame) && as.data.frame) {
as.data.frame <- list(collapse=T)
}
stopifnot(all(sapply(dots, class) %in% c("shingle","factor")))
stopifnot(length(unique(sapply(dots, length)))==1)
if(is.list(as.data.frame) && !as.data.frame$collapse) {
for(i in which(sapply(dots, class)=="shingle")) {
pts<-unique(sort(unlist(levels(dots[[i]]))))
@Envek
Envek / pg_interval_support_4_1.rb
Last active December 18, 2023 14:41
Enables PostgreSQL interval datatype support (as ActiveSupport::Duration) in Ruby on Rails from 4.1 to 6.0
# Enables PostgreSQL interval datatype support (as ActiveSupport::Duration) in Ruby on Rails 4.1.
# Based on https://gist.github.com/clarkdave/6529610
require 'active_support/duration'
# add a native DB type of :interval
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:interval] = { name: 'interval' }
# add the interval type to the simplified_type list. because this method is a case statement
# we can't inject anything into it, so we create an alias around it so calls to it will call
@holly
holly / ddnsmasq
Last active May 1, 2019 00:20
update dnsmasq host file from dhcp lease file
#!/usr/bin/env python3
from argparse import ArgumentParser
from subprocess import Popen, PIPE, STDOUT
import shlex
import hashlib
import time
import warnings
import os, sys, io
import signal
@vollnhals
vollnhals / pg_interval_rails_5_1.rb
Last active July 13, 2020 13:44
Implementation of interval database type in Rails 5.1
# implementation from https://github.com/rails/rails/pull/16919
# activerecord/lib/active_record/connection_adapters/postgresql/oid/interval.rb
require "active_support/duration"
module ActiveRecord
module ConnectionAdapters
module PostgreSQL