在日前结束的Kungfu Rails大会上,来自台湾的著名Rails人张文钿(ihower)为大家带来了一个关于Rails最佳实践的分享,演讲结束后Rails3的核心开发者Yehuda Katz主动索要幻灯片,其内容之精彩已不言而喻。
会后,Bullet插件的作者黄志敏(flyerhzm)根据ihower演讲的内容,制作了一个能够自动检查违背最佳实践代码的Gem,名为rails_best_practices。
rails_best_practices是一个Gem,它会根据ihower在Kungfu Rails大会上的Rails最佳实践的演讲内容,检查Rails应用程序文件的质量。它是一个静态代码解析工具。
rails_best_practices的安装和使用都非常方便,只需简单两条命令就能知道代码中何处需要修改:
sudo gem install rails_best_practices --source http://gemcutter.org
rails_best_practices .
最新的0.3.3版本中,rails_best_practices已经完成了大部分最佳实践的,使用者可以自行配置要检测的条目,目前支持的最佳实践有:
Lesson 1. Move code from Controller to Model
* Move finder to named_scope
* Use model association
* Use scope access
* Add model virtual attribute
* Replace Complex Creation with Factory Method
* Move Model Logic into the Model
Lesson 2. RESTful Conventions
* Overuse route customizations
* Needless deep nesting
* Not use default route
Lesson 3. Model
* Keep Finders on Their Own Model
* Love named_scope
* the Law of Demeter
* Use Observer
Lesson 4. Migration
* Isolating Seed Data
* Always add DB index
Lesson 5. Controller
* Use before_filter
Lesson 6. View
* Move code into controller
* Move code into helper
* Replace instance variable with local variable
Ruby5 不久前在2009年11月17日的Podcast中介绍了rails_best_practices,而railscasts.com的Ryan Bates也已开始关注该项目,相信随着rails_best_practices的日益完善,会有更多的人投来关注的目光。
http://www.slideshare.net/ihower/rails-best-practices