SlideShare a Scribd company logo
1 of 54
Download to read offline
rails-bestpractices.com www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
为什么需要 Rails Best Practices? www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
遵循 Rails Best Practices 写出更漂亮的 Rails 代码 成为更优秀的 Rails 程序员 得到更好的回报 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
提供一个分享最佳实践的地方 提供一个获取最佳实践的地方 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
rails best practices 的故事 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
很久很久以前。。。 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
2009 年 10 月 24 日  上海 ihower 发表演讲 “ Rails Best Practices” http://www.slideshare.net/ihower/rails-best-practices www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
2009 年 11 月 03 日 rails_best_practices 项目启动 http://github.com/flyerhzm/rails_best_practices www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
rails_best_practices 项目 根据 ihower 的演讲对 rails 代码进行静态分析,找出 bad smell www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
2010 年 06 月 19 日 metric_fu 项目集成 rails_best_practices http://metric-fu.rubyforge.org www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
2010 年 07 月 04 日 rails-bestpracitces.com 项目启动 http://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 从何开始? www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Rails Template www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Rails Template 创建 rails 应用的模板 扩展 rails 应用的模板 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Rails Template www.ekohe.com Web Development & Graphic Design China 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 => “.”
Rails Template www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   Demo http://github.com/flyerhzm/rails3-template
我碰到的 Rails3 变动 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   Generators Routes ActiveRecord 接口 Rspec2
Rails3 Route www.ekohe.com Web Development & Graphic Design China 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 :implementation end match 'search' => 'search#show', :as => :search match 'page/:name' => 'pages#show', :as => :page root :to => "posts#index"
Rails3 Route www.ekohe.com Web Development & Graphic Design China 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 }
Rails3 Scope www.ekohe.com Web Development & Graphic Design China 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
介绍一些非常实用的插件 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Haml www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   一种 非常简洁的模板语言,是 ERB 的替代品 http://github.com/nex3/haml
Haml  (Before) www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   <div id=&quot;profile&quot;> <div class=&quot;left column&quot;> <div id=&quot;date&quot;><%= print_date %></div> </div> <div class=&quot;right column&quot;> <div id=&quot;email&quot;><%= current_user.email %></div> <div id=&quot;bio&quot;><%= h current_user.bio %></div> </div> </div>
Haml  (After) www.ekohe.com Web Development & Graphic Design China 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)
Compass www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   一种 css 框架 使得 css 更易于创建和书写 http://github.com/chriseppstein/compass
Compass www.ekohe.com Web Development & Graphic Design China 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 …)
Compass  代码重用 www.ekohe.com Web Development & Graphic Design China 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%; } … }
Compass  代码重用 www.ekohe.com Web Development & Graphic Design China 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; }
Formtastic www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   简化 form 表单的创建 http://github.com/justinfrench/formtastic
Formtastic www.ekohe.com Web Development & Graphic Design China 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 &quot;Register&quot;
Inherited_resources www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   加速 controller 的开发 http://github.com/josevalim/inherited_resources
Inherited_resources www.ekohe.com Web Development & Graphic Design China 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) } end end
Authlogic  authlogic-connect www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   注册、登录 使用 facebook 、 twitter 帐号登录 使用 OpenID 帐号登录
Exception_notification www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   邮件通知服务器上的错误 非常重要,必不可少! http://github.com/rails/exception_notification
简单的优化工作 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
前端优化 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   把所有的 stylesheets 组合为一个 all.css 把所有的 javsacripts 组合为一个 all.js 把所有的图片组合为一个 css_sprite.png
组合 css www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   = stylesheet_link_tag 'compiled/screen', 'compiled/layout', 'compiled/post', :cache => true
组合 javascript www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   = javascript_include_tag 'jquery', 'rails', 'application', :cache => true
Css_sprite www.ekohe.com Web Development & Graphic Design China 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
Css sprite www.ekohe.com Web Development & Graphic Design China 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
SQL 查询优化 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   找出所有没有索引的数据表外键 http://github.com/eladmeidar/rails_indexes
N+1 查询 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   找出 N+1 的查询以及多余的 eager loading http://github.com/flyerhzm/bullet
N+1 查询 www.ekohe.com Web Development & Graphic Design China 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
测试 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   ngty  测试代码主要贡献者
remarkable_activerecord www.ekohe.com Web Development & Graphic Design China 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
remarkable_activerecord www.ekohe.com Web Development & Graphic Design China 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
Module, module, module www.ekohe.com Web Development & Graphic Design China 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 end end
Module, module, module www.ekohe.com Web Development & Graphic Design China 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 end end class Post < ActiveRecord::Base include UserOwnable end
Module, module, module www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development   describe Post do include RailsBestPractices::Macros should_be_user_ownable end 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 end end
分享你的 Rails 最佳实践 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Rails 最佳实践问答 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
rails_best_practices gem demo www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
rails_best_practices 将会在本月底开始支持 rails3 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
Q&A 谢谢 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

