Ruby on Rails na Unip

Preview:

DESCRIPTION

Adaptação da minha palestra na InfoQ e na Campus Party para o grupo .Net Architects que se encontra na Unip. http://www.dotnetarchitects.net/ Foi muito legal.

Citation preview

Ruby on Rails

AkitaOnRails.com

http://www.akitaonrails.com

http://www.locaweb.com.br/rails

1

Joel Spolsky

“Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so

massively scalable.”

“The Perils of JavaSchools” - Joel Spolsky - 29/12/2005

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

“The terms Map and Reduce come from Lisp and functional programming...”

“The Perils of JavaSchools” - Joel Spolsky - 29/12/2005

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

“The very fact that Google invented MapReduce, and Microsoft didn't, says something about why Microsoft is

still playing catch up.”

“The Perils of JavaSchools” - Joel Spolsky - 29/12/2005

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

Codificadoresx

Desenvolvedores

“SOFT”WARE

AGILIDADE

Estamos “descobrindo” maneiras melhores de desenvolver software na prática e

ajudando outros a desenvolver.

Big Design Up Front

Big Design Up Front

Escopo Fechado

Escopo Fechado

Faseamento em Cascata

Faseamento em Cascata

“Change Request”

“Change Request”

Ceticismo

Winston W. Royce - 1970

“I believe in this concept, but the implementation described above is risky

and invites failure.”

Winston W. Royce - 1970

“I believe in this concept, but the implementation described above is risky

and invites failure.”

Winston W. Royce - 1970

“I believe in this concept, but the implementation described above is risky

and invites failure.”

Winston W. Royce - 1970

“Cargo Cult”

2

1993

“Matz”

http://www.ruby-lang.org

2001

“Prag Dave”

http://www.rubycentral.com/book/http://www.pragprog.com/titles/ruby3/programming-ruby-1-9

2004

“DHH”

http://www.rubyonrails.orghttp://www.loudthinking.com

http://rubyonrails.org/screencasts

http://rubyonrails.org/screencasts

“Tornar as coisas simples fáceis e as coisas

complexas possíveis”

Filosofia Ruby

http://www.levenez.com/lang/

Ruby on Rails

RUBY

http://guides.rails.info/

ActiveSupportRails

RUBY

http://guides.rails.info/

ActionPack

ActionController

ActionView

ActiveSupportRails

RUBY

http://guides.rails.info/

ActiveRecord

ActionPack

ActionController

ActionView

ActiveSupportRails

RUBY

http://guides.rails.info/

ActiveRecord

ActionPack

ActionMailer

ActionController

ActionView

ActiveSupportRails

RUBY

http://guides.rails.info/

ActiveRecord

ActionPack

ActiveResource

ActionMailer

ActionController

ActionView

ActiveSupportRails

ActionWebService

RUBY

http://guides.rails.info/

describe Product do include ProductSpecHelper

before(:each) do @product = Product.new end

it "should not be valid when empty" do @product.should_not be_valid end

it "should be valid when having correct information" do @product.attributes = valid_product_attributes @product.should be_valid endend

RSpec

describe Product do include ProductSpecHelper

before(:each) do @product = Product.new end

it "should not be valid when empty" do @product.should_not be_valid end

it "should be valid when having correct information" do @product.attributes = valid_product_attributes @product.should be_valid endend

RSpec

rake spec

class Product < ActiveRecord::Base after_create :set_initial_inventory has_many :variants, :dependent => :destroy has_many :images, :as => :viewable, :order => :position, :dependent => :destroy has_many :properties, :through => :product_properties belongs_to :tax_category

validates_presence_of :name validates_presence_of :master_price validates_presence_of :description

make_permalink :with => :name, :field => :permalinkend

Model

class Product < ActiveRecord::Base after_create :set_initial_inventory has_many :variants, :dependent => :destroy has_many :images, :as => :viewable, :order => :position, :dependent => :destroy has_many :properties, :through => :product_properties belongs_to :tax_category

validates_presence_of :name validates_presence_of :master_price validates_presence_of :description

make_permalink :with => :name, :field => :permalinkend

Model

Product.find(1)

class UsersController < Spree::BaseController resource_controller before_filter :initialize_extension_partials actions :all, :except => [:index, :destroy] show.before do @orders = Order.checkout_completed(true) .find_all_by_user_id(current_user.id) end

create.after { self.current_user = @user }

create.response do |wants| wants.html { redirect_back_or_default(products_path) } end end

Controller

class UsersController < Spree::BaseController resource_controller before_filter :initialize_extension_partials actions :all, :except => [:index, :destroy] show.before do @orders = Order.checkout_completed(true) .find_all_by_user_id(current_user.id) end

create.after { self.current_user = @user }

create.response do |wants| wants.html { redirect_back_or_default(products_path) } end end

Controller

/users/1

<div id="product-listing"> <%= breadcrumbs(@taxon) %> <br/> <%= render :partial => "shared/products.html.erb", :locals => {:products => @products, :taxon => @taxon } %></div>

<% content_for :sidebar do %> <td id="shop-by-col" valign="top"> <%= render :partial => "shared/taxonomies" %> </td><% end %>

<%= render :partial => 'shared/paginate', :locals => {:collection => @products, :options => {}} unless @products.empty? %>

Views ERB

#product-listing =breadcrumbs(@taxon) %br =render :partial => "shared/products.html.erb", :locals => {:products => @products, :taxon => @taxon}

-content_for :sidebar do %td#shop-by-col(:valign => "top") =render :partial => "shared/taxonomies" =render :partial => 'shared/paginate', :locals => {:collection => @products, :options => {}} unless @products.empty?

Views HAML

ActionController::Routing::Routes.draw do |map| map.connect ':controller/service.wsdl', :action => 'wsdl'

