Skip to content

Instantly share code, notes, and snippets.

Nick Larson ifightcrime

Block or report user

Report or block ifightcrime

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ifightcrime
ifightcrime / routes.rb
Created Jul 13, 2011 — forked from pixeltrix/routes.rb
Examples of advanced Rails 3.0 routes
View routes.rb
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+/
},
@ifightcrime
ifightcrime / gist:1126226
Created Aug 4, 2011
See if each object in an array is in another array of objects
View gist:1126226
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
@ifightcrime
ifightcrime / gist:1189032
Created Sep 2, 2011
JavaScript verify text inputs as the user types
View gist:1189032
// 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);
@ifightcrime
ifightcrime / gist:1251339
Created Sep 29, 2011
Delete all empty/false elements from hash recursively
View gist:1251339
# (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
@ifightcrime
ifightcrime / LICENSE.txt
Created Sep 29, 2011 — forked from p01/LICENSE.txt
Sudoku Solver in 140bytes
View LICENSE.txt
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
View gist:1262136
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
@ifightcrime
ifightcrime / _extend.js
Created Oct 26, 2011
for prettier javascript
View _extend.js
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];
}
}
}
@ifightcrime
ifightcrime / trim.js
Created Nov 28, 2011
trim whitespace
View trim.js
var trimmed = str.replace(/^\s+|\s+$/g, '');
@ifightcrime
ifightcrime / date_time_validator.rb
Created Jan 18, 2012
Validates DateTime values in a Rails app
View date_time_validator.rb
# 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"
@ifightcrime
ifightcrime / gist:1712614
Created Jan 31, 2012
Cross browser iframe scrollWidth/scrollHeight and resize iframe to content
View gist:1712614
// 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';
}
You can’t perform that action at this time.