web architecture : 2nd week handout
TRANSCRIPT
Web Application ArchitecturesModule 2: Ruby on Rails
Module Overview
c© 2011-13 G.L. Heileman Module 2: Overview 1 / 2
Module 2 Overview
Rails OverviewYour First Rails AppThe Blog App – Iteration 1Rails PhilosophyVersion ControlGit and Rails
c© 2011-13 G.L. Heileman Module 2: Overview 2 / 2
Web Application ArchitecturesModule 2: Ruby on RailsLecture 1: Rails Overview
c© 2011-13 G.L. Heileman Module 2, Lecture 1 1 / 6
Rails Background
Ruby on Rails (Rails) is a software framework for building Webapplications.
Features:Built using the Ruby programming language.Open source – MIT License.Provides a full stack – back-end data store to front-end presentationof web pages, and everything in between.Released in 2004, and has continued to evolve rapidly.
Some sites built using Rails:Twitter, Hulu, GitHub,Yellow Pages and Funny or Die
c© 2011-13 G.L. Heileman Module 2, Lecture 1 2 / 6
Rails – What’s Inside
Rails is a Ruby gem (a gem is a prepackaged Ruby application orlibrary).Rails includes an extensive set of code generators, automated testingscripts and other features that are intended to make the job ofprogramming a web application easier.A suite of additional tools are provided as part of the Rails ecosystemthat make it easy to deploy a fully-functional web application:
Rake – comes from RubyMAKE, i.e., it’s a utility similar to the Unixmake. You use it to create and migrate databases, clear web sessiondata, etc.WEBrick – web server for hosting Rails web applications. Can also useother web servers, e.g., Apache, Thin, Unicorn, etc.SQLite – a simple database engine pre-installed with Rails.Rack Middleware – standardized interface for interaction between webserver and web application.
c© 2011-13 G.L. Heileman Module 2, Lecture 1 3 / 6
Model-View-Controller
The Rails framework is built around the Model-View-Controller (MVC)design pattern:
WebClient
(Browser)
WebServerNetwork
1: request
2: response
Database
Script/Service
Script/Service
Script/Service
Connector
DatabaseConnector
Clienttier
Webtier
Businesslogictier
Presentation logictier
Dataaccess
tier
Datatier
MVC Design Pattern
c© 2011-13 G.L. Heileman Module 2, Lecture 1 4 / 6
Rails Criticisms
It doesn’t scale – High profile issues at Twitter.
It’s magic – The framework and code generation means you really don’tknow what’s going on “under the hood.”
My Answer – It’s all a part of the natural progression:
Bits −→ Assembly −→ HLL −→ OOP −→ Frameworks
c© 2011-13 G.L. Heileman Module 2, Lecture 1 5 / 6
Efficiency vs. Productivity
“The point is that the cost per request is plummeting, but the cost ofprogramming is not. Thus, we have to find ways to trade efficiency in theruntime for efficiency in the ‘thought time’ in order to make thedevelopment of applications cheaper. I believed we’ve long since entered anage where simplicity of development and maintenance is where the realvalue lies.”
— David Heinemeier Hansson
c© 2011-13 G.L. Heileman Module 2, Lecture 1 6 / 6
Web Application ArchitecturesModule 2: Ruby on Rails
Lecture 2: Your First Rails App
c© 2011-13 G.L. Heileman Module 2, Lecture 2 1 / 3
It’s Time to Get to Work
On a machine with a working Rails installation:1. Open up a terminal window, and at the command prompt ($) type:
$ rails new my_app
2. Change to the application directory (RAILS.root):
$ cd my_app
3. Start the web server:
$ rails server
4. Open a browser window and navigate to:http://localhost:3000
c© 2011-13 G.L. Heileman Module 2, Lecture 2 2 / 3
Rails Directory Structure
RAILS.rootapp
binconfig
db
Gemfile
liblog
public
testtmp
vendor
RAILS.root my_app
app Models, Views and Controllers code
bin Helper scripts (bundle, rails, rake)
config App, database & route configuration
db Database schema and migrations
Gemfile Specify the required gems
logApplication logging directory
public Webroot of the application
test Tests - Agile development
c© 2011-13 G.L. Heileman Module 2, Lecture 2 3 / 3
Web Application ArchitecturesModule 2: Ruby on Rails
Lecture 3: The Blog App – Iteration 1
c© 2011-13 G.L. Heileman Module 2, Lecture 3 1 / 5
Blog App – Specifications
Let’s build a more substantial web application.
The Blog Application, usage specficiations:– Blog is a contraction of “weblog,” a discussion or information site
published on the Web.– There are two types of users: the blog administrator and blog users.– The blog administrator should be able to enter new postings, typically
in reverse chronological order.– Users should be able to visit the blog site, and enter comments about
particular postings.– The blog administrator should be able to modify/remove any posting
or comment.– Users should not be able to modify postings or other users’ comments.
c© 2011-13 G.L. Heileman Module 2, Lecture 3 2 / 5
Blog App – Initial Steps
First create the rails application using:
$ rails new blog
Next, in the blog app directory, use the scaffold generator to createthe MVC components needed for posts and comments:
$ rails generate scaffold post title:string \body:text
$ rails generate scaffold comment post_id:integer \body:text
Now, create the post and comment database tables using:
$ rake db:migrate
c© 2011-13 G.L. Heileman Module 2, Lecture 3 3 / 5
Blog App – Initial Steps
If you type:
$ rake routes
a list of all the URLs currently recognized by your application will beprovided.
Start the web server:
$ rails server
and point your browser to: http://localhost:3000
c© 2011-13 G.L. Heileman Module 2, Lecture 3 4 / 5
Blog App – Initial Steps
At this point, I’d like you to create this blog application on your own, anddemonstrate that you can perform CRUD operations on posts andcomments.
– Create– Read– Update– Destroy
c© 2011-13 G.L. Heileman Module 2, Lecture 3 5 / 5
Web Application ArchitecturesModule 2: Ruby on Rails
Lecture 4: Rails Philosophy
c© 2011-13 G.L. Heileman Module 2, Lecture 4 1 / 5
Rails Philosophy
The Rails philosophy is based upon three principles:Convention over Configuration – Common aspects of a webapplication are provided (i.e., preconfigured) for you, so use them,rather than fight against them! Ideally, the developer should only haveto specify the unconventional aspects of the application.Don’t Repeat Yourself (DRY) – Every piece of information shouldhave a single, unambiguous, authoritative representation within asystem. Duplication of code fragments throughout an application canlead to logical contradictions, and in general make the applicationmore difficult to maintain.Agile Development – Software development methodologies based oniterative and incremental development, where requirements and codeevolve with minimal planning through self-organizing, cross-functionalteams.
c© 2011-13 G.L. Heileman Module 2, Lecture 4 2 / 5
Convention over Configuration
A massive number of “conventions” are built into Rails, and the realtrick is learning all of them. When you create a Rails application, thefull web stack is “pre-wired” and ready to go.
Rails code generators follow specific naming conventions. E.g., if youuse the scaffold (MVC) generator to create a Post, you get:
– A class called Post will be created for the model, along with acorresponding table in the database that will be called posts
– A RESTful controller called posts_controller will be created, androutes will be set up so that specific URLs will be able to performCRUD operations on the post table in the database.
– A posts view will be created, consisting of a set of HTML files thatcan be used to render the results of the CRUD operations.
– Test fixtures, along with unit, functional, integration and performancetest suites are automatically generated.
c© 2011-13 G.L. Heileman Module 2, Lecture 4 3 / 5
DRY
Every piece of information should have a single, unambiguous, authoritativerepresentation within a system.E.g., In Rails, with ActiveRecords, once a model is specified, you don’t needto specify database column names –they’re determined from the model.
When applied successfully, a modification to a system element doesnot change any other logically-unrelated system element, whileelements that are logically related all change predictably anduniformly, thereby keeping them in “sync”.This principle makes it easier to use code generators and automaticbuild systems.Thus, DRY code is typically created by data transformations and codegenerators, allowing the software developer to avoid copy and pasteoperations.Following the DRY principle makes it easier to maintain large softwaresystems.
c© 2011-13 G.L. Heileman Module 2, Lecture 4 4 / 5
Agile Development
Agile development emphasizes working software as the primarymeasure of progress.Rails was built with agile development in mind:
– A working application is available immediately.– In development mode, there are no recompile, deploy, restart cycles.
I.e., Rails does not generally require you to stop the server; changesmade to the application will be automatically picked up by the server.
– Rails has simple tools to generate code quickly.– Testing is built into the Rails framework.
Extreme programming is an agile approach that that centers aroundtest-driven development (TDD). Behavior-driven development (BDD),a second generation agile approach, extends TDD by writing test casesin natural language that non-programmers can read. BDD focuses onobtaining a clear understanding of desired software behavior throughdiscuss with stakeholders.RSpec and Cucumber are BDD tools for Ruby that we’ll use.
c© 2011-13 G.L. Heileman Module 2, Lecture 4 5 / 5
Web Application ArchitecturesModule 2: Ruby on Rails
Lecture 5: Version Control
c© 2011-13 G.L. Heileman Module 2, Lecture 5 1 / 9
Version Control
Midnight Train to Georgia by Gladys Knight and the Pips:Considered the signature song of legend Gladys Knight.One of the greatest R&B songs of all time.Rolling Stone lists it as one of their 500 Greatest Songs of All Time.
The original title was Midnight Plane to Houston — about a plane tripfrom Nashville to Houston, and the author envisioned it as a country song.
c© 2011-13 G.L. Heileman Module 2, Lecture 5 2 / 9
Version Control
Definition (Version Control, Revision Control, Source Code Control)The practice of tracking and providing control over the changes made tosource code.
The need for managing software versions has existed as long as there’sbeen software.
Version control is commonly used by a development team to keeptrack of the various pieces of source code that different members ofthe project are working on, along with the various versions that theymay wish to create.
c© 2011-13 G.L. Heileman Module 2, Lecture 5 3 / 9
Local Version Control
A single developer:
Version 3
Local Computer
Version Database
checkout
Editor
file A
file A’ checkinfiles: A’, B, C’
Version 2files: A, B, C’
Version 1files: A, B, C
c© 2011-13 G.L. Heileman Module 2, Lecture 5 4 / 9
Centralized Version Control
A development team:
Developer 2
Version 3
Central VCS Server
Version Database
checkout
Developer 1
file Afiles: A’, B‘, C’
Version 2files: A, B, C’
Version 1files: A, B, C
file B
checkout
c© 2011-13 G.L. Heileman Module 2, Lecture 5 5 / 9
Centralized Version Control
Differences are stored:
file A
Version 1
file B
Version 2 Version 3 Version 4 Version 5
file C
checkins over time
Δ1
Δ1
Δ1
Δ2
Δ2 Δ3
c© 2011-13 G.L. Heileman Module 2, Lecture 5 6 / 9
Distributed Version Control
As software development has become more team-based, and andteams have become more distributed, the need for distributed versioncontrol systems has emerged.In distributed version control, there is no centralized repository, teammembers don’t check out files from these systems, they check out theentire repository.This does not lock the repository — other team members can alsodownload the current version of a project.Later, when a team member check the project back in, it is marked asa different version in the repository. Thus, you can think of distributedversion control systems as storing different snapshots of a project overtime.It is up to the owner of the repository to merge different versiontogether if he/she so chooses.
c© 2011-13 G.L. Heileman Module 2, Lecture 5 7 / 9
Distributed Version Control
Snaphshots are stored:
file A
Version 1
file B
Version 2 Version 3 Version 4 Version 5
file C
checkins over time
file C’
file A
file B
file C’
file A’
file B’
file C’’
file A’’
file B’
file C’’
file A’’’
file B’
c© 2011-13 G.L. Heileman Module 2, Lecture 5 8 / 9
Distributed Version Control
Distributed development:
Version 3
Server
Version Database
file A
files: A’, B‘, C’
Version 2files: A, B, C’
Version 1files: A, B, C
Version 3Version Database
files: A’, B‘, C’
Version 2files: A, B, C’
Version 1files: A, B, C
Developer 1
Editor
file A’ file AVersion 3
Version Database
files: A’, B‘, C’
Version 2files: A, B, C’
Version 1files: A, B, C
Developer 2
Editor
file A’
pull/push pull/push
pull/push
c© 2011-13 G.L. Heileman Module 2, Lecture 5 9 / 9
Web Application ArchitecturesModule 2: Ruby on RailsLecture 6: Git and Rails
c© 2011-13 G.L. Heileman Module 2, Lecture 6 1 / 6
Distributed Version Control with Git
Git is a popular open source distributed version control system createdin 2005 by the Linux development community and used extensively inthe Rails community.
Git provides means for comparing the differences between the variousversions of a project (think of them as project snapshots), and formerging them accordingly.
You should think of these versions as forming a tree:– Main development branch = master.– New versions may be created along the master branch, or new branches
can be created off of it.– These branches can possibly be merged back into the master branch.
c© 2011-13 G.L. Heileman Module 2, Lecture 6 2 / 6
Git Basics
Git tracks files, which may reside in three “locations”:– Working directory– Staging area– Git repository
Furthermore, the files in your working directory can be in one of twostates:
– Tracked: files that were saved to the Git repository by the last commitoperation.
– Untracked: all other files in your working directory.
Finally, tracked files can be either:– Modified– Unmodified– Staged
c© 2011-13 G.L. Heileman Module 2, Lecture 6 3 / 6
Git Workflow–Local Repository
stagingarea
local gitrepository
commit -a
add . commit
workingdirectory
checkout <branch>
stagingarea
merge <branch>
c© 2011-13 G.L. Heileman Module 2, Lecture 6 4 / 6
Git Workflow–Remote Repository
stagingarea
local gitrepository
remote gitrepository
commit -a
add . commit
push <remote> <branch>
clone <remote> or pull <remote>
fetch
workingdirectory
checkout <branch>
stagingarea
merge <branch>
c© 2011-13 G.L. Heileman Module 2, Lecture 6 5 / 6
Creating a Git Repository at Bitbucket
c© 2011-13 G.L. Heileman Module 2, Lecture 6 6 / 6