Share
Embed
Transcript
44 slides
-
Example: Invoice paid = 1 How do you set that
flag in a RESTFULL way? RFC 5789 -
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 -
# 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 -
# 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 -
class TestJob def run puts "I am running!" end end
Rails.queue.push(TestJob.new) => "I am running!" -
class TestJob def run puts "I am running!" end end
Rails.queue.push(TestJob.new) => "I am running!" -
class Post < ActiveRecord::Base attr_accessible :title, :body end class PostController
< ApplicationController def create @post = Post.create(params[:post]) end end -
class Post < ActiveRecord::Base attr_accessible :title, :body end class PostController
< ApplicationController def create @post = Post.create(params[:post]) end end -
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 -
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 -
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 -
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 -
BCX::Application.routes.draw do concern :commentable do resources :comments end resources :messages,
:forwards, :uploads, :documents, :todos, concerns: :commentable end -
BCX::Application.routes.draw do concern :commentable do resources :comments end resources :messages,
:forwards, :uploads, :documents, :todos, concerns: :commentable end -
class MyController < ActionController::Base def index 100.times { response.stream.write "hello
world\n" } response.stream.close end end . -
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 -
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> -
Show view <title>Serie: <%= @series.title %></title> <div class="container"> <h1><%= @series.title
%></h1> <div class="books"> <%= render @series.books %> </div> </div> -
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 -
There are only two hard things in Computer Science: cache
invalidation and naming things — Phil Karlton -
There are only two hard things in Computer Science: cache
invalidation and naming things — Phil Karlton And understand key based cache — Leois Linka -
Pages caching class ProductsController < ActionController caches_page :index def index
@products = Products.all end def create expire_page :action => :index end end -
Pages caching class ProductsController < ActionController caches_page :index def index
@products = Products.all end def create expire_page :action => :index end end -
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 -
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 -
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 %> -
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 %> -
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] -
Key based cache expiration Never update, never expire Object based
key calculation Cache garbage -
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 -
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 -
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' -
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.