Skip to content

Instantly share code, notes, and snippets.

@choonkeat
choonkeat / add-gitproj-as-rails-plugin
Created Nov 19, 2008
add-gitproj-as-rails-plugin
View add-gitproj-as-rails-plugin
#!/bin/bash
PROJECT=$1
GITPATH=$2
if test -z "$PROJECT" || test -z "$GITPATH"
then
echo Usage: $0 project gitpath
exit 1
fi
git remote add -f $PROJECT $GITPATH
@choonkeat
choonkeat / gist:32869
Created Dec 6, 2008
backup cron (backups of last 7 days, last month or any year)
View gist:32869
#!/bin/bash
#
# place this file in the directory you want to keep the backup
# mysqldumps. this script lets you retrieve dumps of last 7 days,
# last month or any year
#
BACKUP_DB=app_production
cd `dirname $0`
mysqldump $BACKUP_DB -u root > today.sql && \
@choonkeat
choonkeat / gist:43643
Created Jan 6, 2009
loop thru elements, show border, 0-padding, 0-margin to find layout-culprit in IE6
View gist:43643
var searchtext = $('.searchtext')[0];
function slow_hunt_for_ie(items, index) {
items = items || $('#ele, #ele *');
index = index || 0;
searchtext.value = index;
var max = index + 5;
for (; index < max && index < items.length; index++) {
$(items[index]).css({
border: '1px #000 solid',
margin: '0px',
View load_vendor-gems.rb
# Modified from http://errtheblog.com/posts/50-vendor-everything
Dir["#{File.join(File.dirname(__FILE__), '..')}/vendor/gems/**"].map do |dir|
require_paths_file = File.join(dir, ".require_paths")
paths = File.exists?(require_paths_file) ?
IO.read(require_paths_file).split(/[\r\n]+/).collect {|subdir| File.join(dir, subdir) } :
[(File.directory?(lib = "#{dir}/lib") ? lib : dir)]
$LOAD_PATH.unshift(*paths)
end
@choonkeat
choonkeat / gist:64557
Created Feb 15, 2009
a commonly needed server side component: expand tinyurl (et al)
View gist:64557
class BigurlsController < ApplicationController
def index
url = URI.parse(params[:url])
res = Net::HTTP.start(url.host, url.port) {|http| http.get([url.path, url.query].compact.join('?'), "User-Agent" => request.user_agent) }
render :text => (res['location'] || params[:url])
rescue
"#{params[:url]}##{$!.to_s}"
end
end
@choonkeat
choonkeat / gist:75025
Created Mar 6, 2009
irbrc config to make irb or script/console output be copy-paste friendly
View gist:75025
# Append this content into ~/.irbrc
#
# irb console prompt to make the output copy-paste friendly
# overrides :simple for use in Rails' script/console
# overrides :default for use in regular irb session
IRB.conf[:PROMPT][:SIMPLE] = IRB.conf[:PROMPT][:DEFAULT] = {
:PROMPT_I => "\"%N(%m):%03n:%i>\"; ", # normal prompt: an inconsequential statement, a string
:PROMPT_S => "", # prompt for continuing strings: nothing
:PROMPT_C => "", # prompt for continuing statement: nothing
:RETURN => "#=> %s\n" # return value output is commented to not disrupt the code flow
@choonkeat
choonkeat / gist:78819
Created Mar 13, 2009
This controller illustrates the problem of Rails sending "Set-Cookie" back to the client when cookie-store is being used -- even when session data has NOT changed
View gist:78819
# This controller illustrates the problem of Rails sending "Set-Cookie" back to the client
# when cookie-store is being used -- even when session data has NOT changed.
class HomeController < ApplicationController
def index
%w[one two three four five six].each do |word|
session[word] = word
end
render :text => "<a href='#{url_for(:action => 'show')}'>click here</a>"
end
View simple function to inline css into chunk of html string.rb
def simple_inline_css(html, *css_files)
doc = Hpricot(html)
css_files.each do |css_file|
css_content_without_comments = IO.read(css_file).gsub(/\/\*.*?\*\//m, '')
css_content_without_comments.split('}').each do |rule|
(match, properties) = rule.split('{')
next unless properties
match.gsub!(/^\s*|\s*$/, '')
properties.gsub!(/^\s*|\s*$/, '')
(doc/match).each do |ele|
@choonkeat
choonkeat / jQuery.highlight.js
Created Mar 30, 2009
yet another jquery yellow-fade
View jQuery.highlight.js
jQuery.fn.extend({
highlight: function(newColor, originalColor) {
newColor = 'yellow';
if (! originalColor) {
var coloredElem = this;
while (!originalColor && originalColor != 'transparent' && coloredElem) {
originalColor = coloredElem[0].originalColor || coloredElem.css('background-color');
coloredElem[0].originalColor = originalColor;
coloredElem = coloredElem.parent();
}
View facebox.shim.js
/* iframe shim for facebox */
(function($) {
$(document).bind('reveal.facebox', function(event) {
$('#facebox')[0].shim = $('<iframe src="about:blank" frameborder="0"></iframe>').css({
top: $('#facebox').offset().top + "px",
left: $('#facebox').offset().left + "px",
height: $('#facebox').height() + "px",
width: $('#facebox').width() + "px",
position: 'absolute'
});