Skip to content

Instantly share code, notes, and snippets.

class String
# "Opportunity Type" => "opportunity_type"
# "Sub-segments" => "sub_segments"
def unhumanize
words = self.dup
words.downcase!.gsub!(/[ +|-]/, '_')
words
end
function random_chinese_name() {
function random(a, l) {
var x = [];
x.push(a[Math.ceil(Math.random() * a.length)]);
while(l > 1) {
x.push(a[Math.ceil(Math.random() * a.length)]);
l--;
}
return x.join("");
}
require 'rubygems'
require 'hotcocoa'
require 'net/http'
# Replace the following code with your own hotcocoa code
class Application
include HotCocoa
@rociiu
rociiu / webapp.rb
Created November 16, 2010 22:38 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
require 'fileutils'
start_time = Time.now
SOURCE_DB = {
:name => 'db_name',
:user => 'db_user',
:password => 'db_pass',
:host => 'localhost'
$:.unshift("/Users/miloops/Workspace/github/miloops/rails/activerecord/lib")
$:.unshift("/Users/miloops/Workspace/github/railsarel/lib")
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
ActiveRecord::Schema.define do

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

#http://refactormycode.com/codes/281-given-a-hash-of-variables-render-an-erb-template
require 'erb'
require 'ostruct'
def render_template(template, params)
vars = OpenStruct.new params
class << vars
include CustomHelper, ApplicationHelper, ActionView::Helpers::TextHelper
articipantBiometric Load (0.6ms) SELECT * FROM `participant_biometrics` WHERE (`participant_biometrics`.`id` = 25984) AND (`participant_biometrics`.`deleted_at` IS NULL)
ParticipantBiometric Load (0.5ms) SELECT `participant_biometrics`.id FROM `participant_biometrics` WHERE (`participant_biometrics`.`lab_date` = '2009-01-09' AND `participant_biometrics`.user_id = 18542 AND `participant_biometrics`.deleted_at IS NULL AND `participant_biometrics`.id <> 25984) LIMIT 1
ParticipantBiometric Load (0.7ms) SELECT * FROM `participant_biometrics` WHERE (lab_date >= '2009-01-01' and lab_date < '2010-01-01') AND ((`participant_biometrics`.`deleted_at` IS NULL AND `participant_biometrics`.`user_id` = 18542))
ParticipantBiometric Load (0.5ms) SELECT * FROM `participant_biometrics` WHERE (`participant_biometrics`.`id` = 25984) AND (`participant_biometrics`.`deleted_at` IS NULL)
ParticipantBiometric Load (0.6ms) SELECT `participant_biometrics`.id FROM `participant_biometrics` WHERE (`participant_biometri
class Fixtures
alias_method :origin_delete_existing_fixtures, :delete_existing_fixtures
def delete_existing_fixtures; puts "doing nothing."; end
end
# during the import
class Fixtures
alias_method :delete_existing_fixtures, :origin_delete_existing_fixtures
remove_method :origin_delete_existing_fixtures
end