Rapid Development with Ruby, JRuby and Rails
Upcoming SlideShare
Loading in...5
×
 

Rapid Development with Ruby, JRuby and Rails

on

  • 2,219 views

 

Statistics

Views

Total Views
2,219
Views on SlideShare
2,218
Embed Views
1

Actions

Likes
0
Downloads
13
Comments
0

1 Embed 1

http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Rapid Development with Ruby, JRuby and Rails Rapid Development with Ruby, JRuby and Rails Presentation Transcript

    • Rapid Development with Ruby, JRuby and Rails Brian Leonard Software 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.
    • What is Rails? An MVC framework for web applications 4
    • The Features of Rails  Metaprogramming  Active Record  Convention over Configuration  Scaffolding  Easy Ajax  Rapid Feedback Loop 5
    • 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 6
    • Ruby/Rails Terminology  Rake  Ruby Make  Symbols  Often used in place of a string :rocket, :edit  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 flash[:post_id] = @params[:id] 7
    • Rails File Types Extension Type *.rb Ruby file *.erb Embedded Ruby file *.yml * Configuration * YAML Ain't Markup Language 8
    • 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' 9
    • 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 10
    • 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 11
    • How do I use it? 12
    • 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
    • Database Migrations 20
    • 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 end end
    • 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 http://domain/#{controller}/#{id} and/or #{action} Controller ID
    • 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 posts_controller.rb def edit @post = Post.find( params[:id]) 1 end edit.html.erb <% 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 %> posts_controller.rb def update 2 @post = Post.find(params[:id]) if @post.update_attributes(params[:post]) redirect_to(@post) else render :action => “edit” end end
    • 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: class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end  Allows for: # Fetch all comments for post id 1 post = 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: <%= render :partial => "comment", :object => comment %>  Test!
    • First Steps to AJAX  Include the Prototype and script.aculo.us Javascript libraries <%= javascript_include_tag :defaults %>  Replace form_tag (does an HTTP POST) with form_remote_tag (does an XMLHTTPRequest)  Test!
    • Steps to AJAX  Use render :update construct to handle the XMLHTTPRequest  render :update allows you to use Ruby commands to generate JavaScript render :update do |page| page.insert_html :bottom, "comments", :partial => “comment” end  Create a DOM id to reference <div id=”comments”> ... </div>  Test!
    • 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: include Java  Import classes (just as in Java) import java.util.ArrayList  Use the class:  Note the Ruby syntax! dogs = ArrayList.new dogs.add "Spaniel" dogs.add "Hound" dogs.add "Retriever" dogs.each do |dog| puts dog end
    • DEMO Rails Application – Iteration 7: Java
    • Outline  Creating the Project  Schema Migrations  Validation  The View  Database Relationships  Ajax  Java  Deployment
    • Java Server Integration  Goldspike  GlassFish V3 46
    • 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 47
    • 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 48
    • 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 49
    • 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