Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Sierra.ai
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / ladder.sql
Created April 23, 2012 21:49
mysql ladder
CREATE TABLE `ladder` (
`i` int(1) unsigned NOT NULL,
PRIMARY KEY (`i`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO ladder (i) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
SELECT l1.i + l2.i * 10 + l3.i * 100 AS sequence
FROM ladder l1
JOIN ladder l2 ON TRUE
@jeffreyiacono
jeffreyiacono / question.rb
Created March 20, 2012 18:06
(Partial) Question spec for proview (revisited)
class Question < ActiveRecord::Base
default_scope order("seq ASC")
# decrement seq / page # depending on position and page count
def move_up!
if self.first_on_page?
move_to_previous_page!
else
decrement_seq! && self.survey.renumber_questions!
end
@jeffreyiacono
jeffreyiacono / lolrus.js
Created March 18, 2012 04:53
simple normalization of jquery event so offsetX / offsetY can be used cross browsers
/**
* normalizeEvent
*
* Firefox does not implement offsetX, OffsetY, so we have to detect for this an
* manually calculate it ourselves using the pageX, pageY less the event
* target's left offset and right offset
*
* If using a browser that supports offsetX, OffsetY, just return the event,
* don't need to do anything
*/
@jeffreyiacono
jeffreyiacono / question_spec.rb
Created March 17, 2012 23:06
question spec for Proview
require 'spec_helper'
describe Question do
it { should belong_to(:page) }
it { should belong_to(:survey) }
it { should have_many(:answers) }
describe "default scope" do
let(:first_question) { Question.create(seq: 1) }
let(:second_question) { Question.create(seq: 2) }
@jeffreyiacono
jeffreyiacono / make_emailable.rb
Created March 8, 2012 17:40
module that extracts common mailer tasks for soundtrak.me
require 'fileutils'
require 'ostruct'
require 'pony'
require 'active_support'
module MakeEmailable
extend ActiveSupport::Concern
included do
attr_reader :sender, :receiver
@jeffreyiacono
jeffreyiacono / track_spec.rb
Created March 7, 2012 07:03
Track spec for comments / commenter cache retrieval on soundtrak.me
require 'spec_helper'
describe Track do
describe "commenters" do
let(:sleepy) { build(:user, fullname: 'Mr. Sleepy', email: 'sleepy@example.com', username: 'sleepy') }
let(:dopey) { build(:user, fullname: 'Mr. Dopey', email: 'dopey@example.com', username: 'dopey') }
let(:doc) { build(:user, fullname: 'Mr. Doc', email: 'doc@example.com', username: 'doc') }
let(:sleepy_comment) { build(:comment, commenter: sleepy, content: "sleepy's comment") }
let(:dopey_comment) { build(:comment, commenter: dopey, content: "dopey's comment") }
let(:track) { build(:track) }
@jeffreyiacono
jeffreyiacono / triangles-sim.rb
Last active September 30, 2015 13:07
simulation of the math problem: "If you break a straight line randomly in two places, what is the probability that you can form a triangle from the resulting three pieces?"
# simulation to calculate an answer to the question:
#
# "If you break a straight line randomly in two places, what is the probability that you can
# form a triangle from the resulting three pieces?"
#
# usage: ruby ./triangles-sim.rb [trials]
# sample output:
#
# $> ruby ./triangles-sim.rb 1000000
#
@jeffreyiacono
jeffreyiacono / family-sim.rb
Created February 11, 2012 02:12
simulation of the math problem: "In a country in which people only want boys, every family continues to have children until they have a boy. If they have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to gir
# simulation to calculate an answer to the question:
#
# "In a country in which people only want boys, every family continues to have children until they have a boy. If they
# have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to girls in the
# country?"
#
# !/usr/bin/env ruby
# usage: ./family-sim.rb [trials]
#
# sample output:
@jeffreyiacono
jeffreyiacono / Rakefile
Created February 8, 2012 20:15
rake task for precompiling assets using sprockets within a sinatra app + view helpers
require 'rubygems'
require 'bundler'
Bundler.require
require './application'
namespace :assets do
desc 'compile assets'
task :compile => [:compile_js, :compile_css] do
end
@jeffreyiacono
jeffreyiacono / mongoid_counter_cache.rb
Created February 4, 2012 22:52
counter cache extension for mongoid orm
module Mongoid
module CounterCache
extend ActiveSupport::Concern
module ClassMethods
# Use Case:
#
# keep a cached count of embedded collections in the parent collection(s)
#
# for example, if a Post embeds many Comments, and there have been 2