54
rails-bestpractices.com www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Rails bestpractices.com

Embed Size (px)

DESCRIPTION

介绍rails-bestpractices.com,以及开发中使用的plugins/gems

Citation preview

Page 1: Rails bestpractices.com

rails-bestpractices.com

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 2: Rails bestpractices.com

为什么需要 Rails Best Practices?

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 3: Rails bestpractices.com

遵循 Rails Best Practices

写出更漂亮的 Rails代码

成为更优秀的 Rails程序员

得到更好的回报

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 4: Rails bestpractices.com

提供一个分享最佳实践的地方

提供一个获取最佳实践的地方

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 5: Rails bestpractices.com

rails best practices的故事

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 6: Rails bestpractices.com

很久很久以前。。。

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 7: Rails bestpractices.com

2009年 10月 24 日 上海

ihower发表演讲

“Rails Best Practices”

http://www.slideshare.net/ihower/rails-best-practices

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 8: Rails bestpractices.com

2009年 11月 03日

rails_best_practices项目启动

http://github.com/flyerhzm/rails_best_practices

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 9: Rails bestpractices.com

rails_best_practices项目

根据 ihower的演讲对 rails代

码进行静态分析,找出 bad

smell

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 10: Rails bestpractices.com

2010年 06月 19日

metric_fu项目集成

rails_best_practices

http://metric-fu.rubyforge.org

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 11: Rails bestpractices.com

2010年 07月 04日

rails-bestpracitces.com项目

启动

http://rails-bestpractices.com

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 12: Rails bestpractices.com

rails-bestpractices从何开始?

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 13: Rails bestpractices.com

Rails Template

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 14: Rails bestpractices.com

Rails Template

创建 rails应用的模板

扩展 rails应用的模板

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 15: Rails bestpractices.com

Rails Template

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

run “echo TOD > README”

gem “haml”, “>= 3.0.13”

plugin “typus”, :git => “git://github.com/fesplugas/typus.git”

generate “model User login:string email:string”

route “root :to => 'home#show'”

rake “db:migrate”

git :add => “.”

Page 16: Rails bestpractices.com

Rails Template

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Demo

http://github.com/flyerhzm/rails3-template

Page 17: Rails bestpractices.com

我碰到的 Rails3变动

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Generators

Routes

ActiveRecord接口

Rspec2

Page 18: Rails bestpractices.com

Rails3 Route

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

resources :posts do get :archive, :on => :collection resources :comments, :only => :create resources :votes, :only => [:create, :destroy] resource :implementationend

match 'search' => 'search#show', :as => :searchmatch 'page/:name' => 'pages#show', :as => :pageroot :to => "posts#index"

Page 19: Rails bestpractices.com

Rails3 Route

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

/tags/rails/posts => /tags/rails?nav=posts/tags/rails/posts?page=2 => /tags/rails?nav=posts&page=2/tags/rails/posts?action=show&controller=tags&nav=posts&page=2 => /tags/rails?nav=posts&page=2

