Skip to content

Instantly share code, notes, and snippets.

View thegrubbsian's full-sized avatar

JC Grubbs thegrubbsian

View GitHub Profile
@thegrubbsian
thegrubbsian / backbone.events.extensions.js
Created September 8, 2012 04:10
Proxy or forward Backbone events through a mediator
(function() {
var proxy = function(source, eventName) {
var _self = this;
source.on(eventName, function(evt) {
var args = Array.prototype.slice.apply(arguments).splice(1);
args.unshift(evt);
_self.trigger.apply(_self, args);
});
};
@thegrubbsian
thegrubbsian / object.rb
Created August 28, 2012 15:41
Using Rails try() for chaining multiple methods
class Object
def try_all(*methods)
values = [self]
methods.each do |method|
value = values.last.try(method)
return nil if value.nil?
values << value
end
values.last
@thegrubbsian
thegrubbsian / .bash_profile
Created August 8, 2012 21:27
Branch Name in Bash Prompt
parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ →\ \1/'
}
export PS1='\u\[\e[1;37m\]@\[\e[1;32m\]\h\[\e[1;37m\]:\[\e[1;31m\]\W\[\e[1;33m\]$(parse_git_branch)\[\e[0;39m\]> '
export PROMPT_COMMAND='echo -ne "\033]0;${PWD}\007"'
@thegrubbsian
thegrubbsian / deploy.rake
Created June 13, 2012 17:22
Heroku Deploy Rake Task
namespace :deploy do
HEROKU_ACCOUNT = "account_name"
MAINLINE_BRANCH = "master"
STAGING_REPO = "app-name-staging"
PRODUCTION_REPO = "app-name-production"
def make_git_timestamp
"#{@env}-deploy-#{Time.now.to_s.gsub(/:/, "-").gsub(/\s/, "-").gsub(/--/, "-")}"
end
1.9.2-p290
@thegrubbsian
thegrubbsian / backbone_collection_create_child.js
Created March 18, 2012 19:09
Nested/Bound Collections in Backbone
// Trigger a modelChanged event on a model's collection if it has one
Backbone.Model.prototype.change = (function(original) {
return function(options) {
if (this.collection) {
this.collection.trigger("modelChange", this, options);
}
original.call(this, options);
};
})(Backbone.Model.prototype.change);
@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
@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 / 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
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