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 / hoppity.rb
Created November 12, 2009 21:39
fb careers challenge puzzle: hoppity
#!/usr/bin/ruby
# get file name arg from command line
File.open(ARGV[0], 'r') do | f |
# read the first line containing the integer
(1..f.readline.strip.to_i).each do | i |
# div by 3?
if ( i % 3 ) == 0 then
# and by 5?
if ( i % 5 ) == 0 then
#! /usr/bin/ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
DIRECTORY_QUERY_URL_FOR = "https://web.middlebury.edu/database/directory/?cn="
"A".upto("Z") do |letter|
url = "#{DIRECTORY_QUERY_URL_FOR}#{letter}"
@jeffreyiacono
jeffreyiacono / widgets_controller.rb
Created January 29, 2011 20:20
widgets controller
class WidgetsController < ApplicationController
def destroy
@widget = Widget.find(params[:id])
respond_to do |format|
if @widget.delete
format.html { flash[:notice] = "Item Removed"; redirect_to widgets_path }
# change format.json to format.js and it works properly
format.js { render :status => :ok }
else
format.html { flash[:alert] = "Something went terribly wrong"; redirect_to widgets_path }
@jeffreyiacono
jeffreyiacono / widgets.js
Created January 29, 2011 20:25
javascript for widget deletion
$.ajax({
url: this.href,
type: 'POST',
dataType: 'script',
data: {_method: 'delete'},
beforeSend: function() { console.log("in before send ...") },
complete: function() { console.log("in complete ...") }
});
@jeffreyiacono
jeffreyiacono / select_timestamp.rb
Created July 15, 2011 04:13
UI T/F select which sets a objects timestamp
# Given that I have a model Item that inherits from ActiveRecord::Base and it has an attribute #locked_at,
# I want to be able to select T/F from a select in UI to set #locked_at's timestamp
# === VIEW ===
# app/views/items/edit.html.haml, using formtastic
# Attempt #1:
# I could set locked_at => Time.now by setting it in the select's collection,
# but revisiting this form will not show the properly selected option, unless you do :selected => Time.now,
# which seems kludgy. Worse, if unlocked (locked_at => nil), this select option will not be
@jeffreyiacono
jeffreyiacono / damn_you_usec.rb
Created July 18, 2011 04:34
Test sequence discovering how microseconds can cause your tests to fail while reporting that all looks equal
# Givens: Rails 3.0.7, Ruby 1.9.2, with RSpec2 (2.6.4) and FactoryGirl (1.3.3)
# original test ...
require 'spec_helper'
describe Item do
context 'a trivial example' do
before do
@item = Factory.create(:item)
end
@jeffreyiacono
jeffreyiacono / gist:1114452
Created July 29, 2011 18:41 — forked from dhh/gist:1014971
Use concerns to keep your models manageable (fix unscoped issue w/ trashed scope)
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@jeffreyiacono
jeffreyiacono / exporter.rb
Created August 2, 2011 22:21
Simple Exporter
require 'csv'
# Class that takes a set of records from a Rails app and returns CSV'd goodness
# Record's must implement class method #csv_exportable_headers and instance method #csv_exportable_attributes
# Note: This probably should be moved to a module where the headers / attributes are defined
class Exporter
module CSV
def self.export(records)
# TODO: dropout if records is blank
klass = records.model_name.constantize
@jeffreyiacono
jeffreyiacono / controller_specs.rb
Created August 9, 2011 13:51
Items, Widget, and Gears controller spec examples
# Client required that *all* methods for controllers are covered and
# do not allow guest access.
#
# To remove code duplication, add controller macro, update test suite.
# Macro defaults to :html formatted requests and handles member vs collection (assumes :id)
# Note that we can specify methods, formats, params, etc. along the lines of:
# it_should_block_access_to :index, :format => :json
# it_should_block_access_to :something, :method => :post
# it_should_block_access_to :index, :foo => :bar
@jeffreyiacono
jeffreyiacono / controller_specs.rb
Created August 9, 2011 15:00
Items, Widget, and Gears controller spec examples
# Client required that *all* routes for controllers are covered and
# do not allow guest access.
#
# There are several other controllers that, for guest test coverage,
# are identical to the Items Controller spec below
# Note: there are several non-CRUD methods; destroy makes a record
# inactive; there is no record removal revealed via the API (client's request)
# spec/controllers/items_controller_spec.rb
require 'spec_helper'