Skip to content

Instantly share code, notes, and snippets.

View longlostnick's full-sized avatar

Nick Larson longlostnick

View GitHub Profile
@longlostnick
longlostnick / routes.rb
Created July 13, 2011 18:03 — forked from pixeltrix/routes.rb
Examples of advanced Rails 3.0 routes
Rails.application.routes.draw do
get '/(:locale)/products/(:category)/(page/:page).:extension',
:to => 'products#index',
:as => :products,
:constraints => {
:locale => /[a-z]{2}/,
:category => /.+?/,
:page => /\d+/
},
@longlostnick
longlostnick / gist:1126226
Created August 4, 2011 20:46
See if each object in an array is in another array of objects
my_objects.each do |my_object|
if my_other_objects.any?{|my_other_object| my_other_object.Name == my_object.name}
# do something if we found one
end
end
@longlostnick
longlostnick / gist:1189032
Created September 2, 2011 16:10
JavaScript verify text inputs as the user types
// used to verify characters and prevent incorrect input as the user is typing.
// TODO: detect special characters like period/comma/?/etc. in the first regexp
// USAGE: use the keydown event: "return dynamicVerify(e, '[0-9a-zA-Z]');"
// e = event, re = regexp for allowed characters, ce = regexp for allowed key codes
function dynamicVerify(e, re, ke) {
var key = e.keyCode || e.which;
var keychar = String.fromCharCode(key);
var rexp = new RegExp(re);
var kexp = new RegExp(ke);
@longlostnick
longlostnick / gist:1251339
Created September 29, 2011 17:24
Delete all empty/false elements from hash recursively
# (v.respond_to?(:empty?) ? v.empty? : !v) is basically rails' .blank? in plain ruby
class Hash
def delete_blank
delete_if do |k, v|
(v.respond_to?(:empty?) ? v.empty? : !v) or v.instance_of?(Hash) && v.delete_blank.empty?
end
end
end
@longlostnick
longlostnick / LICENSE.txt
Created September 29, 2011 22:38 — forked from p01/LICENSE.txt
Sudoku Solver in 140bytes
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mathieu 'p01' Henri <http://www.p01.org/releases/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@longlostnick
longlostnick / gist:1262136
Created October 4, 2011 16:44
Ruby Array#bsearch
class Array
def bsearch(e, l = 0, u = length - 1)
return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:bsearch(e,l,u)
end
end
@longlostnick
longlostnick / _extend.js
Created October 26, 2011 23:19
for prettier javascript
function _extend(obj) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; i++) {
var source = sources[i];
for (var prop in source) {
obj[prop] = source[prop];
}
}
}
@longlostnick
longlostnick / trim.js
Created November 28, 2011 19:44
trim whitespace
var trimmed = str.replace(/^\s+|\s+$/g, '');
@longlostnick
longlostnick / date_time_validator.rb
Created January 18, 2012 03:50
Validates DateTime values in a Rails app
# this sucker takes care of validating datetime fields before rails gets there and
# messes everything up. it should preserve the local time zone from the user input,
# and check for nil. takes date strings of the format m/d/yyyy m:h (am/pm)
# this goes in the model
validates_with DateTimeValidator, :fields => [:add_date]
# this goes in some place like lib/date_time_validator.rb
class DateTimeValidator < ActiveModel::Validator
DATETIME_FORMAT = "%m/%d/%Y %I:%M %P"
@longlostnick
longlostnick / gist:1712614
Created January 31, 2012 20:08
Cross browser iframe scrollWidth/scrollHeight and resize iframe to content
// Works for me in IE7+, Webkit, and FF
function resizeToContent(frame_id) {
var my_frame = document.getElementById(frame_id);
var content_width = my_frame.contentWindow.document.documentElement.scrollWidth;
var content_height = my_frame.contentWindow.document.documentElement.scrollHeight;
my_frame.style.width = content_width + 'px';
my_frame.style.height = content_height + 'px';
}