Skip to content

Instantly share code, notes, and snippets.

@redconfetti
redconfetti / announcer.js
Created June 11, 2015 22:38
Screen Reader Announcer
/**
* Inserts hidden announcement into page for screen readers
* Expects 'cc-visuallyhidden' class to hide element from visual users
**/
var screenReaderAnnounce = function(message) {
// remove existing announcer
var existingAnnouncer = $window.document.getElementById('cc-sr-announcer');
if (document.contains(existingAnnouncer)) {
existingAnnouncer.parentNode.removeChild(existingAnnouncer);
}
@redconfetti
redconfetti / ng-disabled-aria.html
Last active August 29, 2015 14:22
ngAria with ngDisabled receiving null and reporting as true
<!DOCTYPE html>
<html lang="en">
<head>
<title>Accessibility Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"></link>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-aria.js"></script>
</head>
<!--
@redconfetti
redconfetti / array_to_hash.rb
Last active January 20, 2017 00:49
Convert Array to Indexed Hash
# Often you'll have an array of hashes, and you find yourself needing to
# retrieve an object from that array by some unique attribute of the hash.
#
# Here is a way to convert an array to a hash that contains hashes indexed by the key value you specify.
# This is possible because Array uses the Enumerable module as a mixin
users = [
{'id' => 1, 'name' => 'Susan'},
{'id' => 2, 'name' => 'Pat'},
@redconfetti
redconfetti / my_custom_template.html
Created February 26, 2015 19:46
Creating a Template with Inputs via a Directive in AngularJS
(function(angular) {
'use strict';
angular.module('myapp.directives').directive('myCustomTemplate', function() {
return {
restrict: 'E',
templateUrl: 'shared/my_custom_template.html', // markup for template
scope: {
myList: '=', // Attribute used to pass an array of objects to render via the template: data-my-list="myList"
myConditional: '=', // Attribute used to specify some conditional state in template: data-my-conditional="false"
@redconfetti
redconfetti / hash_decorator.rb
Created October 21, 2014 16:59
Camelize Keys
// Taken from https://github.com/rails-api/active_model_serializers/issues/398#issuecomment-26072287
class Hash
def camelize_keys!
update_keys!(:camelize, :lower)
end
def underscore_keys!
update_keys!(:underscore)
end
@redconfetti
redconfetti / example.js
Created April 24, 2014 23:15
Javascript Asynchronous Loop with clearing
// do something
var doSomething = function() {
var myLink = $('a#ajax-link');
if (myLink.length > 0){
myLink.hide();
stopDoSomethingLoop();
}
};
// do something every 300 milliseconds
@redconfetti
redconfetti / example.rb
Created August 27, 2013 17:32
Custom Rspec matcher for checking class for constant, with optional class type.
class ExampleClass
PI = 3.14159265359
end

Relation between Javascript prototype and object

Example code demonstrating the relationship between the prototype and the objects instance values.

You can see that after explicitly setting the value of wheelCount to 4, it retains it's own value even after the prototype value is changed. The new prototype value of 18 applies to the new vehicle2 instance however.

Based on code from the post on "JavaScript constructors, prototypes, and the new keyword" by Pivotal Labs.

@redconfetti
redconfetti / definition.rb
Last active December 21, 2015 05:19
Creating multiple associated records when using FactoryGirl
FactoryGirl.define do
factory :definition do
title "definition title"
body "definition body"
cached_slug "definition-title"
page 1
created_at "2011-01-01 08:30:00"
updated_at "2011-01-01 08:35:00"
factory :definition_object_relations_theory do