Upgrade to capistrano3

02 Nov 2013

New things

I updated capistrano from 2.x to 3.0 for one project, it was a huge change. The followings are the new things:

1. New structure. If you use capify to generate base structure, you will see some new syntax.

In Capfile, you need to require all dependencies/plugins you need for deployment.

require 'capistrano/setup'
require 'capistrano/deploy'

require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'

Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
... Read More


Use paperclip without activerecord

31 Oct 2013

Recently I built an image upload api which didn't use activerecord, but I don't want to handle resizing image thumbnails by myself, so I decided to reuse paperclip.

Paperclip is an easy file attachment management for ActiveRecord, but we used activemodel without activerecord, I found a gist which gave me a simple solution, but it was not enough. We continued the hacking work.

1. defined the attachment path and url. Paperclip used AR id ...

Read More


railsrumble 2013 - designapis

21 Oct 2013

I took part in this railsrumble last weekend, my entry is designapis.com, vote it up here.

Why did I built it? It comes from my working experience, I worked on serveral projects that needs to design http apis for ios clients, as we worked remotely, we have to write down the apis so that ios developers and ruby developers can work independently.

Before we used google docs and github gists, they are both good to share between team, but they don't provide any style/format, I have to set the for...

Read More


mongoid 3.0.x not set relation properly

08 Sep 2013

I was trying to fix bullet test failure with mongoid 3.0.23, the failed test is to test the 1-1 relationship as follows

it "should detect non preload association" do
  Mongoid::Company.all.each do |company|

After reading the logs, it generated 2 unexpected query

MOPED: QUERY        database=bullet collection=mongoid_companies selector={} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (196.5840ms)
Read More


Safari video tag without referer header

07 Aug 2013

We're building a website which needs to play video online, we know it's pretty easy for modern browsers who support html5, like chrome and safari, they all support video tag, which can play video files online directly.

<video controls>
  <source src="RESOURCE URL HERE" />

It's supposed to work for most cases, but our video resources are uploaded to s3...

Read More


JQuery AMD Plugin Template

06 Aug 2013

Several years ago I posted how to write a jquery plugin template, but in the recent years, browser javascript is evolving, developers are more likely using asynchronous module definition API (Require.js). So the jquery plugin template should also be updated like

(function (factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as anonymous module.
    define(['jquery'], factory);
  } else {
    // Browser globals.
}(function ($) {
Read More


Migrate rails-bestpractices.com to rails4

14 Jul 2013

These 2 weeks I migrated rails-bestpractices.com to rails 4 from rails 3.2.13. Here are some experience I'd like to share with you.

Make sure you have good test code

rails-bestpractices.com has many rspec and cucumber test code, they can find out most of warnings and errors after migration.

Update Gems

First, update rails to 4.0.0 in Gemfile, but soon you will find you have to update many gems, devise, compass-rails, cucumber-rails, etc., some are rc version or raisl4 branch,

You also need to remove some gems, like strong_parameters and turbo-sproc...

Read More


How to render, upload and download large files on heroku with s3

18 Jun 2013

I'm consulting on a rails project on heroku, it involves generating a large pdf for customer, so you must already guess it led to 30s timeout on heroku.

At first, I handled it with common sense, moving pdf render to a background job, in the client side, it polls the status of bj, if job is complete, then render the pdf.

Everything works fine on my laptop, but after pushing to heroku, it succeed to running then job, polling the status, but finally it can't find the generated pdf. Then I realized web dyno and worker dyno are runni...

Read More


My presentation at reddotrubyconf 2013

07 Jun 2013

This is my presentation on reddotrubyconf 2013 with notes, building asynchronous apis.

3. Several years ago when I started learning rails, many people said rails was not fast, but it can significantly speed up development, the famous words are "Hardware is cheap, Programmers are expensive".

4. It is tru...

Read More


another zero downtime deployment solution

30 Jan 2013

I wrote a post for jruby migration 2 monthes ago, it mentioned a solution to do zero downtime deployment: pull out server out of load balancers, restart server, and then put in the server. It works but has some cons

  1. you must have more than 1 app hosts.
  2. deployment process gets much slower if you have lots of app hosts.
  3. you lost one host's throughput during deployment.

I'm using a different solution for zero downtime deployment now, instead of processing app hosts one by one

  1. it starts replicated ruby instances on a...
Read More