Skip to content

Instantly share code, notes, and snippets.

View alassek's full-sized avatar

Adam Lassek alassek

View GitHub Profile
# By Henrik Nyh <http://henrik.nyh.se> 2008-01-30.
# Free to modify and redistribute with credit.
require "rubygems"
require "hpricot"
module TextHelper
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
def truncate_html(text, max_length = 30, ellipsis = "...")
@alassek
alassek / typeIntent.js
Created September 14, 2011 22:13
Trigger change event without hitting enter, like Google Live Search
/*
* Copyright (c) 2011 Lyconic, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@alassek
alassek / store_ext.js
Created September 8, 2011 19:48
amplify.store.remove
amplify.store.remove = function ( key ) {
var value = amplify.store( key );
amplify.store( key, null );
return value;
}
@alassek
alassek / konami.js
Created April 28, 2011 07:39
konami code for jQuery
(function ($) {
$.fn.konami = function (callback, code) {
code = code || "38,38,40,40,37,39,37,39,66,65,13";
return this.each(function () {
var kkeys = [];
$(this).keydown(function (e) {
kkeys.push(e.keyCode);
if (kkeys.toString().indexOf(code) >= 0) {
$(this).unbind('keydown', arguments.callee);
callback(e);
@alassek
alassek / get_flash.rb
Created March 16, 2011 19:58
Get a flash message from a Rails Controller in a Sinatra action
def get_flash
signed_message = request.cookies['_project_name_session']
if signed_message.present?
secret = ProjectName::Application.config.secret_token
verifier = ActiveSupport::MessageVerifier.new(secret)
session = verifier.verify(signed_message)
@flash = session.delete('flash')
@alassek
alassek / load_erb.rb
Created February 18, 2011 22:41
support ERB in a YAML file
class << YAML
def load_erb(file)
load(
ERB.new(
IO.read(file)
).result
)
end
end
activesupport (3.0.3) lib/active_support/dependencies.rb:304:in `depend_on'
activesupport (3.0.3) lib/active_support/dependencies.rb:216:in `require_dependency'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:148:in `modules_for_helpers'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:144:in `map!'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:144:in `modules_for_helpers'
actionpack (3.0.3) lib/action_controller/metal/helpers.rb:101:in `modules_for_helpers'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:97:in `helper'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:161:in `default_helper_module!'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:24:in `inherited'
actionpack (3.0.3) lib/abstract_controller/helpers.rb:24:in `class_eval'
@alassek
alassek / bash_tricks.sh
Created November 22, 2010 22:58
Shell commands for complicated operations
# batch-rename files by extension less -> scss
for f in *less ; do mv $f `basename $f less`scss; done
/Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:22:in `mysql_connection': !!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2' (RuntimeError)
from /Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in `send'
from /Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in `new_connection'
from /Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `checkout_new_connection'
from /Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `checkout'
from /Users/adam/.rvm/gems/ruby-1.8.7-p299@rails3/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
function trim12 (str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}