Rails bestpractices.com

  • Published on
    06-May-2015

  • View
    2.688

  • Download
    1

Embed Size (px)

DESCRIPTION

rails-bestpractices.complugins/gems

Transcript

<ul><li>1.rails-bestpractices.com www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development</li></ul> <p>2. Rails Best Practices? www.ekohe.com Web Development &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 4. www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 5. rails best practices www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 6. www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 12. rails-bestpractices www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 13. Rails Template www.ekohe.com Web Development &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 15. Rails Template www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development run echo TOD &gt; README gem haml, &gt;= 3.0.13 plugintypus, :git =&gt; git://github.com/fesplugas/typus.git generate model User login:string email:string route root :to =&gt; 'home#show' rake db:migrate git :add =&gt; . 16. Rails Template www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development resources :posts do get :archive, :on =&gt; :collection resources :comments, :only =&gt; :create resources :votes, :only =&gt; [:create, :destroy] resource :implementation end match 'search' =&gt; 'search#show', :as =&gt; :search match 'page/:name' =&gt; 'pages#show', :as =&gt; :page root :to =&gt; "posts#index" 19. Rails3 Route www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development /tags/rails/posts =&gt; /tags/rails?nav=posts /tags/rails/posts?page=2 =&gt; /tags/rails?nav=posts&amp;page=2 /tags/rails/posts?action=show&amp;controller=tags&amp;nav=posts&amp;page=2 =&gt; /tags/rails?nav=posts&amp;page=2 match "/tags/:id/posts" =&gt; 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 } 20. Rails3 Scope www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development scope :implemented, where(:implemented =&gt; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 22. Haml www.ekohe.com Web Development &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development </p> <div> <div> </div> <div> </div> </div> <p> 24. Haml(After) www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 =&gt; 'Username' = form.input :email = form.input :password = form.input :password_confirmation = form.buttons do = form.submit "Register" 31. Inherited_resources www.ekohe.com Web Development &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development class AnswersController &lt; InheritedResources::Base before_filter :require_user, :only =&gt; [: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. Authlogicauthlogic-connect www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 36. www.ekohe.com Web Development &amp; 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 &amp; 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 =&gt; true 38. javascript www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development = javascript_include_tag 'jquery', 'rails', 'application', :cache =&gt; true 39. Css_sprite www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 =&gt; [:user] Add to your finder: :include =&gt; [:user] N+1 Query method call stack 44. www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development ngty 45. remarkable_activerecord www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development has_one :implementation, :dependent =&gt; :destroy has_many :posts, :dependent =&gt; :destroy validates_presence_of :title, :body validates_uniqueness_of :title 46. remarkable_activerecord www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development should_have_one :implementation, :dependent =&gt; :destroy should_have_many :posts, :dependent =&gt; :destroy should_validate_presence_of :title, :body should_validate_uniqueness_of :title 47. Module, module, module www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development class Post &lt; ActiveRecord::Base belongs_to user, :counter_cache =&gt; true def belongs_to?(user) self.user == user end end 48. Module, module, module www.ekohe.com Web Development &amp; 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 =&gt; true end end def belongs_to?(user) self.user == user end end class Post &lt; ActiveRecord::Base include UserOwnable end 49. Module, module, module www.ekohe.com Web Development &amp; 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 =&gt; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 51. Rails www.ekohe.com Web Development &amp; 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 &amp; 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 &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development 54. Q&amp;A www.ekohe.com Web Development &amp; Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development </p>