Rapid Development with Ruby, JRuby and Rails

55
Rapid Development with Ruby, JRuby and Rails Brian Leonard Software Engineer Sun Microsystems, Inc.

Transcript of Rapid Development with Ruby, JRuby and Rails

Page 1: Rapid Development with Ruby, JRuby and Rails

Rapid Development with Ruby, JRuby and Rails

Brian LeonardSoftware Engineer

Sun Microsystems, Inc.

Page 2: Rapid Development with Ruby, JRuby and Rails

Overall Presentation Goal

Gain a basic understanding of the

Ruby on Rails concepts

Page 3: Rapid Development with Ruby, JRuby and Rails

Speaker’s Qualifications

Brian Leonard began working with enterprise Java in 1997 at NetDynamics. Today he's a technology evangelist at Sun Microsystems.

Brian developed the Ruby on Rails tutorial series on netbeans.org which is used at the basis for this presentation.

Page 4: Rapid Development with Ruby, JRuby and Rails

4

What is Rails?

An MVC framework for

web applications

Page 5: Rapid Development with Ruby, JRuby and Rails

5

The Features of Rails

Metaprogramming Active Record Convention over Configuration Scaffolding Easy Ajax Rapid Feedback Loop

Page 6: Rapid Development with Ruby, JRuby and Rails

6

Rails Naming Conventions

Class names are usually CamelCase RocketShip

File names are lower_case rocket_ship.rb

Model names are singular Rocket, Person

Table names are plural rockets, people

Page 7: Rapid Development with Ruby, JRuby and Rails

7

Ruby/Rails Terminology

Rake Ruby Make

Symbols Often used in place of a string

Generators Scripts that create project artifacts (i.e., models,

views and controllers) The Flash

A way to pass objects between requests A temporary scratchpad Commonly used for IDs

:rocket, :edit

flash[:post_id] = @params[:id]

Page 8: Rapid Development with Ruby, JRuby and Rails

8

Rails File Types

Extension Type*.rb Ruby file

Embedded Ruby file Configuration

*.erb*.yml*

*YAML Ain't Markup Language

Page 9: Rapid Development with Ruby, JRuby and Rails

9

Symbols

Self-descriptive immediate objects like the number 3, for example.

Atomic, immutable and unique Can't be parsed or modified All references to a symbol refer to the same object

:foo.equals?(:foo) #True 'foo'.equals?('foo') #False

Always interchangeable with strings No right or wrong usage Easier to type Stand out in the editor The different syntax can distinguish keys from

values :name => 'Brian'

Page 10: Rapid Development with Ruby, JRuby and Rails

10

Symbols

Often used to refer to: Method names (:post_comment) Options in a method argument list (:name, :title) Hash keys (:name => 'Brian')

Symbols are not: Variables

They don't hold references to other objects Strings

Although you can get their string representation

Page 11: Rapid Development with Ruby, JRuby and Rails

11

So really, what is a symbol?

Simply, a symbol is something that you use to represent names and strings.

What this boils down to is a way to efficiently have descriptive names while saving the space one would use to generate a string for each naming instance.

Useful whenever you’re going to be reusing a word over and over to represent something else

http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols

Page 12: Rapid Development with Ruby, JRuby and Rails

12

How do I use it?

Page 13: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 14: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 15: Rapid Development with Ruby, JRuby and Rails

Creating a Basic Rails Project

Create your project rails #{project name}

Create your database rake > db > create > all Will create the database(s) as defined in

database.yml: #{project name}_development #{project name}_test #{project name}_production

Page 16: Rapid Development with Ruby, JRuby and Rails

Creating a Basic Rails Project

Scaffold your first resource ruby script/generate scaffold #{model name} #{attribute pairs}

Follow conventions (singular form) Execute the database migrations

rake db:migrate

Page 17: Rapid Development with Ruby, JRuby and Rails

Creating a Basic Rails Project - Example

rails blog rake db > create; ruby script/generate scaffold post title:string

rake db:migrate ruby script/server

Test: http://localhost:3000/posts

Page 18: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 1: Application, Database and

Initial Scaffolding

Page 19: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 20: Rapid Development with Ruby, JRuby and Rails

20

Database Migrations

Page 21: Rapid Development with Ruby, JRuby and Rails

The Evolving Data Model

Our requirements have changed! Our database needs another field

Migrations to the rescue ruby script/generate migration #{migration name} #{attribute pairs}

For example: ruby script/generate migration AddBodyToPosts body:text

class AddBody < ActiveRecord::Migration def self.up

add_column :post, :body, :text endend

Page 22: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 2: Database Migrations

Page 23: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 24: Rapid Development with Ruby, JRuby and Rails

Validation

Defined in the model – the gatekeeper of our data

Common methods: validates_presence_of validates_numericality_of validates_uniqueness_of validates_format_of validates_length_of

Code Templates exist for most of these

Page 25: Rapid Development with Ruby, JRuby and Rails

Validation

Page 26: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 3: Validation

