Skip to content

Instantly share code, notes, and snippets.

View mislav's full-sized avatar

Mislav Marohnić mislav

View GitHub Profile
@mislav
mislav / file.rb
Created December 1, 2008 16:32
Capistrano "push strategy", revisited
## Capistrano "push strategy", take 2
#
# It's based on "remote_cache", but doesn't rely on a central git repository.
# It assumes that the code is already pushed to its "cached-copy".
#
# Usage:
#
# git remote add origin example.com:/path/to/my-app/shared/cached-copy/.git
# cap deploy
#
@mislav
mislav / install-passenger.sh
Created December 6, 2008 17:02
Apache2, Passenger & Ruby Enterprise Edition
set -e
RE="ruby-enterprise-1.8.6-20090520"
PREFIX=/opt/$RE
VERSION=2.2.2
GEM_PATH="$PREFIX/lib/ruby/gems/1.8/gems/passenger-$VERSION"
DOMAIN=example.com
APP_ROOT=/u/apps/$DOMAIN
# install Apache2
@mislav
mislav / gist:58799
Created February 5, 2009 16:23 — forked from rails/gist:58761
Update contents of elements with relative times
document.observe("dom:loaded", function() {
function updateElementsWithTime() {
$$("span[time]").invoke('updateTime')
}
// update immediately
updateElementsWithTime()
// continue updating every 2 minutes
new PeriodicalExecuter(updateElementsWithTime, 60 * 2)
})
@mislav
mislav / gist:61530
Created February 10, 2009 18:52
advanced String#stripTags() with whitelisting
String.prototype.stripTags = String.prototype.stripTags.wrap(
function(proceed, allowTags) {
if (allowTags) {
if (Object.isString(allowTags)) allowTags = $w(allowTags)
this.gsub(/(<\/?\s*)([^\s>]+)(\s[^>]*)?>/, function(match) {
if (allowTags.include(match[2].toLowerCase()))
return match[1] + match[2] + match[3] + '>'
})
} else {
// proceed using the original function
@mislav
mislav / snippet.js
Created February 20, 2009 17:46
Build HTML for a tweet on Twitter.com from JSON payload
// *** JSON to HTML markup for a single update *** //
var buildUpdateFromJSON = (function() {
var updateContainer = $E('ol', { 'class': 'statuses' })
return function(data) {
var isReply = data.in_reply_to_screen_name,
date = new Date(data.created_at),
preparedData = {
id: data.id, reply_class: isReply ? 'reply' : '',
@mislav
mislav / gist:70895
Created February 26, 2009 15:06
Haml monkeypatch to strip newline characters between HTML tags
require 'haml/engine'
module Haml::Precompiler
alias original_flush_merged_text flush_merged_text
def flush_merged_text
@to_merge.each_with_index do |item, i|
next_item = @to_merge[i + 1]
item[1].chomp!("\n") if item[1][0, 1] == '<' and (!next_item or next_item[1][0, 1] == '<')
end
@mislav
mislav / gist:71574
Created February 27, 2009 17:06
serializing the form in Prototype with correct submit button
var currentSubmit = null
form.select('input[type=submit]').invoke('observe', 'click', function(e) {
currentSubmit = this.name
})
form.observe('submit', function(e) {
var params = this.serialize({ submit: currentSubmit })
console.log(Object.inspect(params))
})
@mislav
mislav / gist:71807
Created February 28, 2009 01:03
generic update checker method for scripts on userscripts.org
var checkUserscriptUpdate = (function(){
if (typeof GM_xmlhttpRequest != "function") return (function() {}) // no-op
var update = {
get available() { return getValue('updateAvailable', false) },
set available(value) { setValue('updateAvailable', value) },
get scriptLength() { return getValue('scriptLength') },
set scriptLength(value) { setValue('scriptLength', value) },
get checkedAt() { return getValue('updateTimestamp') },
set checkedAt(value) { setValue('updateTimestamp', value) },
@mislav
mislav / gist:76330
Created March 9, 2009 14:30
Cucumber steps that integrate with Rails named routes
When /^I go to(?: the)? (.*?)(?: page)?$/ do |name|
visit get_named_route(name)
end
Then /^I should be on(?: the)? (.*?)(?: page)?$/ do |name|
path.should == get_named_route(name)
end
## features/support/env.rb
ActionController::Integration::Session.class_eval do
@mislav
mislav / .rspactor
Created March 17, 2009 13:12
RSpec colorized unicode output with Rspactor integration
# this goes in the $HOME dir
# needs mislav-rspactor v0.3.2 and RSpec 1.2
RSpactor::Runner.class_eval do
alias old_formatter_opts formatter_opts
def formatter_opts
# update this path to where you saved unicode_formatter.rb
old_formatter_opts + " -r /Users/mislav/Projects/unicode_formatter -f UnicodeFormatter"
end
end