flyerhzm

flyerhzm

Login flyerhzm

Website http://www.huangzhimin.com/

13 votes

4 comments

4026 views

Use asset_host for production server

Use asset host for cookie-free domains for components, that make your components load faster.

flyerhzm

4 votes

2 comments

6212 views

Use multipart/alternative as content_type of email

Rails uses plain/text as the default content_type for sending email, you should change it to multipart/alternative that email clients can display html formatted email if they support and display plain text email if they don't support html format.

implemented
flyerhzm

8 votes

2 comments

4566 views

Use css sprite automatically

Using css sprite can reduce a large number of http requests, so it makes the web page loaded much faster. It it painful to composite a lot of images manually, do it automatically.

flyerhzm

10 votes

32 comments

12377 views

Use STI and polymorphic model for multiple uploads

This is a flexible and reusable solution for multiple uploads, using STI model to save all the uploaded assets in one "assets" table and using polymorphic model to reuse "Asset" model in different uploadable models.

flyerhzm

0 votes

18 comments

16170 views

Fetch current user in models

I don't remember how many times I need to fetch current user in models, such as audit log. Here is a flexible way to set the current user in and fetch the current user from User model.

flyerhzm

8 votes

1 comments

4845 views

Put scripts at the bottom

Do you experience that your website renders slow due to loading a lot of javascripts, especially loading some third-party javascripts? Move script tags to the bottom of body can speed up the render of your website.

flyerhzm

14 votes

3 comments

17959 views

DRY bundler in capistrano

There are a few posts told you how to integrate bundler into capistrano, but they are out of date now. After bundler 1.0 released, you can add only one line in capistrano to use bundler.

implemented
flyerhzm

8 votes

7 comments

4132 views

Use batched finder for large data query

If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.

flyerhzm

9 votes

4 comments

5148 views

Generate polymorphic url

If you want to generate different urls according to different objects, you should use the polymorphic_path/polymorphic_url to simplify the url generation.

flyerhzm

19 votes

10 comments

7520 views

Use query attribute

Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute

implemented
flyerhzm

4 votes

14 comments

5103 views

Select specific fields for performance

In a system like forum, the title and body is displayed on show page, but only title is on index page. You should use select in query to speed up the query and save memory.

flyerhzm

26 votes

12 comments

11183 views

Use memoization

Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. In rails, you can easily use memoize which is inherited from ActiveSupport::Memoizable.

flyerhzm

14 votes

9 comments

9105 views

remove trailing whitespace

Trailing whitespace always makes noises in version control system, it is meaningless. We should remove trailing whitespace to avoid annoying other team members.

implemented
flyerhzm

10 votes

18 comments

9743 views

Simplify render in views

render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided.

implemented
flyerhzm

7 votes

6 comments

6042 views

Simplify render in controllers

Like the simplify render in views, from rails 2.3, we can also simplify render in controllers.

implemented
flyerhzm

2 votes

9 comments

5599 views

comment your magic codes

Ruby/Rails provides a lot of magic codes, especially for metaprogramming, they are powerful, less codes to implement more functions, but they are not intuition, you should write good comment for your magic codes.

flyerhzm

13 votes

4 comments

3510 views

defer expensive job

If you want to process something expensive as part of a web request, it will delay the response. If the job is not critical, it's wiser to move the expensive to a background queue and returns the response immediately.

flyerhzm

20 votes

21 comments

9646 views

Annotate your models

Are you tired of going to schema.rb to find your table structures information? It would be better to list all the attributes of the model in the model itself.

flyerhzm

13 votes

6 comments

5302 views

Remove empty helpers

If you use rails generator to create scaffolds or controllers, it will also create some helpers, most of the helpers are useless, just remove them.

implemented
flyerhzm

20 votes

6 comments

7820 views

split route namespaces into different files

the routes will become complicated with the growth of your application, contain different namespaces, each with a lot of resources and custom routes, it would be better to split routes into different files according to the namespaces, which makes it easy to maintain the complicated routes.

flyerhzm

2 votes

5 comments

3562 views

Remove tab

Using tabs can mess up the spacing since some IDE's use 4 spaces for a tab, while others use 2, and some people don't use tabs at all, a mix of tabs and spaces causes things to not line up in most cases.

implemented
flyerhzm

3 votes

3 comments

4200 views

Optimize db migration

rails migration provides a convenient way to alter database structure, you can easily add, change and drop column to a existing table, but when the data in existing table are huge, it will take a long time to alter existing table, you should try to merge/optimize the db alter sql statements.

flyerhzm

5 votes

7 comments

6636 views

Use cells to abstract view widgets

Rails developers always pay more attentions on models and controllers refactoring, they don't take care about views modularization, that makes view codes most difficult to maintain. Here I recommend you to use cells gem to write more reuseable, testable and cacheable view codes.

flyerhzm
Fork me on GitHub