Skip to content

Instantly share code, notes, and snippets.

View thegrubbsian's full-sized avatar

JC Grubbs thegrubbsian

View GitHub Profile
@thegrubbsian
thegrubbsian / numeric_identity.rb
Created September 30, 2011 22:28
Mongoid Numeric Identity Module
module Mongoid
module NumericIdentity
module InstanceMethods
def generate_identity
id_attempt = (self.class.last.try(:id) || 0) + 1
dup_check = self.class.where(:id => id_attempt).first
if dup_check.nil?
self.id = id_attempt
@thegrubbsian
thegrubbsian / jquery.metadata.js
Created September 6, 2011 18:08
jQuery Table Plugins
/*
* Metadata - jQuery plugin for parsing metadata from elements
*
* Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Revision: $Id: jquery.metadata.js 3640 2007-10-11 18:34:38Z pmclanahan $
@thegrubbsian
thegrubbsian / backbone_event_proxy.js
Created July 30, 2011 15:56
Backbone.js Proxy Event Method
Backbone.View.prototype.proxy = function(source, eventName, options) {
if (!options) { options = {}; }
var self = this;
source.bind(eventName, function(args) {
var name = options["proxyName"] || eventName;
var newArgs = options["proxyArgs"] ? _.combine(args, options["proxyArgs"]) : args;
self.trigger(name, newArgs);
});
};
@thegrubbsian
thegrubbsian / backbone_sync_update.js
Created June 24, 2011 16:10
Adding an "update" event to Backbone.Collection instances when a model is saved
Backbone.Model.prototype.save = (function(original) {
return function(attrs, options) {
var me = this;
options = options || {};
var origSuccess = options.success;
var wasNew = this.isNew();
options.success = function(model, response) {
if (me.collection && !wasNew) { me.collection.trigger("update", me); }
if (origSuccess) { origSuccess(model, response); }
};
@thegrubbsian
thegrubbsian / jquery.validator.js
Created December 14, 2010 04:40
A very simple validation plugin for jQuery in just over 100 lines.
(function(jQuery) {
jQuery.fn.validator = function(options) {
var isEmpty = function(obj) { return obj == null || obj.length == 0; };
var opts = jQuery.extend(true, { // Allows for overriding any messages or validator functions
wrapperClass: "invalid", messageClass: "message", useMessages: true,
onValidation: null, onValidationFailed: null, // Callbacks which recieve an object containing the element and state { el: el, state: state }
validateOn: "blur", // The jQuery events that cause validation to occur
public class ValidateChildAttribute : ValidationAttribute {
public override bool IsValid(object value) {
if (value == null) return true;
var isValid = IsItValid(value);
if (!isValid) return false;
if (value is IEnumerable) return ((IEnumerable)value).Cast<object>().All(IsItValid);
return true;
}