rapid development with ruby, jruby and rails

Post on 10-May-2015

1.890 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Rapid Development with Ruby, JRuby and Rails

Brian LeonardSoftware Engineer

Sun Microsystems, Inc.

Overall Presentation Goal

Gain a basic understanding of the

Ruby on Rails concepts

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.

4

What is Rails?

An MVC framework for

web applications

5

The Features of Rails

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

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

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]

8

Rails File Types

Extension Type*.rb Ruby file

Embedded Ruby file Configuration

*.erb*.yml*

*YAML Ain't Markup Language

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'

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

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

12

How do I use it?

Outline

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

Outline

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

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

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

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

DEMO

Rails Application – Iteration 1: Application, Database and

Initial Scaffolding

Outline

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

20

Database Migrations

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

DEMO

Rails Application – Iteration 2: Database Migrations

Outline

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

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

Validation

DEMO

Rails Application – Iteration 3: Validation

Outline

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

Understand the Rails URL

Controller ID

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

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 %>

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

DEMO

Rails Application – Iteration 4: The View

Outline

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

Model Relationships

Declaring relationships has_many belongs_to

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

DEMO

Rails Application – Iteration 5: Model Relationships

Outline

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

AJAX

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

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 %>

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 %>

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>

DEMO

Rails Application – Iteration 6: AJAX

Outline

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

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

DEMO

Rails Application – Iteration 7: Java

Outline

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

46

Java Server Integration

Goldspike GlassFish V3

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

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

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

DEMO

Rails Application – Iteration 8: Deploying to GlassFish and

Tomcat

Outline

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

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

Java Powered Ruby

Q&A

Thank you for your attention

top related