match "/tags/:id/posts" => redirect { |params, req| if req.query_string.index('page') # query_string remove controller and action "/tags/#{params[:id]}?#{query_string}" else "/tags/#{params[:id]}?nav=posts" end}

Page 20: Rails bestpractices.com

Rails3 Scope

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

scope :implemented, where(:implemented => true)

default_scope order('created_at desc')

with_exclusive_scope do scope :most_voted, order('vote_points desc')end

Page 21: Rails bestpractices.com

介绍一些非常实用的插件

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 22: Rails bestpractices.com

Haml

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

一种非常简洁的模板语言,是 ERB的替代品

http://github.com/nex3/haml

Page 23: Rails bestpractices.com

Haml (Before)

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

<div id="profile"> <div class="left column"> <div id="date"><%= print_date %></div> </div> <div class="right column"> <div id="email"><%= current_user.email %></div> <div id="bio"><%= h current_user.bio %></div> </div></div>

Page 24: Rails bestpractices.com

Haml (After)

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

#profile .left.column #date= print_date #address= current_user.address .right.column #email= current_user.email #bio= h(current_user.bio)

Page 25: Rails bestpractices.com

Compass

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

一种 css框架

使得 css更易于创建和书写

http://github.com/chriseppstein/compass

Page 26: Rails bestpractices.com

Compass

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

reset

layout (blueprint …)

css3 (border radius, text shadow, gradient …)

utilities (clearfix, float, horizontal list …)

Page 27: Rails bestpractices.com

Compass 代码重用

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

@mixin wikistyle { margin: 1em 0; h1 { font-size: 170%; } h2 { font-size: 150%; } …}

Page 28: Rails bestpractices.com

Compass 代码重用

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

@import ‘wikistyle’

.post .wikistyle { @include wikistyle; margin-top: 10px;}

.question .wikistyle { @include wikistyle; margin-top: 5px;}

Page 29: Rails bestpractices.com

Formtastic

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

简化 form表单的创建

http://github.com/justinfrench/formtastic

Page 30: Rails bestpractices.com

Formtastic

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

= semantic_form_for @user do |form| = form.inputs do = form.input :login, :label => 'Username' = form.input :email = form.input :password = form.input :password_confirmation = form.buttons do = form.submit "Register"

Page 31: Rails bestpractices.com

Inherited_resources

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

加速 controller的开发

http://github.com/josevalim/inherited_resources

Page 32: Rails bestpractices.com

Inherited_resources

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

class AnswersController < InheritedResources::Base before_filter :require_user, :only => [:new, :edit, :create, :update] belongs_to :question

create! do |success, failure| success.html { redirect_to question_path(@question) } failure.html { render 'questions/show' } end

update! do |success, failure| success.html { redirect_to question_path(@question) } endend

Page 33: Rails bestpractices.com

Authlogic authlogic-connect

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

注册、登录

使用 facebook、 twitter帐号登录

使用 OpenID帐号登录

Page 34: Rails bestpractices.com

Exception_notification

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

邮件通知服务器上的错误

非常重要,必不可少!

http://github.com/rails/exception_notification

Page 35: Rails bestpractices.com

简单的优化工作

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 36: Rails bestpractices.com

前端优化

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

把所有的 stylesheets组合为一个 all.css

把所有的 javsacripts组合为一个 all.js

把所有的图片组合为一个 css_sprite.png

Page 37: Rails bestpractices.com

组合 css

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

= stylesheet_link_tag 'compiled/screen', 'compiled/layout', 'compiled/post', :cache => true

Page 38: Rails bestpractices.com

组合 javascript

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

= javascript_include_tag 'jquery', 'rails', 'application', :cache => true

Page 39: Rails bestpractices.com

Css_sprite

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

自动处理 css sprite

约定:images/css_sprite目录下的图片组合成 css sprite图片生成 css_sprite.png和 css_sprite.css (css, sass, scss)

http://github.com/flyerhzm/css_sprite

Page 40: Rails bestpractices.com

Css sprite

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

twitter_icon.png .twitter_icon

hotmail-logo.png hotmail-logo

icons/twitter_icon.png .icons .twitter_icon

widget/icons/twitter_icon.png .widget .icons .twitter_icon

twitter_icon_hover.png .twitter_icon:hover

twitter-icon-hover.png .twitter-icon:hover

logos_hover/gmail_logo.png .logos:hover .gmail_logo

logos-hover/gmail-logo.png .logos:hover .gmail-logo

.gmail-logo-active.png .gmail-logo.active

logos-active/gmail-logo.png .logos.active .gmail-logo

Page 41: Rails bestpractices.com

SQL查询优化

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

找出所有没有索引的数据表外键

http://github.com/eladmeidar/rails_indexes

Page 42: Rails bestpractices.com

N+1查询

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

找出 N+1的查询以及多余的 eager loading

http://github.com/flyerhzm/bullet

Page 43: Rails bestpractices.com

N+1查询

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

动态提示: (alert, console.log, logger, growl, xmpp)

N+1 Query detected Post => [:user] Add to your finder: :include => [:user]N+1 Query method call stack

Page 44: Rails bestpractices.com

测试

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

ngty 测试代码主要贡献者

Page 45: Rails bestpractices.com

remarkable_activerecord

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

has_one :implementation, :dependent => :destroy

has_many :posts, :dependent => :destroy

validates_presence_of :title, :body

validates_uniqueness_of :title

Page 46: Rails bestpractices.com

remarkable_activerecord

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

should_have_one :implementation, :dependent => :destroy

should_have_many :posts, :dependent => :destroy

should_validate_presence_of :title, :body

should_validate_uniqueness_of :title

Page 47: Rails bestpractices.com

Module, module, module

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

class Post < ActiveRecord::Base belongs_to user, :counter_cache => true

def belongs_to?(user) self.user == user endend

Page 48: Rails bestpractices.com

Module, module, module

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

module UserOwnable def self.included(base) base.class_eval do belongs_to :user, :counter_cache => true end end def belongs_to?(user) self.user == user endend

class Post < ActiveRecord::Base include UserOwnableend

Page 49: Rails bestpractices.com

Module, module, module

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

describe Post do include RailsBestPractices::Macros should_be_user_ownableend

module RailsBestPractices::Macros def should_be_user_ownable describe 'being user ownable' do should_belong_to :user, :counter_cache => true it 'should belong to someone if he is the owner of it' it 'should not belong to someone if he is not the owner of it' end endend

Page 50: Rails bestpractices.com

分享你的 Rails最佳实践

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 51: Rails bestpractices.com

Rails最佳实践问答

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 52: Rails bestpractices.com

rails_best_practices gem demo

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 53: Rails bestpractices.com

rails_best_practices将会在本

月底开始支持 rails3

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 54: Rails bestpractices.com

Q&A

谢谢

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development