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 / make_time_agoable.rb
Created February 2, 2012 00:33
module that enables a model to produce "time ago" string representations for any given date field (requires active_support)
module MakeTimeAgoable
extend ActiveSupport::Concern
included do
# code goes here that should be run on the actual include
# or just leave it blank. no one cares. where's my cake?
end
module ClassMethods
attr_reader :_time_agoable
@jeffreyiacono
jeffreyiacono / config.ru
Created January 28, 2012 23:14
sample sinatra config.ru that mounts multiple apps
require 'rubygems'
require 'bundler'
Bundler.require
require './app1'
require './app2'
map '/app1' do
run App1
@jeffreyiacono
jeffreyiacono / pace_car.js
Created January 25, 2012 18:41
a simple pace car to test how fast (or slow) your js runs
/**
* PaceCar makes it easy to test how fast (or slow) you js runs
*
* Example Usage:
*
* // get a PaceCar ready
* var p = new PaceCar();
* // start your engines
* p.start();
* // some sample code you want to test performance of.
@jeffreyiacono
jeffreyiacono / ruby-splat.rb
Created January 6, 2012 22:58
fun with splat in ruby
def add(a, b)
a + b
end
def say(what, *people)
people.each { |person| puts "#{person}: #{what}" }
end
say "Hello!", "Bob", "Alice"
# Bob: Hello!
@jeffreyiacono
jeffreyiacono / sencha.rb
Created December 27, 2011 06:41
module that generates json for usage by sencha touch 2 nested list menu component
module Sencha
module NestedListMenu
class << self
# Sencha Touch nested list menu requires json in the form of:
#
# {"text": "some identifier",
# "children": ["text": "child identifier", "other": "info", "leaf": "true"],
# [ ... ]
# },
# { ... }
@jeffreyiacono
jeffreyiacono / sencha_touch_app_helpers.js
Created December 21, 2011 17:49
js helper to "railsify" param keys so sencha touch 2 and rails controllers play nice
/**
* Application helpers
* requires Underscore.js
*/
var ApplicationHelpers = {
/**
* Railsify object's keys to play nice with default Rails controller setup.
* Rails default controller setup expects params submitted via PUT / POST as:
*
* (POST) {'my_model[attr1]' : 'value1', ... etc. }
@jeffreyiacono
jeffreyiacono / helpers.js
Created December 20, 2011 20:59
another little javascript helper
var ApplicationHelpers = {
/**
* Note: requires Underscore.js
*
* Enable a (sencha touch) form to have a link between
* model and form (ie. MyModel.some_attribute <=> textfield.some_attribute)
* AND allow for POST / PUT submission in the rails-friendly namespaced format
* (ie. {my_model[some_attribute] : "some value"}).
*
* To get above behavior, treate model and forms in normal fashion and use
@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"
@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 / 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