More Related Content

What's hot

Python to go
Python to goPython to go
Python to goWeng Wei
 
Lessons Learned From Cross-Platform at Airbnb + MvRx
Lessons Learned From Cross-Platform at Airbnb + MvRxLessons Learned From Cross-Platform at Airbnb + MvRx
Lessons Learned From Cross-Platform at Airbnb + MvRxGabriel Peal
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overviewJan Berdajs
 

What's hot (6)

Python to go
Python to goPython to go
Python to go
 
Lessons Learned From Cross-Platform at Airbnb + MvRx
Lessons Learned From Cross-Platform at Airbnb + MvRxLessons Learned From Cross-Platform at Airbnb + MvRx
Lessons Learned From Cross-Platform at Airbnb + MvRx
 
Rails 5 subjective overview
Rails 5 subjective overviewRails 5 subjective overview
Rails 5 subjective overview
 
What's new on Rails 5
What's new on Rails 5What's new on Rails 5
What's new on Rails 5
 
Working with Git
Working with GitWorking with Git
Working with Git
 
Upgrading to rails3
Upgrading to rails3Upgrading to rails3
Upgrading to rails3
 

Similar to Rails bestpractices.com

Static Code Analysis For Ruby
Static Code Analysis For RubyStatic Code Analysis For Ruby
Static Code Analysis For RubyRichard Huang
 
Contributing To Rails By Plugin Gem
Contributing To Rails By Plugin GemContributing To Rails By Plugin Gem
Contributing To Rails By Plugin GemDaniel Lv
 
Css sprite best practices
Css sprite best practicesCss sprite best practices
Css sprite best practicesRichard Huang
 
Ruby application based on http
Ruby application based on httpRuby application based on http
Ruby application based on httpRichard Huang
 
Design Pattern From Java To Ruby
Design Pattern From Java To RubyDesign Pattern From Java To Ruby
Design Pattern From Java To Rubyyelogic
 
Email Contacts For Invitation
Email Contacts For InvitationEmail Contacts For Invitation
Email Contacts For InvitationRichard Huang
 
Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010arif44
 
Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Shaer Hassan
 
Vivendo No Mundo Rails
Vivendo No Mundo RailsVivendo No Mundo Rails
Vivendo No Mundo RailsAlisson Sales
 
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyConsegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyFabio Akita
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summarydaniel.mattes
 
Ruby on Rails best resources for self
Ruby on Rails best resources for selfRuby on Rails best resources for self
Ruby on Rails best resources for selfDurga Prasad Tumu
 
Ruby Kaigi09 China Rubyupdate20090718
Ruby Kaigi09 China Rubyupdate20090718Ruby Kaigi09 China Rubyupdate20090718
Ruby Kaigi09 China Rubyupdate20090718tengu
 
1st Chinaonrails Open Course 初试牛刀
1st Chinaonrails Open Course 初试牛刀1st Chinaonrails Open Course 初试牛刀
1st Chinaonrails Open Course 初试牛刀Jesse Cai
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHPGautam Rege
 
Introduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on RailsIntroduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on Railspmatsinopoulos
 
Encontro De Ti - Ruby e Rails
Encontro De Ti - Ruby e RailsEncontro De Ti - Ruby e Rails
Encontro De Ti - Ruby e RailsFabio Akita
 

