Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails 4

Rails 4

975ff6c8166f6f32889f004a904ffa39?s=128

Oursky Limited

October 09, 2013
Tweet

Transcript

  1. Catch up with Rails 4 Edwin Kwok Wednesday, 9 October,

    13
  2. No more Ruby 1.8 Ruby >= 1.9.3 Wednesday, 9 October,

    13
  3. strong_parameters class Post < ActiveRecord::Base attr_accessible :title, :body end class

    PostsController < ApplicationController def create @post = Post.create(params[:post]) end end Wednesday, 9 October, 13
  4. strong_parameters class Post < ActiveRecord::Base end class PostsController < ApplicationController

    def create @post = Post.create(post_params) end private def post_params params.require(:post).permit(:title, :body) end end Wednesday, 9 October, 13
  5. strong_parameters attr_accessible and attr_protected removed and extracted to the protected_attributes

    gem Reason: pass responsibility to controllers instead of models def post_params if @current_user.admin? params.require(:post).permit(:title, :body, :visible) else params.require(:post).permit(:title, :body) end end Wednesday, 9 October, 13
  6. Before_action = Before_filter just renaming Reason: better understanding no filtering

    is required in before_filter Wednesday, 9 October, 13
  7. Turbolinks Traditional way <html> <head> <title>Home</title> </head> <body> Welcome home!

    <a href=”/blog”>Blog</a> </body> </html> <html> <head> <title>Blog</title> </head> <body> <ul> <li>Blog 1</li> <li>Blog 2</li> </ul> </body> </html> Wednesday, 9 October, 13
  8. Turbolinks with Turbolinks <html> <head> <title>Home</title> </head> <body> Welcome home!

    <a href=”/blog”>Blog</a> </body> </html> <html> <head> <title>Blog</title> </head> <body> <ul> <li>Blog 1</li> <li>Blog 2</li> </ul> </body> </html> Wednesday, 9 October, 13
  9. Turbolinks The more CSS and JavaScript you have, the bigger

    the benefit of not throwing away the browser instance and recompiling all of it for every page Problem: https:/ /github.com/kossnocorp/jquery.turbolinks “page:load” event $(function(){ console.log(“Document ready”); }); Wednesday, 9 October, 13
  10. Russian Doll Caching Cache Digests https:/ /github.com/rails/cache_digests Wednesday, 9 October,

    13
  11. Caching: The problem # app/views/projects/show.html.erb <% cache [ “v1”, project

    ] do %> <h1>All documents</h1> <%= render project.documents %> <% end %> # app/views/documents/_documents.html.erb <% cache [ “v1”, document ] do %> My document: <%= document.name %> <% end %> Wednesday, 9 October, 13
  12. Caching: More Difficult # app/views/projects/show.html.erb <% cache [ “v1”, project

    ] do %> <h1>All documents</h1> <%= render project.documents %> <h1>All todolists</h1> <%= render project.todolists %> <% end %> # app/views/documents/_documents.html.erb <% cache [ “v1”, document ] do %> My document: <%= document.name %> <%= render document.comments %> <% end %> # app/views/todolists/_todolist.html.erb <% cache [ "v1", todolist ] do %> My todolist: <%= todolist.name %> <%= render todolist.comments %> <% end %> # app/views/comments/_comment.html.erb <% cache [ "v1", comment ] do %> My comment: <%= comment.body %> <% end %> Wednesday, 9 October, 13
  13. Caching: Solution # app/views/projects/show.html.erb <% cache project do %> ...

    <% end %> # app/views/documents/_documents.html.erb <% cache document do %> ... <% end %> # app/views/todolists/_todolist.html.erb <% cache todolist do %> ... <% end %> # app/views/comments/_comment.html.erb <% cache comment do %> ... <% end %> Wednesday, 9 October, 13
  14. $ rake cache_digests:nested_dependencies\ TEMPLATE=projects/show [ { "documents/document": [ "comments/comment" ]

    }, { "todolists/todolist": [ "comments/comment" ] } ] How it works MD5 the template file itself and all of its dependencies. It'll change if you change either the template or any of the dependencies, and thus allow the cache to expire automatically. Wednesday, 9 October, 13
  15. Caching Deprecations Action caching removed and extracted to actionpack-action_caching gem

    Page caching removed and extracted to actionpack-page_caching gem Wednesday, 9 October, 13
  16. ActionController::Live class MyController < ApplicationController include ActionController::Live def index 100.times

    { response.stream.write “Hello\n” } response.stream.close end end Need correctly configured web-server thin/rainbows/puma Wednesday, 9 October, 13
  17. PATCH verb class PostsController < ApplicationController # PATCH/PUT /posts/1 #

    PATCH/PUT /posts/1.json def update @post = Post.find(params[:id]) end end Better naming for the action put - overwrite the existing record patch - update partial information API requests can use PUT or PATCH Wednesday, 9 October, 13
  18. ActiveRecord # Rails 3 > Post.all.class Array # Rails 4

    > Post.all.class ActiveRecord::Relation > Post.all.to_a.class Array > Post.scoped DEPRECATION WARNING: Use Post.all instead > Post.none #<ActiveRecord::Relation []> Wednesday, 9 October, 13
  19. ActiveRecord # Rails 3 > Post.first SELECT “posts”.* FROM “posts”

    LIMIT 1 # Rails 4 > Post.first SELECT “posts”.* FROM “posts” ORDER BY “posts”.”id” ASC LIMIT 1 > Post.where.not category_ids: [5,7] SELECT “posts”.* FROM “posts” WHERE (“posts”.”category_id” NOT IN (5,7)) Wednesday, 9 October, 13
  20. Goodbye Rails Finder find_by_... where(...).first find_all_by_... where(...) find_last_by_... where(...).last find_or_initialize_by_...

    where(...).first_or_initi alize find_or_create_by_... where(...).first_or_creat e need gem “activerecord-deprecated_finders” Wednesday, 9 October, 13
  21. ActiveModel::Model <%= form_for @contact do |f| %> <%= f.label :first_name

    %> <%= f.text_field :first_name %> <%= f.label :last_name %> <%= f.text_field :last_name %> <%= f.label :subject %> <%= f.text_field :subject %> <%= f.label :message %> <%= f.text_area :message, rows: 5, cols: 60 %> <%= f.submit 'Send Inquiry'%> <% end %> Wednesday, 9 October, 13
  22. ActiveModel::Model class Contact include ActiveModel::Model attr_accessor :first_name, :last_name, :subject, :message

    validates_presence_of :first_name, :last_name, :message validates_length_of :message, minimum: 10 end Wednesday, 9 October, 13
  23. No Plugin folder Not support plugin folder under vendor Use

    gem or migrate them into lib Wednesday, 9 October, 13
  24. Gem Extraction ActiveResource Observer Reason: too less people use them

    Wednesday, 9 October, 13
  25. Routing Concerns # routes.rb concern :commentable do |options| resources :comments,

    options end concern :image_attachable do resources :images, only: index end resources :posts, concerns: [:commentable, :image_attachable] do resource :video, concerns: :commentable end Wednesday, 9 October, 13
  26. Reference http:/ /blog.wyeworks.com/2012/11/13/rails-4- compilation-links/ Wednesday, 9 October, 13