Skip to content

Instantly share code, notes, and snippets.

View thegrubbsian's full-sized avatar

JC Grubbs thegrubbsian

View GitHub Profile
@thegrubbsian
thegrubbsian / iex.ex
Last active August 29, 2015 13:57
Weird API behavior on Process.register
iex(10)> Process.alive? tile_pid
true
iex(11)> Process.register tile_pid, :foo
** (ArgumentError) argument error
:erlang.register(:foo, #PID<0.163.0>)
(elixir) lib/process.ex:333: Process.register/2
@thegrubbsian
thegrubbsian / console.output
Created August 4, 2014 01:23
Phoenix compile issue
tasks> mix compile
Unchecked dependencies for environment dev:
* cowboy (package)
the dependency cowboy in mix.exs is overriding a child dependency:
> In mix.exs:
{:cowboy, "~> 1.0.0", [hex_app: :cowboy]}
> In deps/phoenix/mix.exs:
{:cowboy, "~> 0.10.0", [git: "git://github.com/extend/cowboy.git", optional: true]}
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 / 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