Skip to content

Instantly share code, notes, and snippets.

View shaneriley's full-sized avatar

Shane Riley shaneriley

View GitHub Profile
@shaneriley
shaneriley / gist:1049756
Created June 27, 2011 20:30
iPhone/iPad/iPod Detector
$.is_iDevice = function() {
return $.inArray(navigator.platform, ["iPhone", "iPad", "iPod"]) !== -1;
};
@shaneriley
shaneriley / gist:1051316
Created June 28, 2011 14:58
Javascript String Interpolation
function interpolate(source, dictionary) {
return source.replace(/#{([^{}]*)}/g, function (a, b) {
var r = dictionary[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
// interpolate("#{first} #{last}", { first: "Shane", last: "Riley" });
@shaneriley
shaneriley / gist:1101324
Created July 23, 2011 11:27
datetime-local serialization
var $form = $("form"),
values = $form.serializeArray();
// jQuery doesn't currently support datetime-local inputs despite a
// comment by dmethvin stating the contrary:
// http://bugs.jquery.com/ticket/5667
// Manually storing input type until jQuery is patched
$form.find("input[type='datetime-local']").each(function() {
var $i = $(this);
values.push({ name: $i.attr("name"), value: $i.val() });
});
@shaneriley
shaneriley / gist:1157075
Created August 19, 2011 15:28
Haml2Html
require "hpricot"
require "open-uri"
Dir.glob('app/views/ui/*.html.haml').sort.each do |file|
route = file.gsub(/(\.html\.haml)|(app\/views)/, "")
filename = file.gsub(/(app\/views\/ui)|(\.haml)/, "")
unless route.match(/\/index/)
dom = Hpricot(open("http://localhost:3000#{route}"))
`mkdir static` unless File.directory?("static")
output = File.new("static#{filename}", "w")
@shaneriley
shaneriley / gist:1168143
Created August 24, 2011 14:01
toSentence
var toSentence = function() {
return Array.prototype.join.call(arguments, " ") + ".";
};
@shaneriley
shaneriley / gist:1178644
Created August 29, 2011 15:37
HTML nodeName Dictionary
var el_dictionary = {
a: "anchor",
h1: "heading",
h2: "heading",
h3: "heading",
h4: "heading",
h5: "heading",
h6: "heading",
li: "list item",
ul: "list container",
@shaneriley
shaneriley / gist:1250910
Created September 29, 2011 14:59
$.fn.hasEvent
$.fn.hasEvent = function(event, fn) {
if (!event) { return this; }
var has = {
event: false,
namespace: false,
handler: false
},
events = this.data("events"),
namespace = event.split(".");
event = namespace.shift();
@shaneriley
shaneriley / gist:1513808
Created December 23, 2011 10:21
Faking the time for tests' sake
// app/assets/env/test/time.js
window.__rails_test_time = Date();
// Or for you CS guys
@.__rails_test_time = Date()
In your JS that uses a cached Time.now-style date object, use this for assignment:
var now = window.__rails_test_time || Date();
Then conditionally include your test JS like so:
= javascript_include_tag "env/test/time" if Rails.env.test?
@shaneriley
shaneriley / gist:1854893
Created February 17, 2012 18:59
WTF CoffeeScript
# WHY RETURN STATEMENTS IN CONDITIONAL BLOCKS?????
# According to http://coffeescript.org/#conditionals the resulting code does NOT have a return statement.
$("dl.card :text").change ()->
$e = $(@)
val = $e.val()
odd = []
even = []
sum = 0
$.each(val.split(""), (i, v)->
# e should be defined as a local var. Instead, we get:
@shaneriley
shaneriley / gist:1877256
Created February 21, 2012 16:28
jQuery :data(#{selector}) expression
// Use:
// <input type="checkbox" data-accordion=".additional" />
// $(":data(accordion)").change(doSomething);
$.extend($.expr[":"], {
data: function(el, idx, selector) {
var attr = "data-" + selector[selector.length - 1];
return el.hasAttribute(attr);
}
});