Introducing Rails

Download Report

Transcript Introducing Rails

Web Science Stream
Introducing Rails
Dr Alexiei Dingli
1
What is Rails?
• Open source web application
framework for Ruby
• Intended for agile programming
• Create by David Heinemeier
Hansson
• Released originally in 2004 but only
became popular 2007 when it was
distributed with Mac OS X Leopard
2
3 Environments
• Development
• Testing
• Production
3
Development
• Changes to source code immediately
visible (just reload)
• Speed is not critical
• Provide insight to developer
– Errors are easily spotted by showing the stack
trace
4
Testing
• Fill database using dummy data
• Unit and functional testing are fully
automated
• Invoked from the command line
5
Production
• Update to code base infrequent
• Production environment can be optimised
• Focus on performance
• Errors can be sent by email to the
developer
6
Setting our database
/config/Database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
timeout: 5000
production:
adapter: mysql
database: newMySqlProductionDB
username: root
password: superSecretPassword
timeout: 5000
7
Database architecture
Database
Server
Development
Table 1
Table 2
Test
Table 1
Production
Table 2
Table 1
Table 2
8
Model-View-Controller
Architecture
• Models
– Handling data and business logic
• Controllers
– Handling the user interface and application
logic
• Views
– Handling GUI and persistent objects
9
MVC Process
10
Why MVC?
• Improves scalability
– Upgrade parts of the system without changing other
components (Eg just the database)
• Makes maintenance easier
– Components have low dependency on each other
• Promotes reuse
– Model may be reused by multiple views (or vice11
versa)
MVC in Rails
controllers
helpers
app
models
views
12
The model
• Called Active Records
– Connect to database
– Retrieve data from tables
– Store data in tables
• Abstracts from the database
– Can connect immediately to SQLite, MySQL
and PostgreSQL
13
– RubyGems provide access to other
Databases
Database Abstraction
• Active Records abstract database specific
code thus making it easy to shift between
different databases
• Another magic of Rails is migration which
saves us from creating any SQL
14
The controller
• Active Controller
– Is the glue between the application data,
presentation layer and the web browser
– Decides how to handle a request
– Retrieve data from the model to be passed
– Gather information from browser request
15
Some conventions
• Class names should be written with each
word beginning with a capital letter and no
spaces between words
– StoryBook
– AgeCalculator
• Filenames are written in lowercase with
undderscores separating each word
– story_book
– age_calculator
16
The View
• Action View
– Should only contain presentation logic
– Can contain HTML and also Ruby code using
the embded Ruby (ERb) syntax
<b><%= “Hello World!” %></b>
17
Views conventions
• The template has a one-to-one mapping to
the action of a controller
– Add should be in Add.html.erb
• The folder that stores the template should
be named after the controller
18
Views extensions
html.erb
Standard HTML template which might contan ERb
tags
xml.builder
Output XML (Eg RSS feeds)
js.rjs
Return JavaScript instructions
19
Lets have some REST
• REpresentational State Transfer
– Design pattern not restricted to the WWW
– Offers a resource centric approach where
• Client uses a resource
• Every resource needs to be addressed uniquely
• Interactions with resources are exposed as
operations
• Most common are the CRUD operations
– Create, Read, Update, Delete
20
More REST
• The protocol used is
– Stateless
• Each request to the server is completely
independent
– Cacheable
• Proxies
– Layered
• Routers
• Firewalls
21
RESTing on the WWW
CRUD
HTTP
CREATE
POST
READ
GET
UPDATE
PUT (Not implemented)
DELETE
DELETE (Not implemented)
22
The REST of Rails
• Deep relationship to provide assistance for
easy construction of URLs
• Rails uses modified POST requests to
implement the PUT and DELETE methods
23
Magical Generation
• Rails generate the application’s structure
easily
• Simply use the generate script
• Try
– ruby script/generate
24
Rails Philosophy (1)
• Convention over configuration
– Developers only specify unconventional
aspects
– Eg if we have a class Sale in the model, the
table will be called sales automatically
– Leads to less code and less repetition
– If the convention is not kept then more
programming is needed
25
Rails Philosophy (2)
• Don’t Repeat Yourself (DRY)
– Information located in a single unambiguous
place
– Eg using Active Records, no columns or
tables are specified, everything is done
automatically.
26
Questions?
27