Similar to Rails bestpractices.com (20)

Static Code Analysis For Ruby
Static Code Analysis For RubyStatic Code Analysis For Ruby
Static Code Analysis For Ruby
 
Contributing To Rails By Plugin Gem
Contributing To Rails By Plugin GemContributing To Rails By Plugin Gem
Contributing To Rails By Plugin Gem
 
Css sprite best practices
Css sprite best practicesCss sprite best practices
Css sprite best practices
 
Ruby application based on http
Ruby application based on httpRuby application based on http
Ruby application based on http
 
Design Pattern From Java To Ruby
Design Pattern From Java To RubyDesign Pattern From Java To Ruby
Design Pattern From Java To Ruby
 
Email Contacts For Invitation
Email Contacts For InvitationEmail Contacts For Invitation
Email Contacts For Invitation
 
Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010
 
Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09
 
Vivendo No Mundo Rails
Vivendo No Mundo RailsVivendo No Mundo Rails
Vivendo No Mundo Rails
 
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyConsegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Intro to Rails
Intro to RailsIntro to Rails
Intro to Rails
 
Rails for Django developers
Rails for Django developersRails for Django developers
Rails for Django developers
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
Ruby on Rails best resources for self
Ruby on Rails best resources for selfRuby on Rails best resources for self
Ruby on Rails best resources for self
 
Ruby Kaigi09 China Rubyupdate20090718
Ruby Kaigi09 China Rubyupdate20090718Ruby Kaigi09 China Rubyupdate20090718
Ruby Kaigi09 China Rubyupdate20090718
 
1st Chinaonrails Open Course 初试牛刀
1st Chinaonrails Open Course 初试牛刀1st Chinaonrails Open Course 初试牛刀
1st Chinaonrails Open Course 初试牛刀
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
Introduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on RailsIntroduction to Web Development with Ruby on Rails
Introduction to Web Development with Ruby on Rails
 
Encontro De Ti - Ruby e Rails
Encontro De Ti - Ruby e RailsEncontro De Ti - Ruby e Rails
Encontro De Ti - Ruby e Rails
 

Recently uploaded

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfROWELL MARQUINA
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneUiPathCommunity
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 

Recently uploaded (20)

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdfQMMS Lesson 2 - Using MS Excel Formula.pdf
QMMS Lesson 2 - Using MS Excel Formula.pdf
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyone
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 

