Transcript

Introduo ao Ruby on Rails

Introduo ao Ruby on Rails

Juan Maiz Lulkin Flores da Cunha

Introduo ao Ruby on Rails > Palestrante

WWR person/9354vice-campeo mundial dedefender of the favicon

Histria

David Heinemeier Hanson (DHH)Martin Fowler37Signals

Introduo ao Ruby on Rails > Histria

Ruby on Rails

RubyModel View ControllerPrincpiosComunidadeAnd so much more

Introduo ao Ruby on Rails > Ruby on Rails

Ruby

Smalltalk elegncia conceitualPython facilidade de usoPerl pragmatismo

Introduo ao Ruby on Rails > Ruby on Rails > Ruby

Smalltalk

1 + 1 => 21.+(1) => 2 1.send('+',1) => 2

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Smalltalk

Python

for 1 in 1..10 puts iend 1 2 3 4 5 6 7

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Python

Perl

ls = %w{smalltalk python perl} => ["smalltalk", "python", "perl"]ls.map{|l| Ruby como #{l}. }.join => Ruby como smalltalk. Ruby como python. Ruby como perl.

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Perl

Ruby

5.times{ p ybuR olleH.reverse } "Hello Ruby" "Hello Ruby" "Hello Ruby" "Hello Ruby" "Hello Ruby" => 5

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Hello

Ruby

Totalmente OOMixins (toma essa!)Classes abertasBlocosBibliotecas

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos

Totalmente OO

42.class => FixnumFixnum.class => Class

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos > Totalmente OO

Mixins (toma essa!)

module Cool def is_cool! #{self} is cool! endend

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos > Mixins (toma essa!)

Classes abertas

class String include Coolendputs Ruby.is_cool! Ruby is cool!

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos > Classes abertas

Blocos

[16,19,21,15].select{|n| n > 17} => [19,21][1,2,3].map{|n| n*2 } => [2,4,6][5,6,7,8].sort_by{ }File.open('file.txt').each_line{ }Dir.glob('*.rb').each{ }Benchmark.measure { ... }

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos > Blocos

Bibliotecas

Hpricot + OpenUriHpricot(open 'http://ab.com').search('a').map{|a| a[:href]}PrawnPrawn::Document.new.text(Hello PDF)RedClothRedCloth.new(Hello *Textile*).to_htmlRMagickImage.read('i.pdf').first.write('i.png'){self.quality = 80}ShoesShoes.app{ button "Hello Windows" }RubyGameGame.new.when_key_pressed(:q){ exit }GeokitYahooGeocoder.geocode 'Rua Vieira de Casto 262'

Introduo ao Ruby on Rails > Ruby on Rails > Ruby > Conceitos > Bibliotecas

Model View Controller

Model dados e lgica de domnioView interfaceController caminhos e distribuio

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller

Model

ActiveRecordMigraesOperaes bsicasRelaesValidaesActs asNamed scopesAdicionar mtodos

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model

Migraes

class CreateEvents < ActiveRecord::Migration def self.up create_table :events do |t| t.string :name, :null => false t.boolean :active, :default => true t.integer :year, :null => false t.timestamps end endend

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Migraes

Operaes bsicas

InsertEvent.create :name => 'RS on Rails', :year => Date.today.yeare = Event.newe.name = 'RS on Rails'e.year = 2009e.save

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Operaes bsicas

Operaes bsicas

SelectEvent.all :conditions => 'active', :order => 'created_at', :limit => 10e = Event.firste = Event.find 1e = Event.find_by_name 'RS on Rails'e = Event.find_by_year 2009

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Operaes bsicas

Operaes bsicas

UpdateEvent.update_all 'active = true'e = Event.firste.name = 'other name'e.savee.update_attribute :active => falsee.toggle! :active

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Operaes bsicas

Operaes bsicas

DeleteEvent.delete_alle = Event.firste.destroy

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Operaes bsicas

Relaes

class Event < ActiveRecord::Base has_many :talksendclass Talk < ActiveRecord::Base belongs_to :event has_one :speakerend

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Relaes

Relaes

rsr = Event.find_by_name('rsrails')talk = rsr.talks.firsttalk.name => Introduo ao Ruby on Railstalk.speaker.name => Juan Maiz Lulkin

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Relaes

Validaes

class Event < ActiveRecord::Base validates_presence_of :name, :year validates_numericality_of :year validates_inclusion_of :year, :in => 2009..2099 validates_length_of :name,:minimum => 4 validates_format_of :name,:with => /[A-Z]\d+/end

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Validaes

Acts As

class Category < ActiveRecord::Base acts_as_treeenbCategory.find(10).parentCategory.find(20).children

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Acts as

Acts As

class Doc < ActiveRecord::Base acts_as_versionedenddoc = Doc.create :name = '1st name'doc.version => 1doc.update_attribute :name, '2nd name'doc.version => 2doc.revert_to(1)doc.name => 1st name

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Acts as

Named scopes

class Product < ActiveRecord::Base named_scope :top, :order => 'rank desc' named_scope :ten, :limit => 10 named_scope :between, lambda{|min,max| {:conditions => [price between ? and ?, min, max]}}endProduct.between(0,100).top.ten

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Named scopes

Adicionar mtodos

class Product < ActiveRecord::Base def +(product) price + product.price endendfoo = Product.find(1)bar = Product.find(2)foo + bar

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Model > Adicionar mtodos

Controller

URL: http://seusite.com/events/rsrails

class EventsController < ApplicationController def show name = params[:id] @event = Event.find_by_name(name) endend

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Controller

View

app/views/events/show.html.haml

#content %h1 Palestras %ul - for talk in @event.talks %li= talk.title

* Voc est usando HAML, no?

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > View

Princpios

Convention Over Configuration Se usado na maior parte dos casos, deve ser o padro.Don't Repeat Yourself Mixins, plugins, engines, gems...Mtodos geis BDD, TDD, integrao contnua, deployment...

Introduo ao Ruby on Rails > Ruby on Rails > Princpios

Convention Over Configuration

XML YAMLdevelopment: adapter: postgresql encoding: unicode database: events_development pool: 5 username: event_admin password: *****

Introduo ao Ruby on Rails > Ruby on Rails > Princpios > CoC

Convention Over Configuration

rsrails = Event.find(1) => assume campo id na tabela eventsrsrails.talks => assume uma tabela talks com um fk event_idrsrails.talks.first.speaker => assume campo speaker_id em talks como fk para tabela speakers

Introduo ao Ruby on Rails > Ruby on Rails > Princpios > CoC

Convention Over Configuration

script/generate scaffold Eventname:stringyear:integeractive:boolean

Introduo ao Ruby on Rails > Ruby on Rails > Princpios > CoC

Don't Repeat Yourself

active_scaffoldlink_to loginimage_tag 'star.png' * hotel.starsrender :partial => 'event',:collection => @events

Introduo ao Ruby on Rails > Ruby on Rails > Princpios > DRY

Mtodos geis

class EventTest < ActiveSupport::TestCase test "creation" do assert Event.create :name => 'rsrails',:year => 2009 endend

Introduo ao Ruby on Rails > Ruby on Rails > Princpios > Mtodos geis

Mtodos geis

rake integratecap deploy

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Princpios > Mtodos geis

Mtodos geis

Getting Realhttp://gettingreal.37signals.com/

Introduo ao Ruby on Rails > Ruby on Rails > Model View Controller > Princpios > Mtodos geis

Comunidade

Lista rails-brRuby Inside (.com e .com.br)Working With Rails

Introduo ao Ruby on Rails > Ruby on Rails > Comunidade

And so much more

Internacionalizao (i18n)RotasCacheAjaxBackground Jobs...

Introduo ao Ruby on Rails > Ruby on Rails > And so much more

Fim

Introduo ao Ruby on Rails > Fim