Skip to content

Instantly share code, notes, and snippets.

View lxneng's full-sized avatar
🎯
Focusing

Eric Luo lxneng

🎯
Focusing
View GitHub Profile
@lxneng
lxneng / gist:331739
Created March 14, 2010 03:00
How to add :format options to a named route in Rails
# Add .format in your path specification, like this:
map.profile '/:profile.:format', :controller => 'users', :action => 'show'
# Then you'll end up with:
profile /:profile(.:format) {:action=>"show", :controller=>"users"}
@lxneng
lxneng / gist:331740
Created March 14, 2010 03:04
has_many
create_table "messages", :force => true do |t|
t.string "subject"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.string "message_type"
t.integer "user_id"
t.boolean "deleted", :default => false
end
@lxneng
lxneng / gist:331743
Created March 14, 2010 03:08
set default ActiveRecord id (mysql)
class CreateCustomers < ActiveRecord::Migration
def self.up
create_table :customers do |t|
t.string :name
t.timestamps
end
execute "ALTER TABLE customers AUTO_INCREMENT = 1000;"
end
month_dict = {"Jan":1,"Feb":2,"Mar":3,"Apr":4, "May":5, "Jun":6,
"Jul":7,"Aug":8,"Sep":9,"Oct":10,"Nov":11,"Dec":12}
def to_dict(name):
return month_dict[name]
#http://docs.python.org/library/time.html#time.strptime
time.strptime('Sat Mar 20', '%a %b %d')
time.struct_time(tm_year=1900, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=79, tm_isdst=-1)
time.strptime('3pm', '%I%p')
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=15, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
In [19]: dts = 'Sat Mar 20 2010 5pm'
@lxneng
lxneng / gist:333536
Created March 16, 2010 01:25
geopy test
In [1]: from geopy import geocoders
In [2]: us = geocoders.GeocoderDotUS()
In [3]: print us.geocode("314 W 77th St #4B, New York NY 10024")[0]
------> print(us.geocode("314 W 77th St #4B, New York NY 10024")[0])
314 W 77th St, New York, NY 10024
<% for item in @all_items %>
<li><%= link_to item.content_type + item.content_id.to_s, :controller => item.content_type.tableize, :action => "edit", :id => item.content_id %></li>
<% end %>
eval(key.to_s + "_path")
def timer(end_time)
d = (end_time - Time.now).to_i
return "#{(d/60)%60}:#{d%60}"
end
>> timer(10.minutes.from_now)
=> "9:59"
counts = Users.count(:group => :country)
counts.each |country, count| do
p "#{country} - #{count}"
end
# Prints
US 123
UK 54
DE 33
@lxneng
lxneng / gist:336131
Created March 18, 2010 07:40
Using the C API MySQL module for Ruby, let rails connect to mysql faster!
# http://www.tmtm.org/en/mysql/ruby/
apt-get install libmysqlclient16-dev
wget http://rubyforge.org/frs/download.php/69181/mysql-ruby-2.8.2.tar.gz
tar zxvf mysql-ruby-2.8.2.tar.gz
cd mysql-ruby-2.8.2/
ruby extconf.rb --with-mysql-dir=/usr/local/mysql
make
make install