• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Rails 2.3, 3.0 and 3.1 - RubyConfBR - 26oct2010

on

  • 1,485 views

 

Statistics

Views

Total Views
1,485
Views on SlideShare
1,485
Embed Views
0

Actions

Likes
0
Downloads
13
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • Rails 3 is out two months ago and you are probably aware of most of its features. Today, I won&apos;t describe how to use such features, but instead I will give you the concepts and reflections behind some internal changes. And to do that, we will need to look at Rails 2.3 several times, to help us understand the differences. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • Each object implement few methods, so it is very easy to replace them. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • Rails 2.3 hacked into Rubygems to allow gems to work as engines so things like app/ could work. <br />
  • <br />
  • <br />
  • <br />
  • Extra paths = db/migrate, public and vendor/plugins <br />
  • Extra paths = db/migrate, public and vendor/plugins <br />
  • Rails 3 is definitely amazing and brought several improvements but it could be even better! For example, while everyone loves ARel, it reduced ActiveRecord performance a bit. So we need to work on that for future releases. And this is why we are going to talk about Rails 3.1. <br />
  • <br />
  • As DHH already did in on latest Rails Conf, from now we will have spoilers about the future of Rails. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • Client cache now works, but what about server side? If on every first request from a client we have to render a file, it will be slow. Once we render a .js file for one person, it is available for everyone. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • This means all the work we have done for client side caching of JS will just work on Server Side as well. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • There is no need to learn one extra thing. Learn just HTTP Cache and you will be fine. <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • If every time we get a record, we store it in the hash, in a few requests we will have the whole database in our hash. <br />
  • If every time we get a record, we store it in the hash, in a few requests we will have the whole database in our hash. <br />
  • WeakHash is a hash where its key and/or values can be garbage collected. <br />
  • <br />
  • <br />
  • <br />
  • Experienced Rails developers knows that this is a bad practice, but why should it? <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • This may be in Rails 3.1, maybe 3.2 or maybe never, that depends on you! <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Rails 2.3, 3.0 and 3.1 - RubyConfBR - 26oct2010 Rails 2.3, 3.0 and 3.1 - RubyConfBR - 26oct2010 Presentation Transcript

  • Rails 2.3, 3.0 e 3.1 Passado, Presente e Futuro
  • Eu sou José Valim @josevalim
  • Eu trabalho na blog.plataformatec.com.br
  • Core Team Member
  • Rails 3.0 Presente
  • Rails 3.0 Modulos! Modulos! Modulos!
  • Active Model
  • Active Record & Active Resource
  • ActiveModel::AttributeMethods ActiveModel::Callbacks ActiveModel::Dirty ActiveModel::MassAssignmentSecurity ActiveModel::Naming ActiveModel::Observing ActiveModel::Serialization ActiveModel::Translation ActiveModel::Validation
  • Abstract Controller
  • Action Controller & Action Mailer
  • AbstractController::AssetPaths AbstractController::Callbacks AbstractController::Helpers AbstractController::Layouts AbstractController::Logger AbstractController::Rendering AbstractController::Translation AbstractController::UrlFor AbstractController::ViewPaths
  • Opa, tem mais...
  • ActionController::Metal
  • ApplicationController
  • ActionController::Base ApplicationController
  • ActionController::Metal ActionController::Base ApplicationController
  • AbstractController::Base ActionController::Metal ActionController::Base ApplicationController
  • class APIController < ActionController::Metal include ActionController::Rendering include ActionController::MimeResponds respond_to :json end
  • Rails 3.0 Objetos! Objetos! Objetos!
  • Rails 3 cria mais objetos e compartilha melhor responsabilidades
  • 0.x Render Controller View Rendered template
  • A view é responsável por parsear parâmetros, buscar e renderizar templates
  • 1.0 Render Controller View Rendered Template Template template handler
  • A view é responsável por parsear parâmetros e buscar templates
  • 2.2 Render Find template View Controller View paths Template Rendered Template Template template handler
  • A view é responsável por parsear parâmetros
  • Os view paths armazenam caminhos para o lesystem e buscam templates
  • 3.0 Render + Find Find Lookup Context template template View Controller View Resolver paths Template Template Rendered Template Template template handler
  • • Lookup context armazena “detalhes” • A view apenas parseia parâmetros • View paths armazenam resolvers • Resolvers buscam templates • Handlers renderizam templates
  • Simples contratos entre APIs
  • TemplateHandler#call
  • HAML, Builder, ERb...
  • Resolver# nd_all
  • Resolvers permitem buscar templates em qualquer local, incluindo o nosso banco de dados
  • Rails 3.0 Agnosticismo!
  • Geradores + Active Model + Rails::Railtie
  • Geradores con g.generators.orm :datamapper con g.generators.test_framework :rspec con g.generators.template_engine :haml
  • Active Model User.model_name user.persisted? user.valid? user.errors user.to_key user.to_param
  • Rails::Railtie class ActiveRecord::Railtie < Rails::Railtie config.generators.orm :active_record, :migration => true, :timestamps => true rake_tasks do load "active_record/railties/databases.rake" end initializer "active_record.initialize_timezone" do ActiveRecord.time_zone_aware_attributes = true ActiveRecord.default_timezone = :utc end end
  • Rails 3.0 Applications, engines & railties!
  • class MyEngine < Rails::Engine end
  • Engine = Railtie + Paths
  • app/controllers app/helpers app/mailers app/models app/views con g/initializers/* con g/routes.rb con g/locales/* lib lib/tasks/*
  • class MyApp < Rails::Application end
  • Application = Engine + Extra paths + Boot
  • Herança no Ruby #FTW
  • Rails 3.1 OMG, o futuro!
  • SPOILER ALERT
  • Yehuda, I am your father
  • Rails 3.1 Melhores Engines
  • 3.0 Application = Engine + Extra paths + Boot
  • 3.1 Application = Engine + Boot
  • db/migrate public vendor/plugins
  • rake spree:install:migrations db/migrate public vendor/plugins
  • rake spree:install:assets rake spree:install:migrations db/migrate public vendor/plugins
  • Engines são aplicações rack com o seu próprio router e middleware stack
  • mount Rails::Admin => “/admin”
  • a partir do engine main_app.posts_path rails_admin.posts_path a partir da aplicação
  • Geradores e muito mais!
  • Rails 3.1 Assets revolution
  • 3.0 public/{javascripts,stylesheets}
  • 3.1 app/assets/ {javascripts,stylesheets}
  • #1 Adicione uma nova rota match "javascripts/*path", :to => "javascripts#index"
  • #2 Crie o controller e a action class JavascriptsController < ActionController::Base self.view_paths = ["app/assets"] def index render :template => params[:path] end end
  • #3 Coloque arquivos em app/assets # app/assets/javascripts/foo.js.erb alert(“<%= :omg %>”)
  • JavascriptsController.use Minify
  • Problemas: Cache no cliente e no servidor
  • Client Side Cache
  • 3.0 Cache-Control: public Expires: <%= 1.year.from_now %>
  • 3.0 /javascripts/foo.js?1271462400 File.stat("javascripts/foo.js ").mtime.to_i
  • Isso não funcionará no Rails 3.1 por dois motivos
  • #1 Assume que arquivos javascripts estão no lesystem
  • Render + Find Find Lookup Context template template View Controller View Resolver paths Template Template Rendered Template Template template handler
  • #2 Assume que arquivos javascripts não possuem dependências
  • # app/assets/javascripts/foo.js.erb alert(“<%= render “bar” %>”)
  • 3.1 /javascripts/foo.js?1271462400 File.stat("javascripts/foo.js ").mtime.to_i
  • 3.1 /javascripts/foo.js?a64b85c84f ./RELEASE
  • Server Side Cache
  • Rack::Cache
  • Rack::Cache é um middleware que atua como caching proxy para qualquer aplicação Rack
  • Internet Web server Rack::Cache Rack::Cache App App Storage
  • Internet Rack::Cache App
  • Internet Rack::Cache App /javascripts/foo.js
  • Internet Rack::Cache App /javascripts/foo.js /javascripts/foo.js
  • Internet Rack::Cache App /javascripts/foo.js /javascripts/foo.js Cache-Control: public alert(“omg”);
  • Internet Rack::Cache App /javascripts/foo.js /javascripts/foo.js Cache-Control: public Cache-Control: public alert(“omg”); alert(“omg”);
  • Internet Rack::Cache App /javascripts/foo.js /javascripts/foo.js Cache-Control: public Cache-Control: public alert(“omg”); alert(“omg”); /javascripts/foo.js
  • Internet Rack::Cache App /javascripts/foo.js /javascripts/foo.js Cache-Control: public Cache-Control: public alert(“omg”); alert(“omg”); /javascripts/foo.js Cache-Control: public alert(“omg”);
  • Rack::Cache usa as diretivas do HTTP Cache (RFC2616)
  • Internet Rack::Cache App
  • Internet Rack::Cache App /posts/1
  • Internet Rack::Cache App /posts/1 /posts/1
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Last-Modi ed: 13, Oct 10 <html>...
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Cache-Control: public Last-Modi ed: 13, Oct 10 Last-Modi ed: 13, Oct 10 <html>... <html>...
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Cache-Control: public Last-Modi ed: 13, Oct 10 Last-Modi ed: 13, Oct 10 <html>... <html>... /posts/1
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Cache-Control: public Last-Modi ed: 13, Oct 10 Last-Modi ed: 13, Oct 10 <html>... <html>... /posts/1 If-Modi ed-Since: 13, Oct 10 /posts/1
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Cache-Control: public Last-Modi ed: 13, Oct 10 Last-Modi ed: 13, Oct 10 <html>... <html>... /posts/1 If-Modi ed-Since: 13, Oct 10 /posts/1 304 Not Modi ed
  • Internet Rack::Cache App /posts/1 /posts/1 Cache-Control: public Cache-Control: public Last-Modi ed: 13, Oct 10 Last-Modi ed: 13, Oct 10 <html>... <html>... /posts/1 If-Modi ed-Since: 13, Oct 10 /posts/1 Cache-Control: public Last-Modi ed: 13, Oct 10 304 Not Modi ed <html>...
  • 3.0 caches_page
  • caches_page, pede pra sair
  • 3.1 caches_page
  • Aprendam HTTP Cache agora
  • Rails 3.1 Assets revolution Vol. 2
  • Rails 3.1 terá um gerenciamento de assets mais robusto
  • con g.js_bundle :omg, :with => %w(foo bar baz)
  • Rails 3.1 incluirá sprockets e scss
  • # foo.js.sp //= require <bar>
  • # foo.css.scss background: sprite-image(“icons/home.png”)
  • Rails 3.1 Identity Map
  • “Um ‘identity map’ é um padrão de acesso ao banco de dados utilizado para melhorar a performance através de um cache na memória prevenindo a busca do mesmo objeto no banco de dados” - Wikipedia
  • 3.0 post.comments. rst.post != post
  • 3.1 post.comments. rst.post == post
  • #1 Armazene todos os objetos em um hash
  • #2 Remova objetos inválidos e destruídos do hash
  • #3 Veja os seus servidores morrerem sem memória
  • #4 Use um WeakHash
  • map[:Post][1] #<Post id:1>
  • map[:Post][1] #<Post id:1>
  • #5 Aproveite :D
  • Account.first.posts.each do |post| post.account.email post.comments.each do |comment| comment.post.account.email end end
  • 1 account com 50 posts, cada post tem 10 comments
  • Alocação de objetos
  • Alocação de objetos Without Identity Map Warmup: 555077 allocations | 19.18 MB Actual: 554746 allocations | 19.11 MB
  • Alocação de objetos Without Identity Map Warmup: 555077 allocations | 19.18 MB Actual: 554746 allocations | 19.11 MB With Identity Map Warmup: 118825 allocations | 3.53 MB Actual: 17924 allocations | 0.23 MB
  • Rails 3.1 Migrations mais simples
  • 3.0 def self.up create_table :users do |t| t.string :name end end def self.down drop_table :users end
  • 3.1 def change create_table :users do |t| t.string :name end end
  • Futuro == dezembro ?
  • Star Wars http://www.motifake.com/104735.html DHH http://www.flickr.com/photos/x180/2823889501/ Nom nom nom http://www.flickr.com/photos/linniekin/2768221518/
  • Perguntas? José Valim blog.plataformatec.com @josevalim
  • Perguntas? ID blog twitter José Valim blog.plataformatec.com @josevalim