Rails bestpractices.com

  • 1. rails-bestpractices.com www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 2. 为什么需要 Rails Best Practices? www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 3. 遵循 Rails Best Practices 写出更漂亮的 Rails 代码 成为更优秀的 Rails 程序员 得到更好的回报 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 4. 提供一个分享最佳实践的地方 提供一个获取最佳实践的地方 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 5. rails best practices 的故事 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 6. 很久很久以前。。。 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 7. 2009 年 10 月 24 日 上海 ihower 发表演讲 “ Rails Best Practices” http://www.slideshare.net/ihower/rails-best-practices www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 8. 2009 年 11 月 03 日 rails_best_practices 项目启动 http://github.com/flyerhzm/rails_best_practices www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 9. rails_best_practices 项目 根据 ihower 的演讲对 rails 代码进行静态分析,找出 bad smell www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 10. 2010 年 06 月 19 日 metric_fu 项目集成 rails_best_practices http://metric-fu.rubyforge.org www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 11. 2010 年 07 月 04 日 rails-bestpracitces.com 项目启动 http://rails-bestpractices.com www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 12. rails-bestpractices 从何开始? www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 13. Rails Template www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 14. Rails Template 创建 rails 应用的模板 扩展 rails 应用的模板 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 15. Rails Template www.ekohe.com Web Development & Graphic Design China 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 => “.”
  • 16. Rails Template www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Demo http://github.com/flyerhzm/rails3-template
  • 17. 我碰到的 Rails3 变动 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Generators Routes ActiveRecord 接口 Rspec2
  • 18. Rails3 Route www.ekohe.com Web Development & Graphic Design China 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 :implementation end match 'search' => 'search#show', :as => :search match 'page/:name' => 'pages#show', :as => :page root :to => &quot;posts#index&quot;
  • 19. Rails3 Route www.ekohe.com Web Development & Graphic Design China 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 &quot;/tags/:id/posts&quot; => redirect { |params, req| if req.query_string.index('page') # query_string remove controller and action &quot;/tags/#{params[:id]}?#{query_string}&quot; else &quot;/tags/#{params[:id]}?nav=posts&quot; end }
  • 20. Rails3 Scope www.ekohe.com Web Development & Graphic Design China 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
  • 21. 介绍一些非常实用的插件 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 22. Haml www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 一种 非常简洁的模板语言,是 ERB 的替代品 http://github.com/nex3/haml
  • 23. Haml (Before) www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development <div id=&quot;profile&quot;> <div class=&quot;left column&quot;> <div id=&quot;date&quot;><%= print_date %></div> </div> <div class=&quot;right column&quot;> <div id=&quot;email&quot;><%= current_user.email %></div> <div id=&quot;bio&quot;><%= h current_user.bio %></div> </div> </div>
  • 24. Haml (After) www.ekohe.com Web Development & Graphic Design China 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)
  • 25. Compass www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 一种 css 框架 使得 css 更易于创建和书写 http://github.com/chriseppstein/compass
  • 26. Compass www.ekohe.com Web Development & Graphic Design China 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 …)
  • 27. Compass 代码重用 www.ekohe.com Web Development & Graphic Design China 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%; } … }
  • 28. Compass 代码重用 www.ekohe.com Web Development & Graphic Design China 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; }
  • 29. Formtastic www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 简化 form 表单的创建 http://github.com/justinfrench/formtastic
  • 30. Formtastic www.ekohe.com Web Development & Graphic Design China 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 &quot;Register&quot;
  • 31. Inherited_resources www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 加速 controller 的开发 http://github.com/josevalim/inherited_resources
  • 32. Inherited_resources www.ekohe.com Web Development & Graphic Design China 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) } end end
  • 33. Authlogic authlogic-connect www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 注册、登录 使用 facebook 、 twitter 帐号登录 使用 OpenID 帐号登录
  • 34. Exception_notification www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 邮件通知服务器上的错误 非常重要,必不可少! http://github.com/rails/exception_notification
  • 35. 简单的优化工作 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 36. 前端优化 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 把所有的 stylesheets 组合为一个 all.css 把所有的 javsacripts 组合为一个 all.js 把所有的图片组合为一个 css_sprite.png
  • 37. 组合 css www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development = stylesheet_link_tag 'compiled/screen', 'compiled/layout', 'compiled/post', :cache => true
  • 38. 组合 javascript www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development = javascript_include_tag 'jquery', 'rails', 'application', :cache => true
  • 39. Css_sprite www.ekohe.com Web Development & Graphic Design China 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
  • 40. Css sprite www.ekohe.com Web Development & Graphic Design China 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
  • 41. SQL 查询优化 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 找出所有没有索引的数据表外键 http://github.com/eladmeidar/rails_indexes
  • 42. N+1 查询 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 找出 N+1 的查询以及多余的 eager loading http://github.com/flyerhzm/bullet
  • 43. N+1 查询 www.ekohe.com Web Development & Graphic Design China 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
  • 44. 测试 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development ngty 测试代码主要贡献者
  • 45. remarkable_activerecord www.ekohe.com Web Development & Graphic Design China 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
  • 46. remarkable_activerecord www.ekohe.com Web Development & Graphic Design China 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
  • 47. Module, module, module www.ekohe.com Web Development & Graphic Design China 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 end end
  • 48. Module, module, module www.ekohe.com Web Development & Graphic Design China 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 end end class Post < ActiveRecord::Base include UserOwnable end
  • 49. Module, module, module www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development describe Post do include RailsBestPractices::Macros should_be_user_ownable end 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 end end
  • 50. 分享你的 Rails 最佳实践 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 51. Rails 最佳实践问答 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 52. rails_best_practices gem demo www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 53. rails_best_practices 将会在本月底开始支持 rails3 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
  • 54. Q&A 谢谢 www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development