65
Ruby on Rails For Java Programmers Rob Sanheim www.robsanheim.com www.seekingalpha.com www.ajaxian.com

Ruby on Rails For Java Programmers

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ruby on Rails For Java Programmers

Ruby on Rails For Java Programmers

Rob Sanheim

www.robsanheim.comwww.seekingalpha.com

www.ajaxian.com

Page 2: Ruby on Rails For Java Programmers

(Briefly) About Me

Page 3: Ruby on Rails For Java Programmers

Why Rails?

Page 4: Ruby on Rails For Java Programmers

Because its fun.

Page 5: Ruby on Rails For Java Programmers

Because it will make you a better

programmer.

Page 6: Ruby on Rails For Java Programmers

“Learn at least one new [programming] language every year. Different languages solve the same problems in different ways. By learning several

different approaches, you can help broaden your thinking and avoid getting stuck in a rut.” - David Thomas and Andy Hunt

Page 7: Ruby on Rails For Java Programmers

Because you might just be able to get

paid for it.

Page 8: Ruby on Rails For Java Programmers

•Fully Object Oriented

•Interpreted

•Simple, Yet Powerful

•Open Source

•Born in Japan in 1995

•Yukihiro Matsumoto (“Matz”)

What is Ruby?

Page 9: Ruby on Rails For Java Programmers

Power

•Closures (aka Blocks)

•Modules

•Reflection

•Open Classes

Page 10: Ruby on Rails For Java Programmers

Typing

•Strong

•Dynamic

•“Duck”

Page 11: Ruby on Rails For Java Programmers

Lets see some code

Page 12: Ruby on Rails For Java Programmers

HelloWorld

Page 13: Ruby on Rails For Java Programmers
Page 14: Ruby on Rails For Java Programmers
Page 15: Ruby on Rails For Java Programmers

Reading a file

Page 16: Ruby on Rails For Java Programmers
Page 17: Ruby on Rails For Java Programmers
Page 18: Ruby on Rails For Java Programmers

Reading a file - Ruby

Page 19: Ruby on Rails For Java Programmers

Collections

Page 20: Ruby on Rails For Java Programmers
Page 21: Ruby on Rails For Java Programmers
Page 22: Ruby on Rails For Java Programmers
Page 23: Ruby on Rails For Java Programmers

Reflection

Page 24: Ruby on Rails For Java Programmers
Page 25: Ruby on Rails For Java Programmers
Page 26: Ruby on Rails For Java Programmers

Ruby on Rails

• David Heinemeier Hansson (DHH)

• Began via Basecamp in mid 2003

• Released July 2004

• Full Stack MVC Web Framework

• Easy database-based web apps

Page 27: Ruby on Rails For Java Programmers

Accolades

• 2006 Jolt award - Web Dev Tool

• Featured in BusinessWeek, Wired

• DHH “Best Hacker” award from Google

Page 28: Ruby on Rails For Java Programmers

Converts

•Dave Thomas

•Bruce Tate

•Stuart Halloway

• Justin Gehtland

•David Geary

• James Duncan Davidson

Page 29: Ruby on Rails For Java Programmers

“Rails is the most well thought-out web development framework I’ve ever used. And that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped develop

the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.”

-James Duncan Davidson, Creator of Tomcat and Ant

Page 30: Ruby on Rails For Java Programmers

Platform

•MySQL, PostgreSQL, SQLite, Oracle, SQL Server, DB2, or Firebird

•develop anywhere

•Apache or LightTPD + FastCGI for production

Page 31: Ruby on Rails For Java Programmers

DRY - Don’t Repeat Yourself

* Done via intelligent reflection and metaprogramming* Emphasis on writing code to get stuff done - avoid boilerplate* Scaffolding handles left over boilerplate

Page 32: Ruby on Rails For Java Programmers

Convention over Configuration

NO XML!Intelligent DefaultsOnly explicitly configure when necessary

Page 33: Ruby on Rails For Java Programmers

ActiveRecordAn object that wraps a row in a database table or view, encapsulates the database

access, and adds domain logic on that data.

Page 34: Ruby on Rails For Java Programmers

Controller and View

• ActionPack maps URLs to controller• View handled via Ruby in HTML (RHTML) or builders• Ajax provided via Prototype and RJS

Page 36: Ruby on Rails For Java Programmers

Lets see some code

Page 37: Ruby on Rails For Java Programmers

create table orders ( id int not null auto_increment, name varchar(100) not null, email varchar(255) not null, address text not null, pay_type char(10) not null, shipped_at datetime null, primary key (id));

Page 38: Ruby on Rails For Java Programmers

class Order < ActiveRecord::Base end

Page 39: Ruby on Rails For Java Programmers

