-
Notifications
You must be signed in to change notification settings - Fork 5
Lecture 05
Ruby On Rails (or just Rails) is a web framework written in Ruby. It was started by David Heinemeier Hansson (a.k.a. DHH) in during his work on the famous 37signals' application – project management tool Basecamp. The code from Basecamp was extracted for further development of other products in 37signals, and along with that it was also released as open-source under MIT License in July 2004.
Rails is designed to help developers in programming web application in their daily job. The core of Rails is based on some common assumptions about what each developer needs for doing their job. It means writing less code and avoiding wasting of time. Instead of spending time on many unimportant things, a developer can more focus on more important ones such as designing and developing required features of an application.
Rails offers "the best way" how to do some things. This approach is encouraged many ways inside the framework such as an architecture, a way of work, generators of code, a directory structure and so on. This is called The Rails way and is strongly opinionated. On the other hand it offers effective and rapid development.
Rails guides several principles:
- Don't Repeat Yourself (DRY) – Writing the same code over and over is just wasting of time
- Convention over configuration (CoC) – There are some assumptions about a developer want to do how (s)he is going to do it rather than specifying every little thing in configuration somewhere.
- REST (Representational State Transfer) – An pattern for organizing an web application around resource and standard HTTP verbs.
Ruby on Rails is based on the model-view-controller (MVC) architecture and it provides the core foundation for a Rails' application. It seperates the whole application to three main parts which are logically different. In Rails, those important parts are kept in a specify directory structure inside app directory in the main project directory. This directory are also kept other important things such as JavaScript files, Cascading Style Sheets (CSS), images and so on, which are not a part of MVC paradigm but they also have the same importance.
The basic structure of app directory is the following:
app
assets
controllers
helpers
mailers
models
views
- Models define domain logic – interaction with records stored in tables to retrieve and set information to them. The part of domain logic are many things such as validations or actions to perform on the data.
- Controllers interact with models which are called by methods. The models can return a single object of a given model or collection of these object of a given model. Those objects are made available to the view through instance variables.
- Views display the information gathered by the controller through instance variables. The templates and layouts use by default a templating language is Embedded Ruby (ERB).
- Assets is for storing static assets of the application, such as JavaScript files, Cascading Style Sheets (CSS), images.
- Helpers are a place for Ruby modules providing helper methods for just the views. These helpers can help keeping code in views readable and easier for maintenance and they can be also reusable on the other place.
- Mailers are a place for classes dealing with sending emails.
Rails is composed of individual components. Those components are packed to many parts called Railties, such as Active Support, Active Records, Action Pack or Action Mailer. On the following diagram is showed the whole Rails architecture composed of all main components:
In the following table is briefly explained each mentioned component:
Action Pack [VC] | contains Action Controller, Action View and Action Dispatch components. |
Action Dispatch [C] | handles incoming requests and dispatcher them to the application any other Rack application (Rails Engine, Sinatra and so on). |
Action Controller [C] | processes incoming requests, extracts parameters, and dispatches them to the given action. It also cares about session management, template rendering and redirects to another controller or own action in the controller. |
Action View [V] | manages rendering templates, including nested and partial templates. The default templating language is Embedded Ruby (ERB). |
Action Mailer | is a framework for receiving and processing incoming emails and sending emails – multipart html plain with attachments. |
Active Model [M] | provides a defined interface between Action Pack's parts and object-data mapping (ORM – object-relationship mapping, ODM – object-document mapping) libraries such as Active Record, DataMapper, CouchREST, Mongoid and so on. |
Active Record [M] | is a default object-relationship mapping based on Active Model. It provides basic CRUD (Create-Read-Update-Destroy) operations, advanced finding capabilities relating models to one another, migrations and other capabilities. |
Active Resource [M] | is for managing the connection between business objects and RESTful web services based on CRUD semantics. |
Active Support | provides Ruby language extensions, utilities and other stuff, used by other components |
Railties | is the core Rails code and glues each components and other part such as plugins together. |
Before creating a first application in Ruby On Rails it is needed to have installed the following dependencies:
- Ruby 1.8.7 and later (for Rails 3.1 is recommended to use Ruby 1.9.2)
- RubyGems (built in Ruby since 1.9.x)
- Ruby on Rails –
gem install rails
Rails has a command line tool for wide usage. This critical part of usage for each Rails developer and their everyday job. There is a guide A Guide to The Rails Command Line on the official page explained things more deeply.
To generate application, is needed to enter the following command to a terminal:
rails new new_application
It creates a basic application skeleton. The following table (taken from Getting Started with Rails and edited) briefly explains an each part:
File / Folder | Purpose |
---|---|
Gemfile | This file allows to specify what gem dependencies are needed for a Rails application. |
README | This is a brief instruction manual for the application. |
Rakefile | This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, own tasks should be added to the lib/tasks directory of the application. |
app/ | Contains the controllers, models, views and assets for the application. |
config/ | Configure your application’s runtime rules, routes, database, and more. |
config.ru | Rack configuration for Rack based servers used to start the application. |
db/ | Shows the current database schema, as well as the database migrations. |
doc/ | In-depth documentation for the application. |
lib/ | Extended modules for the application |
log/ | Application log files |
public/ | The only folder seen to the world as-is. Contains the static files and compiled assets. |
script/ | Contains the rails script that starts the application and can contain other scripts used to deploy or run the application. |
test/ | Unit tests, fixtures, and other test apparatus |
tmp/ | Temporary files |
vendor/ | A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if it is included) and plugins containing additional prepackaged functionality. |
Configuring Rails Applications
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Rails for the dependency management uses Bundler. When
source 'http://rubygems.org'
gem 'rails', '3.1.1'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :test do
gem 'turn', require: false
end