76
Ruby on Rails Chew Choon Keat <sharedcopy.com> 9th April 2008 @ Singapore Management University Sharing Session for Singapore PHP User Group

Ruby on Rails - Sharing Session for Singapore PHP User Group

Embed Size (px)

Citation preview

Ruby on Rails

Chew Choon Keat <sharedcopy.com>9th April 2008 @ Singapore Management University

Sharing Session for Singapore PHP User Group

Background

• Chew Choon Keat @ blog.choonkeat.com

• Various startups & IT shops doing webapps

• Somehow almost always relates to mobile stuff, e.g. SMS, MMS, WAP,...

• J2EE shop

• 2004: Hello Rails

• RssFwd.com project

• SharedCopy.com

Ruby on Rails• Ruby Web Framework based on MVC

“optimized for programmer happiness and sustainable productivity”

Ruby on Railsamazon.com

BBCCapGemini

BPNCiscoC|Net

EA (Electronic Arts)IBM

JP MorganNASAOakleyOracleSiemens

ThoughtWorksYahoo!

http://blog.obiefernandez.com/content/2008/03/big-name-compan.html

Ruby Web Framework based on MVC

• A Programming Language

• Does it matter?

• What language do you use when talking to your [____________] ?

• Ever read a EULA before?

• Domain Specific Language

• Big words. Somebody else need it.

• Jargons e.g. “Google it!”

• Writing programs down to the language?

• Build the language up?

• Article belongs_to :author

• Author has_many :articles

Ruby Web Framework based on MVC

• You could

• echo “HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 100\r\n\r\n<html>...”

• Or use a framework

• render “index.html”

• Common vs Exceptions

Ruby Web Framework based on MVC

http://flickr.com/photos/kervinchong/6074454/

http://flickr.com/photos/qousqous/4682900/

• Model

• View

• Controller

Ruby Web Framework based on MVC

http://flickr.com/photos/kodama/8050629/

http://flickr.com/photos/brutalworks/136746095/

• Structure that helps you to build GUI apps

Ruby Web Framework based on MVC

http://flickr.com/photos/lincolnian/120277776/

http://flickr.com/photos/pulpolux/1579275746/

http://flickr.com/photos/justusthane/1252907196/

http://localhost:3000/articles

app/models/article.rbM

app/views/articles/index.html.erbV

app/controllers/articles_controller.rbC

Result

http://localhost:3000/articles

app/models/article.rbM

app/views/articles/index.html.erbV

app/controllers/articles_controller.rbCGet “Articles” to fetch data from DB

Result

http://localhost:3000/articles

app/models/article.rbM

app/views/articles/index.html.erbV

app/controllers/articles_controller.rbCPass @articles data to render

Result

http://localhost:3000/articles

app/models/article.rbM

app/views/articles/index.html.erbV

app/controllers/articles_controller.rbC

Result

Rendered HTML goes back to browser

Formshttp://localhost:3000/articles/new

Formsapp/views/articles/new.html.erb http://localhost:3000/articles/new

Forms

Submitted values is accessible via “params”

Forms

Submitted values is accessible via “params”

app/controllers/articles_controller.rb

• Options

• All the options, all the time

• You may like to go... Left? Right?

• Left? Right?

• Right?

Configuration

http://flickr.com/photos/zzathras777/1477996488/

• Accept that not all roads are equal

• Common case; 80/20 Rule

• Culture that values conventions

• Development process highway

• Understand somebody else’s project

• Maintenance - when does it start?

Convention

Version Control

• Do you version control your source code?

Version Control

• Do you version control your source code?

Who?

Version Control

• Do you version control your source code?

What?

Who?

Version Control

• Do you version control your source code?

What?

Who?

When?

Version Control

• Do you version control your source code?

What?

Who?

When?

Where?

Version Control

• Do you version control your source code?

Why?What?

Who?

When?

Where?

Version Control

• Do you version control database schema?

Version Control

• Do you version control database schema?

idemail

User

Version Control

• Do you version control database schema?

idemail

Useridfirst_namelast_nameemailcreated_at

User

ALTER TABLE user .... ;

Version Control

• Do you version control database schema?

idemail

Useridfirst_namelast_nameemailcreated_at

Useridnameemailcreated_atupdated_at

User

ALTER TABLE user .... ;ALTER TABLE user .... ;

Version Control

• Do you version control database schema?

idemail

