Skip to content

Instantly share code, notes, and snippets.

@jkappers
jkappers / css.css
Last active October 19, 2015 11:07
Micro Optimized CSS for Phark an Gilder/Levin Image Replacement
/* `Utilities
/******************************/
/* Gilder/Levin & Phark image replacement */
a.ir, a.phark { cursor:pointer; text-decoration:none }
a.ir:active, a.phark:active { outline:0 }
.ir, .phark { display:block; position:relative; overflow:hidden }
.ir span { display:block; height:100%; width:100%; position:absolute; top:0; left:0; background:no-repeat 0 0 }
.phark{text-indent:-999em}
@jkappers
jkappers / src.js
Created August 30, 2012 16:13
Quick alpha sorting for content
$.fn.alphabetize = function(options){
var $list = this,
$item = this.children();
options = options || {};
$item.sort(function(a,b){
var x = (options.on && $(a).find(options.on) || $(a)).text(),
y = (options.on && $(b).find(options.on) || $(b)).text();
@jkappers
jkappers / src.js
Created August 30, 2012 18:44
Attach instances of javascript classes to DOM nodes
(function(exports){
// When the DOM is ready...
$(function(){
// ...Find any elements that have a data-view attribute
$('[data-view]').each(function(){
// Store a reference to the node and the data-view attribute value.
var $node = $(this), attr = $node.data('view');
// Create an instance of a class with a name that matches the value of attr,
// pass the node as a parameter to the constructor, and then store the instance
// in the node's data hash.
@jkappers
jkappers / src.js
Created October 15, 2012 15:06
Force line wrapping in SVG text tags
(function() {
var svgns = "http://www.w3.org/2000/svg";
function forceTextWrappingOn(node, width) {
console.log(node);
var chars = node.firstChild.nodeValue.split(' '),
x = parseInt(node.getAttribute('x'), 10),
y = parseInt(node.getAttribute('y'), 10),
index = 0,
tspan, tspanWidth, textNode
@jkappers
jkappers / pluginize.js
Last active December 17, 2015 22:29
Convert javascript class to jQuery plugin.
var camelize = function(str) {
return str[0].toLowerCase() + str.replace(/-([a-z])/g, function(a, b) { return b.toUpperCase(); }).slice(1);
}
var pluginize = function(classname, context) {
var camelized = camelize(classname)
$.fn[camelized] = function(config){
if (!this.length) return;
return this.each(function(){
var $element = $(this)
select.form-control + .chosen-container.chosen-container-single .chosen-single {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.428571429;
color: #555;
vertical-align: middle;
background-color: #fff;
@jkappers
jkappers / application_controller.rb
Last active January 3, 2016 06:19
Easily allow additional parameters for :sign_up, :invite, :accept_invite etc.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def devise_parameter_sanitizer
return super unless resource_class == User
Sanitizers::User.new(User, :user, params)
end
end
@jkappers
jkappers / file.sql
Created March 4, 2014 20:01
Get table sizes in sql
select table_name,
round((data_length + index_length)/1024/1024/1024, 2) as `gb`
from information_schema.tables
where table_schema = "schema"
order by `gb` desc;
@jkappers
jkappers / data.rake
Created May 27, 2014 22:56
Simple rake task for obfuscating data in a small rails app.
class Scrubber
VALUE_PROVIDERS = {
"name" => Proc.new { Faker::Name.name },
"email" => Proc.new { Faker::Internet.email },
"ssn" => Proc.new { rand.to_s[2..10] }
}
def scrub(*classes)
classes.each do |klass|
klass.all.each { |model| fake(model) }
@jkappers
jkappers / model.js
Last active August 29, 2015 14:09
Organizing AJAX calls by using a very simple inheritance structure and jQuery extend.
function Model(){}
// Converts a hash or each hash of an array into an instance of the calling class.
Model.fromObject = function(obj){
if (obj && !$.isArray(obj)){
return $.extend(new this(), obj);
}
var collection = [];
for(var index in obj){ collection.push(this.fromObject(obj[index])); }