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

Rails 4.0

Leois
November 19, 2012

Rails 4.0

Leois

November 19, 2012
Tweet

Other Decks in Programming

Transcript

  1. Example: Invoice paid = 1 How do you set that

    flag in a RESTFULL way? RFC 5789
  2. Submitting paid=1 via PUT to /invoices/:id does not conform to

    HTTP semantics, because such request would not be sending a complete representation of the invoice for replacement. Example: Invoice paid = 1 How do you set that flag in a RESTFULL way? RFC 5789
  3. # application.rb config.action_mailer.async = true class WelcomeMailer < ActionMailer::Base self.async

    = true def welcome(id) @user = User.find(id) ... end end WelcomeMailer.welcome(@user.id).deliver
  4. # application.rb config.action_mailer.async = true class WelcomeMailer < ActionMailer::Base self.async

    = true def welcome(id) @user = User.find(id) ... end end WelcomeMailer.welcome(@user.id).deliver
  5. class TestJob def run puts "I am running!" end end

    Rails.queue.push(TestJob.new) => "I am running!"
  6. class TestJob def run puts "I am running!" end end

    Rails.queue.push(TestJob.new) => "I am running!"
  7. class Post < ActiveRecord::Base attr_accessible :title, :body end class PostController

    < ApplicationController def create @post = Post.create(params[:post]) end end
  8. class Post < ActiveRecord::Base attr_accessible :title, :body end class PostController

    < ApplicationController def create @post = Post.create(params[:post]) end end
  9. class Post < ActiveRecord::Base end class PostController < ApplicationController def

    create @post = Post.create(post_params) end private def post_params params.require(:post). permit(:title,:body) end end
  10. class Post < ActiveRecord::Base end class PostController < ApplicationController def

    create @post = Post.create(post_params) end private def post_params params.require(:post). permit(:title,:body) end end
  11. BCX::Application.routes.draw do resources :messages { resources :comments } resources :forwards

    { resources :comments } resources :uploads { resources :comments } resources :documents { resources :comments } resources :todos { resources :comments } end example: /messages/1/comments
  12. BCX::Application.routes.draw do resources :messages { resources :comments } resources :forwards

    { resources :comments } resources :uploads { resources :comments } resources :documents { resources :comments } resources :todos { resources :comments } end example: /messages/1/comments
  13. BCX::Application.routes.draw do concern :commentable do resources :comments end resources :messages,

    :forwards, :uploads, :documents, :todos, concerns: :commentable end
  14. BCX::Application.routes.draw do concern :commentable do resources :comments end resources :messages,

    :forwards, :uploads, :documents, :todos, concerns: :commentable end
  15. class MyController < ActionController::Base def index 100.times { response.stream.write "hello

    world\n" } response.stream.close end end http://www.youtube.com/watch?feature=player_embedded&v=g3O1PgvUzpo
  16. Layout <html> <head> <title>Boek uit? Vind je volgende fantasy boek</title>

    <%= stylesheet_link_tag "application", :media => "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <div class="header"> <div class="logo"> <%= link_to root_path do %> volgendboek<span>.nl</span> <% end %> </div> </div> <div class="wrapper"> <div id="main"> <%= yield %> </div> </div> </body> </html>
  17. Show view <title>Serie: <%= @series.title %></title> <div class="container"> <h1><%= @series.title

    %></h1> <div class="books"> <%= render @series.books %> </div> </div>
  18. Controller class BooksController < ApplicationController def show @book = Book.find(params[:id])

    @series = @book.series @offset = params[:offset] if request.xhr? render :layout => false end end end
  19. There are only two hard things in Computer Science: cache

    invalidation and naming things — Phil Karlton
  20. There are only two hard things in Computer Science: cache

    invalidation and naming things — Phil Karlton And understand key based cache — Leois Linka
  21. Pages caching class ProductsController < ActionController caches_page :index def index

    @products = Products.all end def create expire_page :action => :index end end
  22. Pages caching class ProductsController < ActionController caches_page :index def index

    @products = Products.all end def create expire_page :action => :index end end
  23. Action Caching class ProductsController < ActionController before_filter :authenticate caches_action :index

    def index @products = Product.all end def create expire_action :action => :index end end
  24. Action Caching class ProductsController < ActionController before_filter :authenticate caches_action :index

    def index @products = Product.all end def create expire_action :action => :index end end
  25. Fragment Caching <% Order.find_recent.each do |o| %> <%= o.buyer.name %>

    bought <%= o.product.name %> <% end %> <% cache “recent_products” do %> All available products: <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %> <% end %>
  26. Fragment Caching <% Order.find_recent.each do |o| %> <%= o.buyer.name %>

    bought <%= o.product.name %> <% end %> <% cache “recent_products” do %> All available products: <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %> <% end %>
  27. Sweepers class ProductSweeper < ActionController::Caching::Sweeper observe Product def after_save(product) expire_cache(product)

    end def after_destroy(product) expire_cache(product) end def expire_cache(product) expire_fragment 'recent_products‘ end end Controller cache_sweeper :product_sweeper, :only => [:create, :update, :destroy]
  28. Dependency structures projects/show.html.erb <% cache project do %> <p> All

    my todo lists:</p> <%= render project.todolists %> <% end %> todolists/_todolist.html.erb <% cache todolist %> <p> <%= todolist.name %>:</p> <%= render todolist.todos %> <% end %> Todos/_todo.html.erb <% cache todo do %> <p><%= todo.name %></p> <% end %> Project Todolist Todo class Project < ActiveRedord::Base end class Todolist< ActiveRedord::Base belongs_to :project end class Todo< ActiveRedord::Base belongs_to :todolist end
  29. Dependency structures projects/show.html.erb <% cache project do %> <p> All

    my todo lists:</p> <%= render project.todolists %> <% end %> todolists/_todolist.html.erb <% cache todolist %> <p> <%= todolist.name %>:</p> <%= render todolist.todos %> <% end %> Todos/_todo.html.erb <% cache todo do %> <p><%= todo.name %></p> <% end %> Project Todolist Todo class Project < ActiveRedord::Base end class Todolist< ActiveRedord::Base belongs_to :project, touch: true end class Todo< ActiveRedord::Base belongs_to :todolist, touch: true end
  30. Person.pluck(:id, :name) # SELECT people.id, people.name FROM people # =>

    [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']] Removal of Rails 2 finder syntax Example: Post.find(:all, …) Post.find(:first,...) Post.find(:last,...) Post.find_by name: 'Spartacus', rating: 4 Post.find_by "published_at < ?", 2.weeks.ago Post.find_by! name: 'Spartacus'
  31. New Deprecation Policy: Many of the above deprecations will still

    work in Rails 4.0 and included as gem dependencies, but will be removed in the jump to Rails 4.1. This means that the upgrade to Rails 4.1 may be more painful than the upgrade to Rails 4.0.