ruby on rails rad

12
Ruby on Rails Rapid Application Development Petronela-Alina Dănilă Andreea- Mădălina Lazăr

Upload: alina-danila

Post on 08-May-2015

2.389 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Ruby on rails RAD

Ruby on Rails

Rapid Application Development

Petronela-Alina DănilăAndreea- Mădălina Lazăr

Page 2: Ruby on rails RAD

Table of contents

1. Introduction2. Overview3. Architecture

3.1. Rails modules3.2. MVC pattern

4. Application example4.1. Creating a project4.2. Creating a controller4.3. Creating a view

5. Comparisons6. Conclusions7. Bibliography

Page 3: Ruby on rails RAD

1. Introduction

We decided to choose as a development environment Ruby on Rails because we felt the need in challenging our selves and learn something new in a short period of time.

This paper covers the steps of developing a Ruby On Rails web application including theory and practical work. We start of by pointing the language used, framework and RAD that we used, including small technical details.

Further we present the architecture of the Rad that we have chosen, a small glimpse at it’s architecture and after that shortly pointing out the modules

In the Application Example module we present a path in creating a web application using Ruby on Rails: creating a new project, controller and view and describing some of the framework’s features involving the method that we used in development.

To close it all we propose a chapter of comparisons between Rails, Ruby on Rails and other frameworks, RAD’s with pro’s and cont’s.

Page 4: Ruby on rails RAD

2. Overview Ruby

Is a general purpose, cross platform programming language. It supports multiple paradigms, including functional, object oriented, imperative and reflective. The language is interpreted and dynamic typed, having automatic memory management. Ruby was designed to have an elegant syntax and made as human readable as possible.

Rails

Is an open source web application framework written in the Ruby programming language, based on the Model-View-Controller pattern.

Ruby on Rails (RoR)

The framework includes tools that make web development easier, and it was created as a response to heavy web frameworks as J2EE and .NET.

In order to make the development faster, Ruby on Rails uses coding by convention and assumptions, eliminating configuration code.

Many of the common tasks for web development are built-in in the framework, including email management, object database mappers, file structure, code generation, elements naming and organization and so on.

Ruby on Rails framework has the following features:

● Model-View-Controller architecture● REST for web services● Support for major databases (MySQL, Oracle, MS SQL Server, PostgresSQL, IBM

DB2)● Convention over configuration● Script generators● YAML machine, for human readable data serialization● Extensive use of javascript libraries

Page 5: Ruby on rails RAD

3. Architecture

Figure 1. Overall framework architecture

3.1. Rails modules Action Mailer

Is the module responsible for providing e-mail services. It handles incoming and outgoing mails, using simple text or rich-format emails. It has common built-in as welcome messages or sending forgotten passwords. Action Pack

Provides the controller and view layers of the MVC pattern. This module captures the user requests made by the browser/ client and maps the requests to the actions defined in the controller layer. After the action is executed, a view is rendered to be displayed in browser.

This module contains 3 sub-modules: ● Action Dispatch - handles the routing of the request (dispatching);● Action Controller - the request is routed to the corresponding controller, which

contains actions to control the model and view, as well as managing user sessions;● Action View - renders the representation of the web page, which can be made from

templates, feeds and other presentation formats.

Page 6: Ruby on rails RAD

Active Model

Represents the interface between Action Pack and Active Record. Active Record

Is used to manage data in relational databases through objects. This module connects the database tables with the representation in Ruby classes. Rails provide CRUD functionality with zero configuration. Active Resource

This module manages the connection between RESTful web services and objects. It maps model classes to remote REST resources. Active Support

Represents the collection of utility classes and standard Ruby libraries. Railties

Is the core of Rails framework that connects all the modules and handles all processes. 3.2. MVC pattern Model

This layer represents the business logic of the application. Usually, the models are backed by database, but they can also be ordinary Ruby classes that implement a set of interfaces provided by Active Model. View

Is the front-end of the application. The views are HTML files with embedded Ruby code (.erb files). They usually contain loops and conditionals, displaying data received from the controller. Controller

This layer is responsible for handling incoming HTTP requests. The controller interacts with models and views by manipulating the model and rendering the view. They process data from models and pass it to view.

Page 7: Ruby on rails RAD

4. Application example

