Skip to content

Instantly share code, notes, and snippets.

Andy Holland AMHOL

Block or report user

Report or block AMHOL

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
@AMHOL
AMHOL / Category drop-down
Created Nov 8, 2012
Create recursive category drop-down navigation by assigning data('children') to navigation li and calling elem.createDropdownFromChildren
View Category drop-down
(($) ->
$.fn.createDropdownFromChildren = ($level) ->
$elem = $(@)
# if element has children data
unless @data("children") is "undefined"
@bind
# MOUSE ENTER
mouseenter: ->
clearTimeout $(document).data("dd-timeout")
View config initializers date_time_formats.rb
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 Dec 18, 2012
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
View jquery.placeholder-fallback.js
// 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 Jan 25, 2013
Javascript get host
View gist:4633484
window.location.protocol + '//' + window.location.hostname + (!!window.location.port ? ':' + window.location.port : '')
@AMHOL
AMHOL / gist:5327827
Created Apr 6, 2013
Library for building dependencies with ActiveResource
View gist:5327827
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 Dec 17, 2015
Cool Ruby snippets
View tricks.md

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 Dec 17, 2015
Windows phone console
View gist:5555467
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
jQuery swipe library with support for android/iPad touch events and mouse events
View jquery.swipe.js
(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 Jun 7, 2013
jQuery rails-like methods for string manip
View gist:5730159
$.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 Jul 11, 2013
Try chain in Ruby
View object.rb
class Object
def try_chain(*a)
a.inject(self){ |object, method| object.try(method.to_sym) }
end
end
You can’t perform that action at this time.