Useridfirst_namelast_nameemailcreated_at

Useridnameemailcreated_atupdated_at

Useridfullnameemailcreated_atupdated_at

User

ALTER TABLE user .... ;ALTER TABLE user .... ;ALTER TABLE user .... ;

Version Control

• Do you version control database schema?

001_create_user002_add_names_columns

003_combine_name_columns004_rename_fullname

rake db:migrate:down

rake db:migrate

Version Control

• Do you version control deployments?

Version Control

• Do you version control deployments?

http://flickr.com/photos/ztephen/239387292/

myfacester.com v1laptop

mysqldump < schema.sql

Version Control

• Do you version control deployments?

myfacester.com v2laptop

http://flickr.com/photos/ztephen/239387292/

backup.tar?

mysqldump < alter_table.sql

Version Control

• Do you version control deployments?

laptop

http://flickr.com/photos/ztephen/239387292/

myfacester.com v2.1

/tmp/monday.tar.gz?

backup.tar?

mysqldump < alter_table2.1.sql

Version Control

• Do you version control deployments?

laptopmyfacester.com v2.1

/tmp/monday.tar.gz?

backup.tar?

mysqldump < alter_table2.1.sql

What if there’s a problem?

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

source version control, remember?

Version Control

• Do you version control deployments?

laptopmyfacester.com

cap deploy

svn.myfacester.com

http://flickr.com/photos/bods/485081783/

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

rake db:migrateschema version control, remember?

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

Version Control

• Do you version control deployments?

laptopmyfacester.com

cap deploy

svn.myfacester.com

http://flickr.com/photos/bods/485081783/

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

rake db:migrate

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

Version Control

• Do you version control deployments?

laptopmyfacester.com

cap deploy

svn.myfacester.com

http://flickr.com/photos/bods/485081783/

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

rake db:migrate

Version Control

• Do you version control deployments?

laptopmyfacester.com

svn.myfacester.com

rake db:migrate:down

cap rollback

http://flickr.com/photos/bods/485081783/

• “Make it work, make it right, make it fast”

• scaffolding - get it working

• Learn as you go.

• add_column vs SQL

• form_for vs HTML

• ./script/server vs Capistrano

Learning the Framework

• Object Oriented

• Message Passing

• Dynamically typed

• Duck typing

• Open classes

http://www.ruby-lang.org/

Ruby System

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

Ruby System

ruby

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

Ruby System

ruby

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

rubygems

Ruby Systemrubyforge.org (gem_server)

ruby

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

rubygems

Ruby Systemrubyforge.org (gem_server)

rails

rfacebook

mongrelruby

rakeactionmaileractionpackactiverecordactiveresourceactivesupport

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

rubygems

Ruby Systemrubyforge.org (gem_server)

rails

rfacebook

mongrelruby

rakeactionmaileractionpackactiverecordactiveresourceactivesupport

Ruby app (*.rb)#!/bin/rubyrequire “mongrel”

http://flickr.com/photos/31637055@N00/449968042/

Programmer CraftsmanRuby app (*.rb)#!/bin/rubyrequire “rubygems”require “mongrel”

rubygems

Ruby Systemrubyforge.org (gem_server)

rubygems

rails

rfacebook

mongrelruby

rakeactionmaileractionpackactiverecordactiveresourceactivesupport

Ruby app (*.rb)#!/bin/rubyrequire “rubygems”require “mongrel”

http://flickr.com/photos/10380973@N02/2303539234/

http://flickr.com/photos/31637055@N00/449968042/

Programmer Craftsman

Happy User

Object Oriented

Object Oriented

Closures

Closures

Closures

Closures

http://martinfowler.com/bliki/Closure.html

Closures

Common Usage

Mixins

Mixins

Mixins

Mixins

Meta-Programming

• Write less dumb code

• What does your software already know?

Meta-Programming

• Write less dumb code

• What does your software already know?

Builder

Builder

Exercise

• Project contains image-processing code

• One day, you learn about “orientation”

• How do you update your app?

Edit > Find & Replace

img = Image.load(“photo.jpg”)img = my_new_load(“photo.jpg”)

Aliasing Methods

• Affords the confidence to march forward

• without falling down subsequently

• Less needy to overly protect design with more layers upfront

Personal Benefits

Links

• http://www.ruby-lang.org/en/

• http://rubyonrails.org/

• http://rubini.us/

• http://sharedcopy.com/

The End