Skip to content

Instantly share code, notes, and snippets.

View AMHOL's full-sized avatar

Andy Holland AMHOL

  • Manchester, UK
  • 08:55 (UTC)
View GitHub Profile
@AMHOL
AMHOL / Category drop-down
Created November 8, 2012 15:09
Create recursive category drop-down navigation by assigning data('children') to navigation li and calling elem.createDropdownFromChildren
(($) ->
$.fn.createDropdownFromChildren = ($level) ->
$elem = $(@)
# if element has children data
unless @data("children") is "undefined"
@bind
# MOUSE ENTER
mouseenter: ->
clearTimeout $(document).data("dd-timeout")
Date::DATE_FORMATS[:default] = '%d/%m/%Y'
# if you want to change the format of Time display then add the line below
Time::DATE_FORMATS[:default]= '%d/%m/%Y %H:%M:%S'
# if you want to change the DB date format.
Time::DATE_FORMATS[:db]= '%d/%m/%Y %H:%M:%S'
@AMHOL
AMHOL / jquery.placeholder-fallback.js
Created December 18, 2012 11:11
HTML5 placeholder fallback for browsers that don't support it, implemented in jQuery. NOTE: This will not work for elements added to the DOM after initial load
// Function to test for attribute support
function elSupportsAttr(el, attr) {
return attr in document.createElement(el);
}
// does input support placeholder? if not, add fallback
if ( !elSupportsAttr('input', 'placeholder') ) {
$(document).ready(function() {
// everything with placeholder attr
$('[placeholder]').each(function() {
$(this).val($(this).attr('placeholder'));
@AMHOL
AMHOL / gist:4633484
Created January 25, 2013 10:53
Javascript get host
window.location.protocol + '//' + window.location.hostname + (!!window.location.port ? ':' + window.location.port : '')
@AMHOL
AMHOL / gist:5327827
Created April 6, 2013 22:07
Library for building dependencies with ActiveResource
module ResourceHas
def self.included base
unless base.ancestors.include? InheritedResources::Base
raise '%s must inherit from InheritedResources::Base to implement ResourceHas' % base.name
end
base.extend(ClassMethods)
base.append_before_filter :__build_relations
end
## Class methods
module ClassMethods
@AMHOL
AMHOL / tricks.md
Last active December 17, 2015 04:49
Cool Ruby snippets

USEFUL TRICKS

Array#reduce

Array.reduce will inject and initial value into a block and pass in each value in turn until all values in the array has been iterated i.e

([1] * 10).reduce(0) { |total, current_value| total + current_value } #= 10
# Inject does the same
([1] * 10).inject(0) { |total, current_value| total + current_value } #= 10

However, if we simply require a sum of all values in the array, we can take advantage of Symbol#to_proc as follows:

@AMHOL
AMHOL / gist:5555467
Last active December 17, 2015 05:08
Windows phone console
if ( (typeof window.console == 'undefined' || typeof window.console.log != 'function') && !!window.location.href.match(/\?console=true/i) ) {
var ul = _createTag('ul', false, {
'line-height': '30px',
'background-color': '#fff',
'list-style-type': 'square',
color: '#000',
position: 'absolute',
top: 0,
right: 0,
left: 0,
@AMHOL
AMHOL / jquery.swipe.js
Created May 22, 2013 15:37
jQuery swipe library with support for android/iPad touch events and mouse events
(function($) {
'use strict';
// settings
$.fn.swipe = function(options) {
var args = Array.prototype.slice.call(arguments);
// bind using bind or on?
var bind = typeof $.fn.on == 'function' ? 'on' : 'bind';
var unbind = typeof $.fn.off == 'function' ? 'off' : 'unbind';
// events to bind
var events = {
@AMHOL
AMHOL / gist:5730159
Created June 7, 2013 15:34
jQuery rails-like methods for string manip
$.truncate = function(text, length) {
if ( length === undefined ) length = 255;
if ( text.length <= length ) return text;
return text.substring(0, length) + '...';
};
$.number_with_delimiter = function(number, delimiter) {
var part, new_number = [];
if ( delimiter === undefined ) delimiter = ',';
number = number.toString().split('').reverse();
@AMHOL
AMHOL / object.rb
Created July 11, 2013 08:20
Try chain in Ruby
class Object
def try_chain(*a)
a.inject(self){ |object, method| object.try(method.to_sym) }
end
end