Skip to content

Instantly share code, notes, and snippets.

@ericchen
ericchen / gist:882707
Created March 23, 2011 06:37
Cart model
class Cart
def initialize
@products = []
@rules = []
end
def regular_total
@products.map { |p| p.price }.inject(&:+)
end
@ericchen
ericchen / gist:887934
Created March 26, 2011 01:18
define dynamic class methods in model
class << self
attrs_without_id = AppConfig.column_names.delete_if {|x| x=='id'}
default_values = {'pending_earned_buck_period'=>30*24*60, 'pending_refer_buck_period'=>60*24*60, 'refer_buck_valid_period'=>100*24*60, 'order_delivery_hold_period'=>3*24*60,'code_length'=>8}
attrs_without_id.each do |attr|
define_method(attr) do
load_config
@config && @config.send(attr) || default_values[attr]
end
end
end
@ericchen
ericchen / instant_search.js
Created March 26, 2011 09:58
instant search
$(document).onready(function(){
var _timerId = 0;
$("searchButton").keyup(function(){
window.clearTimeout(_timerId);
_timerId = window.setTimeout(function() {
...perform actual search...
}, 170);
}).keydown(function(){
window.clearTimeout(_timerId);
@ericchen
ericchen / keyup_delay.js
Created April 29, 2011 02:21
Jquery keyup delay
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('input').keyup(function() {
@ericchen
ericchen / .gitconfig
Created May 6, 2011 01:05
git configuration
[user]
name = eric chen
email = youremail
[alias]
st = status
co = checkout
ci = commit
br = branch
[color]
ui = auto
@ericchen
ericchen / call_back_test.rb
Created May 12, 2011 01:10
Test callback methods in Rails
it "should sending command after record created" do
@cmd_pool = CommandPool.new
@cmd_pool.valid?.should == true
@cmd_pool.should_receive(:send_udp_command)
@cmd_pool.run_callbacks(:create)
end
@ericchen
ericchen / gist:970096
Created May 13, 2011 06:49
rspec test
it "should not update email" do
lambda {
post :profile, {:user => {:email=>'', :first_name => 'jacky'}}
@user.reload
}.should_not change(@user, :email)
end
it "should update first name" do
expect {
post :profile, {:user => {:email=>'', :first_name => 'jacky'}}
@ericchen
ericchen / gist:992366
Created May 26, 2011 01:24
manully raise exception for rspec
User.stub!(:create!).and_raise(Mongoid::Errors::Validations)
@ericchen
ericchen / gist:994892
Created May 27, 2011 09:02
mock http test
mock_http = mock("http")
Net::HTTP.stub!(:new).with('www.google.com', 443).and_return(mock_http)
mock_http.should_receive(:use_ssl=).with(true)
mock_http.stub!(:start).and_yield mock_http
mock_http.stub!(:request).and_return 3
get :rate_password, :password => '12345678'
response.should be_success
@ericchen
ericchen / gist:1000225
Created May 31, 2011 09:18
Yahoo Weather Condition Codes
<?xml version="1.0" encoding="UTF-8"?>
<yahoo-weather-codes>
<code number="0" description="tornado"/>
<code number="1" description="tropical storm"/>
<code number="2" description="hurricane"/>
<code number="3" description="severe thunderstorms"/>
<code number="4" description="thunderstorms"/>
<code number="5" description="mixed rain and snow"/>
<code number="6" description="mixed rain and sleet"/>
<code number="7" description="mixed snow and sleet"/>