order = Order.neworder.name = “Rob”order.email = “[email protected]”order.address = “123 1st St”order.save

Page 40: Ruby on Rails For Java Programmers

an_order = Order.find(24)

robs_order = Order.find(:first, :conditions => "name = 'Rob'")

rob_count = Order.count(:all, :conditions => “name = ‘Rob’”)

robs_order = Order.find_by_name(“Rob”)

more_orders = Order.find_all_by_name_and_pay_type(name, type)

Order.delete(24)

robs_order.destroy

Page 41: Ruby on Rails For Java Programmers

create table order_lines ( id int not null auto_increment, description varchar(100) not null, order_id int not null, primary key (id), constraint fk_order foreign key (order_id) references orders(id));

Page 42: Ruby on Rails For Java Programmers

class Order < ActiveRecord::Base has_many :order_lines end

class OrderLine < ActiveRecord::Base belongs_to :orderend

one to many between orders and order lines

“has_many” is actually a class level method call that generates instance methods

Page 43: Ruby on Rails For Java Programmers

my_order = Order.find(id)has_order_lines = my_order.order_lines?order_lines_count = my_order.order_lines.size

my_order.order_lines.push(new_order, another_new_order)

specific_order_line = my_order.find(....)specific_order_lnes = my_order.find_by_description(“abc”)

my_line = OrderLine.find(15)has_owning_order = my_line.order.nil?

old_order = my_line.ordermy_line.order= another_order

Page 44: Ruby on Rails For Java Programmers

Validation

Page 45: Ruby on Rails For Java Programmers

00

0

0000

16,400

04/17/2006 4868Return Prepared:Prepared On:

personalized tax filing advice for:

to file your 2005 federal extension, simply follow these instructions

quick summary

filing checklist for your 2005 federal application for automatic extension

$$

Gross Income

Total Taxable Income

Adjusted Gross Income$$

$

$$

For more information about tax, mortgage and financial services call 1-800-HRBLOCK or visit hrblock.com

to file your return.

Total Deductions

$

This checklist has been customized for you based on the method you're using to file your 2005

Total PaymentsTotal Tax

Refund Amount

tax return. The statements below are designed to help guide you through the steps you will take

Amount You Owe

STEP B - Since you indicated that you are not paying an amount withForm 4868, there is nothing to mail.

number on your printed copy of Form 4868 and keep for your records.

STEP A - Once your e-filed extension has been accepted, you willreceive an email with a confirmation number. Enter the confirmation

* confirmation checks two password fields against each other

* acceptance is pattern of terms of service/agreement checkbox

Page 46: Ruby on Rails For Java Programmers

00

0

0000

16,400

04/17/2006 4868Return Prepared:Prepared On:

personalized tax filing advice for:

to file your 2005 federal extension, simply follow these instructions

quick summary

filing checklist for your 2005 federal application for automatic extension

$$

Gross Income

Total Taxable Income

Adjusted Gross Income$$

$

$$

For more information about tax, mortgage and financial services call 1-800-HRBLOCK or visit hrblock.com

to file your return.

Total Deductions

$

This checklist has been customized for you based on the method you're using to file your 2005

Total PaymentsTotal Tax

Refund Amount

tax return. The statements below are designed to help guide you through the steps you will take

Amount You Owe

STEP B - Since you indicated that you are not paying an amount withForm 4868, there is nothing to mail.

number on your printed copy of Form 4868 and keep for your records.

STEP A - Once your e-filed extension has been accepted, you willreceive an email with a confirmation number. Enter the confirmation

Page 47: Ruby on Rails For Java Programmers

Much More...

• many to many via has_many :through

• eager loading

• acts_as_list, acts_as_tree

• single table inheritance

• callbacks and observers

Page 48: Ruby on Rails For Java Programmers

ActionController

Page 49: Ruby on Rails For Java Programmers

http://url.com/weblog/aka

http://url.com/weblog/index.rhtml

Page 50: Ruby on Rails For Java Programmers

/app/controllers/weblog_controller.rb:class WeblogController < ActionController::Base layout "weblog/layout"

def index @posts = Post.find_all end

def display @post = Post.find(:params[:id]) end

def new @post = Post.new end

def create @post = Post.create(params[:post]) redirect_to :action => "display", :id => @post.id end end

||= conditional assignment operator

cart is a non persistent model object used for storing/calculating products

“the flash” = place to throw stuff like response messages, error reports that accumulate as a request is processed

Page 51: Ruby on Rails For Java Programmers

/app/views/weblog/layout.rhtml: <html><body> <%= @content_for_layout %> </body></html>

