Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Eight Sleep
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / mind_blowing_sql.sql
Created August 24, 2011 15:10
Most / least expensive dealer
-- in mysql, create "shop" table with article, dealer, and price
CREATE TABLE shop (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, article INT, dealer VARCHAR(100), price DOUBLE);
-- populate some records
-- article 1
INSERT INTO shop (article, dealer, price) VALUES (1, "jeff", 100.0);
INSERT INTO shop (article, dealer, price) VALUES (1, "bill", 101.0);
-- article 2
INSERT INTO shop (article, dealer, price) VALUES (2, "jeff", 30);
INSERT INTO shop (article, dealer, price) VALUES (2, "bill", 30);
@jeffreyiacono
jeffreyiacono / user_spec.rb
Created August 24, 2011 19:21
A sample user spec for Fernando / Dallas.rb
describe User do
it { should validate_presence_of :email }
describe :full_name do
subject { User.new(:email => "jeff@iacono.com") }
context "when first and last name are present" do
before { subject.first_name, subject.last_name = "Jeff", "Iacono" }
its(:full_name) { should eq("Jeff Iacono") }
end
@jeffreyiacono
jeffreyiacono / dev_to_prod.js
Created September 20, 2011 07:37
(budget) find / replace for mongodb
// For a collection "things" that has multiple documents of the form:
// { "_id" : ObjectId("4e5c47d1b0207a1ad3000001"),
// "filename" : "uploads/development/user/4dbf2b0ab0207a2e8a000001/avatar/avatar.png",
// "other_data" : "foo"
// }, etc.
// we want to replace "development" in "filename" with "production".
// Run via mongo shell:
db.things.find().forEach(function(e) {
var parts = e.filename.split('development');
e.filename = parts[0] + 'production' + parts[1];
@jeffreyiacono
jeffreyiacono / gist:1243483
Created September 26, 2011 21:38
Excel VBA user defined function sample
Public Function tell_if_odd_or_even(num As Integer) As String
Dim parity As String
If num Mod 2 = 0 Then
parity = "even"
Else
parity = "odd"
End If
tell_if_odd_or_even = num & " is " & parity & "!"
@jeffreyiacono
jeffreyiacono / rails-3-ujs-example.js
Created October 16, 2011 21:38
Rails 3 UJS Remote JavaScript callbacks (example)
// via: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
jQuery(function($) {
// create a convenient toggleLoading function
var toggleLoading = function() { $("#loading").toggle() };
$("#tool-form")
.bind("ajax:loading", toggleLoading)
.bind("ajax:complete", toggleLoading)
.bind("ajax:success", function(event, data, status, xhr) {
$("#response").html(data);
@jeffreyiacono
jeffreyiacono / table-vars-and-set-operators.sql
Created October 24, 2011 00:02
stanford open course db-class: "Table variables and set operators"
-- To get started, create the database (load sql below, stop after dump completion)
--
-- command to dump db: jfi@mac:~$ mysqldump -u root -p dbclass
--
-- Table structure for table `apply`
--
DROP TABLE IF EXISTS `apply`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
# written after reading:
# http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
# moderator class so we can encapsulate everything and if we need to change the
# moderation (#moderate) algo, we can easily do that without messing with the rest
# of the program
class Moderator
# moderate algo:
# if number is divisible by 3 and 5, return "FizzBuzz"
# else if number is divisible by only 3, return "Fizz"
@jeffreyiacono
jeffreyiacono / avatar_image.rb
Created November 4, 2011 16:03
simple gravatar / avatar image model
class AvatarImage
GRAVATAR_IMAGE_BASE_URL = 'http://www.gravatar.com/avatar/'
GRAVATAR_IMAGE_DEFAULT_SIZE = '32'
DEFAULT_URL = 'http://your-awesome-domain.com/images/your-awesome-default-image.png'
attr_accessor :email
def self.default_url
DEFAULT_URL
end
@jeffreyiacono
jeffreyiacono / avatarable.rb
Created November 13, 2011 22:08
simple gravatar / avatar image module, alternative to https://gist.github.com/1339702
module Avatarable
extend ActiveSupport::Concern
GRAVATAR_IMAGE_BASE_URL = 'http://www.gravatar.com/avatar/'
GRAVATAR_IMAGE_DEFAULT_SIZE = '32'
DEFAULT_URL = 'http://your-awesome-domain.com/images/your-awesome-default-image.png'
# Avatarable assumes the class (or other module) that includes this module has an email attribute
# if the email attribute is named something other than email, use alias_attribute to map it to email
# alias_attribute :email, :your_email_attribute
@jeffreyiacono
jeffreyiacono / helpers.js
Created December 16, 2011 03:01
little javascript helper
// requires Underscore.js
var helpers = {
// Takes object and converts key / values to a sentence
//
// Sample usage:
//
// obj = {"this" : "is good", "that" : "is bad", "those" : "are great"}
//
// helpers.to_sentence(obj)
// # "this is good, that is bad, and those are great"