O que vem por aí com Rails 3

1,754 views

Published on

2º encontro de Frevo on Rails

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,754
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

O que vem por aí com Rails 3

  1. 1. FREVO ON RAILS GRUPO DE USUÁRIOS RUBY/RAILS DE PERNAMBUCO RUBY ON RAILS 3 O que vem de novo por aí... GUILHERME CARVALHO E LAILSON BANDEIRA
  2. 2. FREVO ON RAILS “No, no another Rails upgrade!”
  3. 3. FREVO ON RAILS Rails 2 + Merb Anunciado em dezembro de 2008
  4. 4. FREVO ON RAILS Versão 3.0.0.beta1 Versão 3.0.0.beta2 Versão 3.0.0.beta3 Versão 3.0.0.beta4 Funciona em Ruby 1.8.7 e 1.9.2 Versão 3.0.0.rc1 re . N en . e- ss ldr es ris le hi ec rp or r c pi y. t ad o te 3 fo all en age od sm of t g ins a No nt o Co
  5. 5. FREVO ON RAILS MAJOR UPGRADE
  6. 6. FREVO ON RAILS Mudanças arquiteturais importantes
  7. 7. FREVO ON RAILS RAILS
  8. 8. FREVO ON RAILS ACTIVE SUPPORT ACTIVE ACTION RECORD PACK RAILTIES ACTIVE ACTIVE MODEL RESOURCE ACTION MAILER
  9. 9. FREVO ON RAILS COMMAND INTERFACE
  10. 10. FREVO ON RAILS rails nome_da_app
  11. 11. FREVO ON RAILS rails new nome_da_app
  12. 12. FREVO ON RAILS script/server
  13. 13. FREVO ON RAILS rails server
  14. 14. FREVO ON RAILS rails s
  15. 15. FREVO ON RAILS script/generate controller nome_do_controlador
  16. 16. FREVO ON RAILS rails generate controller nome_do_controlador
  17. 17. FREVO ON RAILS rails g controller nome_do_controlador
  18. 18. FREVO ON RAILS console runner profiler dbconsole destroy plugin benchmarker
  19. 19. FREVO ON RAILS Gerenciamento de gems com o Bundler
  20. 20. FREVO ON RAILS ROUTING
  21. 21. FREVO ON RAILS Não se usa mais o map!
  22. 22. FREVO ON RAILS ActionController::Routing::Routes.draw do |map| map.resources :posts end
  23. 23. FREVO ON RAILS ActionController::Routing::Routes.draw do |map| resources :posts end
  24. 24. FREVO ON RAILS Resources e singular resources não mudaram
  25. 25. FREVO ON RAILS Namespaces e scopes
  26. 26. FREVO ON RAILS map.with_options(:namespace => “admin”) do |a| a.resources :photos end
  27. 27. FREVO ON RAILS namespace “admin” do resources :photos end
  28. 28. FREVO ON RAILS Scopes foram criados para auxiliar na organização
  29. 29. FREVO ON RAILS map.resources :photos, :member => {:preview => :get }
  30. 30. FREVO ON RAILS resources :photos do get :preview, on: :member end
  31. 31. FREVO ON RAILS Sai o método connect, entra o match
  32. 32. FREVO ON RAILS rake routes
  33. 33. FREVO ON RAILS RESPONDERS
  34. 34. FREVO ON RAILS class PostsController < ApplicationController def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } end end def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end ... end
  35. 35. FREVO ON RAILS class PostsController < ApplicationController respond_to :html, :xml, :json def index @posts = Post.all respond_with(@posts) end def show @post = Post.find(params[:id]) respond_with(@post) end ... end
  36. 36. FREVO ON RAILS É possível também criar outros responders
  37. 37. FREVO ON RAILS Three reasons to love Responders http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder
  38. 38. FREVO ON RAILS ACTION CONTROLLER
  39. 39. FREVO ON RAILS Gargalo de performance: roteamento + renderização
  40. 40. FREVO ON RAILS Separação de responsabilidades: Action Dispatch
  41. 41. FREVO ON RAILS Hierarquia de controladores
  42. 42. FREVO ON RAILS AbstractController::Base ActionController::Metal ActionController::Base
  43. 43. FREVO ON RAILS ACTIVE RECORD QUERY API
  44. 44. FREVO ON RAILS Nova API de consulta Active Relation
  45. 45. FREVO ON RAILS @posts = Post.find(:all, ! :conditions => ['created_at > ?', date])
  46. 46. FREVO ON RAILS @posts = Post.where(['created_at > ?', date])
  47. 47. FREVO ON RAILS Lazy loading
  48. 48. FREVO ON RAILS @posts = Post.where(['created_at > ?', date]) if only_published? ! @posts = @posts.where(:published => true) end
  49. 49. FREVO ON RAILS # @posts.all @posts.each do |p| ! ... end
  50. 50. FREVO ON RAILS group from joins order where having includes limit select offset
  51. 51. FREVO ON RAILS minimum maximum first all sum last count average calculate
  52. 52. FREVO ON RAILS Active Record Query Interface 3 http://m.onkey.org/2010/1/22/active-record-query-interface
  53. 53. FREVO ON RAILS Escopos também foram simplificados
  54. 54. FREVO ON RAILS class Post < ActiveRecord::Base named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false} end
  55. 55. FREVO ON RAILS class Post < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false) end
  56. 56. FREVO ON RAILS VALIDAÇÕES SEM MODELOS
  57. 57. FREVO ON RAILS Consequência da modularização Active Model
  58. 58. FREVO ON RAILS class Person include ActiveModel::Validations validates_presence_of :first_name, :last_name attr_accessor :first_name, :last_name end
  59. 59. FREVO ON RAILS person = Person.new person.valid? # false person.errors # {:first_name=>["can't be bl... p.first_name = 'John' p.last_name = 'Travolta' p.valid? # true
  60. 60. FREVO ON RAILS Make Any Ruby Object Feel Like AR http://yehudakatz.com/2010/01/10/activemodel-make-any- ruby-object-feel-like-activerecord/
  61. 61. FREVO ON RAILS VALIDADORES CUSTOMIZADOS
  62. 62. FREVO ON RAILS Agora é possível criar validadores que podem ser reusados
  63. 63. FREVO ON RAILS Mais um resultado do desacoplamento
  64. 64. FREVO ON RAILS module ActiveModel module Validations class CepValidator < EachValidator FORMATO_CEP = /d{5}-d{3}/ def initialize(options) super(options) end def validate_each(record, attribute, value) unless valid?(value) record.errors[attribute] = 'não é válido' end end def valid?(value) FORMATO_CEP =~ value end end end end
  65. 65. FREVO ON RAILS require "#{Rails.root}/lib/validadores/cep_validator" class Person < ActiveRecord::Base validates :cep, cep: true end
  66. 66. FREVO ON RAILS ACTION MAILER
  67. 67. FREVO ON RAILS Sai TMail, entra Mail
  68. 68. FREVO ON RAILS Uma nova casa para os mailers ./app/mailers/nome_do_mailer
  69. 69. FREVO ON RAILS Criação de defaults para diminuir duplicação de código
  70. 70. FREVO ON RAILS class UserMailer < ActionMailer::Base ! def welcome(user) ! ! recipients user.email ! ! from “email@example.com” ! ! subject “Welcome to my site” ! ! body { :user => user } ! end end
  71. 71. FREVO ON RAILS class UserMailer < ActionMailer::Base ! default from: “email@example.com” ! def welcome(user) ! ! @user = user ! ! mail(to: user.email, ! ! ! subject: “Welcome to my site”) ! end end
  72. 72. FREVO ON RAILS Anexos muito mais fáceis
  73. 73. FREVO ON RAILS class UserMailer < ActionMailer::Base ! default from: “email@example.com” ! def welcome(user) ! ! @user = user ! ! attachments[“hello.gif”] = File.read(‘...’) ! ! mail(to: user.email, ! ! ! subject: “Welcome to my site”) ! end end
  74. 74. FREVO ON RAILS class UserMailer < ActionMailer::Base ! default from: “email@example.com” ! def welcome(user) ! ! @user = user ! ! attachments.inline[“logo.png”] = ! ! ! File.read(...) ! ! mail(to: user.email, ! ! ! subject: “Welcome to my site”) ! end end
  75. 75. FREVO ON RAILS
  76. 76. FREVO ON RAILS DEMO TIME
  77. 77. FREVO ON RAILS RECURSOS
  78. 78. FREVO ON RAILS Rails Dispatch http://railsdispatch.com/
  79. 79. FREVO ON RAILS Rails Guides Edge http://guides.rails.info/
  80. 80. FREVO ON RAILS Engine Yard Blog http://www.engineyard.com/blog
  81. 81. FREVO ON RAILS Dive into Rails 3 Screencasts http://rubyonrails.org/screencasts/rails3
  82. 82. FREVO ON RAILS Railscasts http://railscasts.com/
  83. 83. FREVO ON RAILS Agile Web Development with Rails (4th ed.) http://pragprog.com/titles/rails4/agile-web-development-with-rails
  84. 84. FREVO ON RAILS The Rails 3 Way http://my.safaribooksonline.com/9780132480345
  85. 85. FREVO ON RAILS FREVO ON RAILS GRUPO DE USUÁRIOS RUBY DE PERNAMBUCO

×