Page 27: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 28: Rapid Development with Ruby, JRuby and Rails

Understand the Rails URL

Controller ID

http://domain/#{controller}/#{id} and/or #{action}

Page 29: Rapid Development with Ruby, JRuby and Rails

erb Templates

<p> <b>Title:</b> <%=h @post.title %></p><p> <b>Body:</b> <%=h @post.body %></p><%= link_to 'Edit', edit_post_path(@post) %> |<%= link_to 'Back', posts_path %>

Page 30: Rapid Development with Ruby, JRuby and Rails

Application Flow

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

<% form_for(@post) do |f| %> <p><b>Title</b><br /> <%= f.text_field :title %></p> <p><b>Body </b><br /> <%= f.text_area :body %></p> <p><%= submit “Update” %></p><% end %>

def update @post = Post.find(params[:id]) if @post.update_attributes(params[:post]) redirect_to(@post) else render :action => “edit” end end

posts_controller.rb

edit.html.erb

posts_controller.rb

1

2

Page 31: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 4: The View

Page 32: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 33: Rapid Development with Ruby, JRuby and Rails

Model Relationships

Declaring relationships has_many belongs_to

Page 34: Rapid Development with Ruby, JRuby and Rails

Model Relationships

For example:

Allows for:

class Post < ActiveRecord::Base has_many :commentsend

class Comment < ActiveRecord::Base belongs_to :postend

# Fetch all comments for post id 1post = Post.find(1)comments = post.comments.collect

Page 35: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 5: Model Relationships

Page 36: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 37: Rapid Development with Ruby, JRuby and Rails

AJAX

Prototype and script.aculo.us libraries are included with Rails Others can be easily added

Page 38: Rapid Development with Ruby, JRuby and Rails

Prepare the Application for AJAX

Move the html you would like to be dynamic into a partial template.

“Partials” Chunks of rhtml that act as methods Prepended with an underscore.

For example: _comment.rhtml Usage:

Test!

<%= render :partial => "comment", :object => comment %>

Page 39: Rapid Development with Ruby, JRuby and Rails

First Steps to AJAX

Include the Prototype and script.aculo.us Javascript libraries

Replace form_tag (does an HTTP POST) with form_remote_tag (does an XMLHTTPRequest)

Test!

<%= javascript_include_tag :defaults %>

Page 40: Rapid Development with Ruby, JRuby and Rails

Steps to AJAX

Use render :update construct to handle the XMLHTTPRequest

render :update allows you to use Ruby commands to generate JavaScript

Create a DOM id to reference

Test!

render :update do |page| page.insert_html :bottom,

"comments", :partial => “comment”

end

<div id=”comments”> ... </div>

Page 41: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 6: AJAX

Page 42: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 43: Rapid Development with Ruby, JRuby and Rails

Java

To call Java from Ruby:

Import classes (just as in Java)

Use the class: Note the Ruby syntax!

include Java

import java.util.ArrayList

dogs = ArrayList.newdogs.add "Spaniel"dogs.add "Hound"dogs.add "Retriever"

dogs.each do |dog| puts dogend

Page 44: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 7: Java

Page 45: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations Validation The View Database Relationships Ajax Java Deployment

Page 46: Rapid Development with Ruby, JRuby and Rails

46

Java Server Integration

Goldspike GlassFish V3

Page 47: Rapid Development with Ruby, JRuby and Rails

47

Goldspike

Rails Plugin Packages Rails application as WAR WAR contains a servlet that translates data

from the servlet request to the Rails dispatcher

Works for any servlet container rake war:standalone:create

Page 48: Rapid Development with Ruby, JRuby and Rails

48

GlassFish v3

Next version of GlassFish Ideal container for Web 2.0 applications Small

Kernel < 100k Fast

Starts up in < 1 second Modular

Java, Ruby, PHP, JavaScript, ... Will be Java EE 6 compatible

Page 49: Rapid Development with Ruby, JRuby and Rails

49

Why JRuby on GlassFish ?

Java EE is tested deployment platform Integrate existing Java EE & RoR apps in

one container Hot Deployment

No need to restart container Database Connection Pooling One instance, one process OOTB Clustering and High Availability

Page 50: Rapid Development with Ruby, JRuby and Rails

DEMO

Rails Application – Iteration 8: Deploying to GlassFish and

Tomcat

Page 51: Rapid Development with Ruby, JRuby and Rails

Outline

Creating the Project Schema Migrations The View Validation Database Relationships Ajax Java Deployment

Page 52: Rapid Development with Ruby, JRuby and Rails

Summary

The popular Ruby language and Ruby on Rails framework are now available on the Java platform

Ruby's dynamic nature make it a fun and ideal language for web development (Ruby on Rails)

Rails applications can be deployed to your favorite Java server container

Page 53: Rapid Development with Ruby, JRuby and Rails

Java Powered Ruby

Page 54: Rapid Development with Ruby, JRuby and Rails

Q&A

Page 55: Rapid Development with Ruby, JRuby and Rails

Thank you for your attention