/app/views/weblog/index.rhtml: <% for post in @posts %> <p><%= link_to(post.title, :action => "display", :id => post.id %></p> <% end %>

/app/views/weblog/display.rhtml: <p> <b><%= post.title %></b><br/> <b><%= post.content %></b> </p>

/app/views/weblog/new.rhtml: <%= form "post" %>

Page 52: Ruby on Rails For Java Programmers

http://url.com/weblog/display/5http://url.com/weblog/display/123

http://url.com/weblog/new/

Page 53: Ruby on Rails For Java Programmers

• rxml - XML Builders

• rhtml - HTML templates

• helpers - module to assist the view

• form and field helpers

• layouts and partials - similiar to tiles

• page caching and fragment caching

ActionPack features

Page 54: Ruby on Rails For Java Programmers

FORM HELPERS 342

!"#$#%&'(')

''''*+,'(-'./012

''''*3&4$'(-')'

''''''''*5#%4'(-'6'777'62'

''''''''*8935:$;'(-'6'777'62

''''''''*"#&&<9$,'(-'6'777'6''=

=

>?9$%'#8:+95(6@%;#""@&#A4@./016-

''''>+5"3:'5#%4(63&4$B5#%4C6'777'-

''''>+5"3:'5#%4(63&4$B8935:$;C6'777'-

''''>+5"3:'5#%4(63&4$B"#&&<9$,C6'777'-

''''7'7'7

>@?9$%-

>D('?9$%E:#F'*#8:+95'(-'G&#A4G2'*+,'(-'!3&4$'D-

>D(':4H:E!4I,'''''''''''G3&4$G2'G5#%4G''D->@"-

>D(':4H:E!4I,'''''''''''G3&4$G2'G8935:$;G''D->@"-

>D('"#&&<9$,E!4I,'G3&4$G2'G"#&&<9$,G''D->@"-

7'7'7

>D('45,E?9$%E:#F'D-

!"#'&#A4

''3&4$'('J&4$7!5,K"#$#%&B*+,CL

''$#'3&4$73",#:4E#::$+M3:4&K"#$#%&B*3&4$CL

'''''777

''"%!

"%!

NO4'#""I+8#:+95'$484+A4&'#'$4P34&:'

:9'4,+:'#'3&4$7'Q:'$4#,&':O4',#:#'+5:9'

#'54<'J&4$'%9,4I'9MR48:7

NO4'4,+:7$O:%I':4%"I#:4'+&'8#II4,7'Q:'

3&4&':O4'+5?9$%#:+95'+5':O4'3&4$'

9MR48:':9'F454$#:4777

:O4'SNTU'+&'&45:':9':O4'M$9<&4$7'

VO45':O4'$4&"95&4'+&'$484+A4,777

:O4'"#$#%4:4$&'#$4'4H:$#8:4,'+5:9'#'

54&:4,'O#&O7'

NO4'&#A4'#8:+95'3&4&':O4'

"#$#%4:4$&':9'!5,':O4'3&4$'$489$,'

#5,'3",#:4'+:7

&'())*+,%-.,//".0.1

"!$-0.2-&/

!"#'4,+:

''!3&4$'('J&4$7!5,K"#$#%&B*+,CL

"%!

!

"

#

$

%

!

"

#

$

%

Figure 17.2: Models, Controllers, and Views Work Together

Report erratumPrepared exclusively for Robert Sanheim

hash created with user attributes for easy update/save

field helpers make input(s) easy

Page 55: Ruby on Rails For Java Programmers

• Ajax support built in since March ’05

• Built on Prototype and Scriptaculous

• Updating forms and tables via Ajax is trivial

• Best Ajax support of any web framework

Ajax Support

Page 56: Ruby on Rails For Java Programmers

• Awesome Testing support built in

• Generators/scaffolding

• Migrations - refactor your database

• Capistrano - automate deployment

• Huge, helpful community

Other Stuff

Page 57: Ruby on Rails For Java Programmers

But Does it Scale?

Page 58: Ruby on Rails For Java Programmers

Yes.

Page 59: Ruby on Rails For Java Programmers

• scales just like Yahoo! , LiveJournal, etc

• share nothing

• load balance to each tier

• add servers as needed

Page 61: Ruby on Rails For Java Programmers

Why Not Rails?

Page 62: Ruby on Rails For Java Programmers

• Legacy databases

• Tool support

• Deployment is hard

• Internationalization

• Its different

• Its advanced

Page 63: Ruby on Rails For Java Programmers

• Rails growth will continue

• Its influence is already felt everywhere

• JRuby could be huge

• Its biggest impact may be felt in future frameworks

The Future

Page 64: Ruby on Rails For Java Programmers

No mater what, its going to be a fun ride.

Page 65: Ruby on Rails For Java Programmers

Thank You!