O que vem por aí com Rails 3

Preview:

DESCRIPTION

2º encontro de Frevo on Rails

Citation preview

RUBY ON RAILS 3O que vem de novo por aí...

FREVO ON RAILSGRUPO DE USUÁRIOS RUBY/RAILS DE PERNAMBUCO

GUILHERME CARVALHO E LAILSON BANDEIRA

FREVO ON RAILS

“No, no another Rails upgrade!”

FREVO ON RAILS

Rails 2 + MerbAnunciado em dezembro de 2008

FREVO ON RAILS

Versão 3.0.0.beta4Funciona em Ruby 1.8.7 e 1.9.2

Contain

s sm

all p

iece

s.

Not good fo

r child

ren

of a

ge 3 o

r les

s. Not

ente

rpris

e-re

ady.

Versão 3.0.0.beta3

Versão 3.0.0.beta2

Versão 3.0.0.beta1

Versão 3.0.0.rc1

FREVO ON RAILS

MAJOR UPGRADE

FREVO ON RAILS

Mudanças arquiteturais importantes

FREVO ON RAILS

RAILS

FREVO ON RAILS

ACTIVERECORD

ACTIVESUPPORT

ACTIVEMODEL

ACTIONPACK

ACTIVERESOURCE

ACTIONMAILER

RAILTIES

FREVO ON RAILS

COMMAND INTERFACE

FREVO ON RAILS

rails nome_da_app

FREVO ON RAILS

rails new nome_da_app

FREVO ON RAILS

script/server

FREVO ON RAILS

rails server

FREVO ON RAILS

rails s

FREVO ON RAILS

script/generate controller nome_do_controlador

FREVO ON RAILS

rails generate controller nome_do_controlador

FREVO ON RAILS

rails g controller nome_do_controlador

FREVO ON RAILS

console

dbconsole

plugin

destroy

benchmarker

profiler

runner

FREVO ON RAILS

Gerenciamento de gems com o Bundler

FREVO ON RAILS

ROUTING

FREVO ON RAILS

Não se usa mais o map!

FREVO ON RAILS

ActionController::Routing::Routes.draw do |map| map.resources :postsend

FREVO ON RAILS

ActionController::Routing::Routes.draw do |map| resources :postsend

FREVO ON RAILS

Resources e singular resources não mudaram

FREVO ON RAILS

Namespaces e scopes

FREVO ON RAILS

map.with_options(:namespace => “admin”) do |a| a.resources :photosend

FREVO ON RAILS

namespace “admin” do resources :photosend

FREVO ON RAILS

Scopes foram criados para auxiliar na organização

FREVO ON RAILS

map.resources :photos, :member => {:preview => :get }

FREVO ON RAILS

resources :photos do get :preview, on: :memberend

FREVO ON RAILS

Sai o método connect, entra o match

FREVO ON RAILS

rake routes

FREVO ON RAILS

RESPONDERS

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

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

FREVO ON RAILS

É possível também criar outros responders

FREVO ON RAILS

Three reasons to love Respondershttp://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder

FREVO ON RAILS

ACTION CONTROLLER

FREVO ON RAILS

Gargalo de performance:roteamento + renderização

FREVO ON RAILS

Separação de responsabilidades:Action Dispatch

FREVO ON RAILS

Hierarquia de controladores

FREVO ON RAILS

ACTIVE RECORDQUERY API

FREVO ON RAILS

Nova API de consultaActive Relation

FREVO ON RAILS

@posts = Post.find(:all,! :conditions => ['created_at > ?', date])

FREVO ON RAILS

@posts = Post.where(['created_at > ?', date])

FREVO ON RAILS

Lazy loading

FREVO ON RAILS

@posts = Post.where(['created_at > ?', date])

if only_published?! @posts = @posts.where(:published => true)end

FREVO ON RAILS

# @posts.all@posts.each do |p|! ...end

FREVO ON RAILS

from

order

select

wherehaving

joins

group

includeslimit

offset

FREVO ON RAILS

minimum

firstall

sumcount

maximum

averagecalculate

last

FREVO ON RAILS

Active Record Query Interface 3http://m.onkey.org/2010/1/22/active-record-query-interface

FREVO ON RAILS

Escopos também foram simplificados

FREVO ON RAILS

class Post < ActiveRecord::Base named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false}end

FREVO ON RAILS

class Post < ActiveRecord::Base scope :published, where(:published => true) scope :unpublished, where(:published => false)end

FREVO ON RAILS

VALIDAÇÕES SEMMODELOS

FREVO ON RAILS

Consequência da modularizaçãoActive Model

FREVO ON RAILS

class Person include ActiveModel::Validations validates_presence_of :first_name, :last_name attr_accessor :first_name, :last_nameend

FREVO ON RAILS

person = Person.newperson.valid? # falseperson.errors # {:first_name=>["can't be bl...p.first_name = 'John'p.last_name = 'Travolta'p.valid? # true

FREVO ON RAILS

VALIDADORESCUSTOMIZADOS

FREVO ON RAILS

Agora é possível criar validadores que podem ser reusados

FREVO ON RAILS

Mais um resultado do desacoplamento

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 endend

FREVO ON RAILS

require "#{Rails.root}/lib/validadores/cep_validator"

class Person < ActiveRecord::Base validates :cep, cep: trueend

FREVO ON RAILS

ACTION MAILER

FREVO ON RAILS

Sai TMail, entra Mail

FREVO ON RAILS

Uma nova casa para os mailers./app/mailers/nome_do_mailer

FREVO ON RAILS

Criação de defaults para diminuir duplicação de código

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 }! endend

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”)! endend

FREVO ON RAILS

Anexos muito mais fáceis

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”)! endend

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”)! endend

FREVO ON RAILS

FREVO ON RAILS

DEMO TIME

FREVO ON RAILS

RECURSOS

FREVO ON RAILS

Dive into Rails 3 Screencastshttp://rubyonrails.org/screencasts/rails3

FREVO ON RAILS

Agile Web Development with Rails (4th ed.)http://pragprog.com/titles/rails4/agile-web-development-with-rails

FREVO ON RAILS

The Rails 3 Wayhttp://my.safaribooksonline.com/9780132480345

FREVO ON RAILS

FREVO ON RAILSGRUPO DE USUÁRIOS RUBY DE PERNAMBUCO

Recommended