Skip to content

Instantly share code, notes, and snippets.

View maxjustus's full-sized avatar

Max Justus Spransy maxjustus

  • People With Jetpacks
  • Los Angeles, CA
View GitHub Profile
@maxjustus
maxjustus / gist:2257305
Created March 30, 2012 23:02
dynamic insertion into partition based on value in insert for Postgres
create or replace function insert_herp_to_derp()
returns trigger as $$
begin
execute 'INSERT INTO ' || quote_ident('derp_' || NEW.derp_id || '_herps') || ' SELECT ($1).*' using NEW;
return null;
end;
$$ LANGUAGE plpgsql;
CREATE TRIGGER insert_herps_trigger BEFORE INSERT ON herps FOR EACH ROW EXECUTE PROCEDURE insert_herp_to_derp();
@maxjustus
maxjustus / gist:2257922
Created March 30, 2012 23:37
Insert to correct Postgres partition dynamically using trigger, create partition automatically if not found.
create or replace function insert_herp_to_derp()
returns trigger as $body$
declare
name text := 'derp_' || NEW.derp_id || '_herps';
s text;
begin
-- check that the needed table exists on the database
perform 1
from pg_class, pg_namespace
where relnamespace = pg_namespace.oid
@maxjustus
maxjustus / sysctl.conf
Created April 4, 2012 14:53
This allows you to conf postgres to accept 100 max connections on OSX (stored in /etc)
kern.sysv.shmmax=1073741824
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.sysv.shmall=4096
@maxjustus
maxjustus / .tmux.conf
Created April 6, 2012 21:49
basic tmux from screen conf
unbind-key C-b
set -g prefix C-a
bind-key C-a send-prefix
set -g history-limit 7000
@maxjustus
maxjustus / gist:2764682
Created May 21, 2012 21:03
Paranoid hashes
module CoreMixins
module ParanoidHash
def[](k)
fetch(k)
end
end
end
class Hash
def paranoid!
extend(CoreMixins::ParanoidHash)
@maxjustus
maxjustus / gist:2776305
Created May 23, 2012 16:48
Recursive stubs with method expectations for things like arel query chains
class ChainStub
attr_accessor :instance, :result
def initialize(instance)
@instance = instance
@result = Object.new
end
def method_missing(method_name, *args, &blk)
if blk
instance.stub(method_name).with(*args) { blk.call }
@maxjustus
maxjustus / count_by_interval.rb
Created June 7, 2012 19:45
aggregate arbitrary column on time interval for charts using Postgres and ActiveRecord/Arel
module CountByInterval
def count_by_interval(aggregate_range = 'day', column = "#{self.table_name}.created_at", aggregate_operation = 'count(*)')
aggregate_on = "date_trunc('#{aggregate_range}', #{column})"
self.select_values = ["coalesce(#{aggregate_operation}, 0) as value", "#{aggregate_on} as date"]
ActiveRecord::Base.connection.execute(self.group(aggregate_on).to_sql).collect {|r| r}
end
end
ActiveRecord::Relation.send(:include, CountByInterval) #EWWWWwww
@maxjustus
maxjustus / ghetto_child_class_list.rb
Created June 7, 2012 20:12
get child classes of a module or a class in ruby
Module.class_eval do
def child_classes
self.constants.collect do |constant|
c = self.const_get(constant)
c if c.is_a?(Class)
end.compact
end
end
@maxjustus
maxjustus / bash_profile
Created June 9, 2012 04:44
lulz dumbness
1 echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
2 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
3 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
4 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
5 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
6 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
7 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
8 [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
###.....................FOREVER recursiveness
@maxjustus
maxjustus / json_responder.rb
Created June 19, 2012 23:26
default json responder for rails controllers
module Responders
class JsonTemplateResponder < ActionController::Responder
def to_json
render :json => {:html => controller.render_to_string("#{controller.action_name}.html")}
end
end
end