map.resources :products, :member => {:change_image => :post} map.resources :addresses map.resources :orders, :has_many => [:line_items]

map.namespace :admin do |admin| admin.resources :users admin.resources :products endend

Rotas RESTFul

ActionController::Routing::Routes.draw do |map| map.connect ':controller/service.wsdl', :action => 'wsdl'

map.resources :products, :member => {:change_image => :post} map.resources :addresses map.resources :orders, :has_many => [:line_items]

map.namespace :admin do |admin| admin.resources :users admin.resources :products endend

Rotas RESTFul

GET /products/newGET /productsPOST /productsGET /products/1GET /products/1/editPUT /products/1DESTROY /products/1

class RenameAppConfiguration < ActiveRecord::Migration def self.up rename_table :app_configurations, :configurations change_table :configurations do |t| t.string :type end end

def self.down change_table :configurations do |t| t.remove :type end rename_table :configurations, :app_configurations endend

Migrations

class RenameAppConfiguration < ActiveRecord::Migration def self.up rename_table :app_configurations, :configurations change_table :configurations do |t| t.string :type end end

def self.down change_table :configurations do |t| t.remove :type end rename_table :configurations, :app_configurations endend

Migrations

rake db:migrate

“Beautiful Code”

http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programminghttp://weblog.jamisbuck.org/2008/11/29/recovering-from-enterprise-video-available

11 mil classes!

46 só de Collections!

http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programminghttp://weblog.jamisbuck.org/2008/11/29/recovering-from-enterprise-video-available

• Modules:

• Enumerable

• Comparable

• Classes:

• Array

• Hash

• Set

• Sorted Set

• Modules:

• Enumerable

• Comparable

• Classes:

• Array

• Hash

• Set

• Sorted Set

1.400classes

só 6 de Collections!

• Convention over Configuration

• Don’t Repeat Yourself

• You Ain’t Gonna Need It

• Automação

• Boas Práticas

• Código Bonito

• Ferramentas Simples

http://macromates.comhttp://www.apple.com/macbook

3

Mitos

http://www.loudthinking.com/posts/29-the-rails-myths

Rails não Escala

FUD: http://www.techcrunch.com/2008/05/22/twitter-at-scale-will-it-work/

To put things into perspective, though,

Friendster was written in Java to start, and switched to

PHP. Myspace was written in ColdFusion and transitioned

to ASP.NET.

When people run into problems scaling sites they

often think that the language is the problem, but I think it’s

rarely the case. Blaine Cook

http://www.akitaonrails.com/2008/6/17/chatting-with-blaine-cook-twitter

http://www.akitaonrails.com/2008/6/17/chatting-with-blaine-cook-twitter

“The New York Times used Ruby on Rails to pull together, analyze and display election results in near real time on one of its busiest Web

traffic days ever. ”

http://www.computerworld.com.au/article/268003/ruby_rails_rolls_into_enterprise?fp=16&fpid=1

http://www.computerworld.com/action/article.do?command=viewArticleBasic&articleId=9120778

“They serve up 23 million visitors a month. The conversion resulted in 20,000 lines of Ruby code instead of 125,000 lines of Java code, and most importantly eased

the difficulty they had in maintaining it. Once complete, and optimized their site is now faster than before. They also completed the rewrite in three months with

four developers.”

http://www.railsonwave.com/railsonwave/2008/6/4/yellowpages-com-migrates-to-rails

http://www.akitaonrails.com/2008/11/21/rails-podcast-brasil-qcon-special-john-straw-yellowpages-com-and-matt-aimonetti-merbhttp://www.rubyonrailsexamples.com/sites-on-rails/yellowpagescom-goes-ror/

http://www.techcrunch.com/2008/01/24/hulu-discusses-private-beta-suggests-public-launch-time-frame/

http://www.blogblogs.com.br

Mitos

Deployment de Rails é difícil

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

Apache + FastCGI

LightTPD + FastCGI

Litespeed + SAPI

Apache + Mongrel

Nginx + Mongrel

Nginx + Thin

http://phusion.nlhttp://www.modrails.com/

gem install passengerpassenger-install-apache2-module

http://phusion.nlhttp://www.modrails.com/

Mitos

Rails é mal documentado

Geoffrey

http://www.peepcode.com

Jason e Gregg

http://railsenvy.comhttp://envycasts.com

Ryan Bates

http://railscasts.com

Pratik Naik

http://guides.rails.info/

Satish Talim

http://rubylearning.org

Peter Cooper

http://rubyinside.comhttp://railsinside.comhttp://rubyflow.comhttp://jrubyinside.comhttp://yorails.com

_why

http://whytheluckystiff.net/

http://www.amazon.com/s/ref=nb_ss_gw?url=search-alias%3Dstripbooks&field-keywords=ruby+rails&x=0&y=0

4

Open Source

Chris Wanstrath

http://github.com

http://rubyforge.orghttp://gitorious.org

http://jruby.codehaus.orghttp://www.macruby.orghttp://www.ironruby.nethttp://ruby.gemstone.com/http://rubini.us/

http://gettingreal.37signals.com/GR_por.phphttp://aprendaaprogramar.rubyonrails.pro.br/http://why.nomedojogo.com/http://rubyonrails.pro.brhttp://rubyonbr.org

http://gettingreal.37signals.com/GR_por.phphttp://aprendaaprogramar.rubyonrails.pro.br/http://why.nomedojogo.com/http://rubyonrails.pro.brhttp://rubyonbr.org

Conferências

http://www.confreaks.com/http://www.akitaonrails.com/railsconf2008

http://www.locaweb.com.br/railssummithttp://www.akitaonrails.com/railssummit2008

Especialista de uma coisa só é um amador em todo o

resto.