Skip to content

Instantly share code, notes, and snippets.

View thegrubbsian's full-sized avatar

JC Grubbs thegrubbsian

View GitHub Profile
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;
}
@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
@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 / 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 / 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 / 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
task :deploy => ['deploy:push', 'deploy:restart', 'deploy:tag']
namespace :deploy do
task :migrations => [:push, :off, :migrate, :restart, :on, :tag]
task :rollback => [:off, :push_previous, :restart, :on]
task :push do
puts 'Deploying site to Heroku ...'
puts `git push heroku`
end
@thegrubbsian
thegrubbsian / require_benchmark.rb
Created November 30, 2011 21:04
Require Benchmark
require 'benchmark'
module Kernel
alias old_require require
def require(path)
output = nil
@required_files ||= []
benchmark = Benchmark.measure do
output = old_require path
@thegrubbsian
thegrubbsian / product.rb
Created February 11, 2012 19:00
QueryBuilder Example
class Product < ActiveRecord::Base
def self.find_by_criteria(params)
query = where("id IS NOT null") # A little odd but not sure how to get an ActiveRecord::Relation that returns "all"
query = query.where("price = ?", params[:price]) if params[:price].present?
query = query.where("manufacturer = ?", params[:manufacturer]) if params[:manufacturer].present?
query = query.where("retailer = ?", params[:retailer]) if params[:retailer].present?
query = query.where("category = ?", params[:category]) if params[:category].present?
query
end
@thegrubbsian
thegrubbsian / rails_engine_test.rb
Created March 18, 2012 19:07
blog: Emulate Rails auto loading for testing engines
include ActiveSupport
["app/**/", "lib/**/"].each do |glob|
Dir.glob(glob).each do |dir|
Dependencies.autoload_paths << File.join(File.dirname(__FILE__), dir)
end
end