Before development, you must first install Ruby on Rails (http://rubyonrails.org/download) and find an IDE (RubyMine, Eclipse, NetBeans, RubyInSteel, RadRails).

As an example, we propose an application that gathers information from three web services: www.slideshare.net, www.eventbrite.com and www.twitter.com for a user given query.

4.1. Creating a project

This can be made using the IDE or running a command in the console. All the projects have the same structure, with the file structure containing directories for models, views, controllers and configuration files.

An important configuration file is routes.rb, which specifies the application routing: get "home/index"get "home/respond" 4.2. Creating a controller

In Ruby on Rails, a Controller is a class inheriting ApplicationController:(class HomeController < ApplicationController), that contains a number of predefined methods equaly ( regarding both the name and the number) to the number and name of the application’s views.

Calling REST servicesThe default method is using the standard Ruby RestClient.

# create the requestquery_params = {:api_key => SLIDESHARE_KEY, :ts => ts, :hash => hash, :q => query, :page => '1', :items_per_page => SLIDESHARE_MAX_COUNT}response = RestClient.get(SLIDESHARE_SEARCH_URL, :params => query_params)

4.3. Creating a viewA view is mapped to a method in the controller, as specified in the routing file. When

a page is requested, the corresponding controller handles the request (if necessary interacts with the model) and passes the control to the view. In our example, we have the respond method, that is mapped to the respond view.

def respondif !params["query"].nil?

@slideshare = slideshare_get (params["query"]) @twitter = twitter_get (params["query"]) @event = eventbrite_get (params["query"])

Page 8: Ruby on rails RAD

end end

To better ensure the quality of reading, under the params hash variable, you can find the data sent by the user, from your controller. There are two types of parameters: the first are as part of your URL(everything after “?” in your URL), and the second type the ones sent from a POST request.

Usually views, in RoR are html.erb documents. They use the html format in order to render text visually in the browser, and the .erb extension in order to bind the Rails instance variables that holds information from the controller and shares it with the current view. The content of these variables can be used by embedding the <% %> and <%= %> tags.

In this example :

<% index = 0%><%@slideshare.each do |p| %>

<li id ='slides_<%=index%>'><%= image_tag p.thumbnail, :class => "thumbnail" %><p class="titles"><%=p.title%></p></li>

<% index= index+1%><% end %>

you can see the usage of both these tags. The difference between the two is that the one using the “=” sign actually puts the value of the variable inside the statement, whereas the other one simply resembles and embeds Ruby code.

Another thing that you can spot from the statement above is the usage of <%= image tag %>. This is one of the many helper methods that Rails comes to the developer’s aid in that it replaces bulks of html code with one easy and intuitive call. These kind of helpers can be used for forms(<%= form_tag %>), links (<%= link_to %>),images (as above), meta tags( link tag for css - <%= stylesheet_link_tag "main" %>, <%= csrf_meta_tags %>), scripts( <%= javascript_include_tag "application" %>).

Page 9: Ruby on rails RAD

5. Comparisons

Figure 2. Rails vs PHP vs Java

Figure 3. Google trends, search comparison

Why use Ruby on Rails:● Ruby is more elegant than other languages● quicker launch - it could take about half of the development time with other

frameworks● easier changes - future modifications can be made more quickly● convenient plugins● code testing

When to use Ruby on Rails

Page 10: Ruby on rails RAD

● e-commerce● membership sites - this option is built-in in Rails and there are a variety of plugins● content management● custom database solutions

Why not to use Ruby (on Rails)

● Ruby is slow● Ruby is new● not very scalable

Page 11: Ruby on rails RAD

6. Conclusions

Since the first version was launched, Ruby on Rails has received widespread support, especially from the open-source community. It’s purpose is rapid application development and AGILE practices.

The framework architecture meets most of it’s intended goals, but not without any flaws.

“Ruby is optimized for programmer happiness and sustainable productivity”.

Page 12: Ruby on rails RAD

7. Bibliography http://rubyonrails.org/http://en.wikipedia.org/wiki/Ruby_on_Rails#Technical_overviewhttp://www.adrianmejiarosario.com/content/ruby-rails-architectural-designhttp://www.slideshare.net/jonkinney/ruby-on-rails-overviewhttp://en.wikipedia.org/wiki/Ruby_%28programming_language%29https://github.com/rails/railshttp://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworkshttp://en.wikipedia.org/wiki/Ruby_on_Railshttp://en.wikipedia.org/wiki/Convention_over_Configurationhttps://picasaweb.google.com/Dikiwinky/Ruby#5116531304417868130http://www.slideshare.net/dosire/when-to-use-ruby-on-rails-1308900http://on-ruby.blogspot.com/2006/11/tim-bray-comparing-intrisics.htmlhttp://www.google.com/trends?q=symfony,+ruby+on+rails,+cakephp,+django+python,+asp.net+mvc&ctab=0&geo=all&date=all&sort=3