31
Lecture 11 Rails Topics Topics SaaS SaaS Readings: Readings: SaaS book Ch SaaS book Ch 4.1-4.5 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

Embed Size (px)

Citation preview

Page 1: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

Lecture 11Rails

Lecture 11Rails

Topics Topics

• SaaSSaaS

Readings: Readings: SaaS book Ch SaaS book Ch 4.1-4.54.1-4.5

February 24 2014

CSCE 740 Software Engineering

Page 2: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 2 – CSCE 740 Spring 2014

Tools - Tools -

Last TimeLast Time

Ruby BasicsRuby Basics

Ruby RegexpRuby Regexp

New New

Test 1Test 1

Revisit Chapter 2Revisit Chapter 2

ClassesClasses

ObjectsObjects

RailsRails

xxxxxx

Next Time:Next Time:

Page 3: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 3 – CSCE 740 Spring 2014

http://gorails.com/setup/ubuntu/13.10

github.com:saasbook/courseware

Page 4: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 4 – CSCE 740 Spring 2014

Rails exposes the client-server, three-tier Rails exposes the client-server, three-tier architecture, and model–view–controller patterns, all architecture, and model–view–controller patterns, all of which are common in SaaS apps. of which are common in SaaS apps.

Rails’ ActiveRecord package uses Ruby’s Rails’ ActiveRecord package uses Ruby’s metaprogramming and convention over metaprogramming and convention over configuration to free you from writing any code at all configuration to free you from writing any code at all to perform the basic Create, Read, Update and Delete to perform the basic Create, Read, Update and Delete (CRUD) operations on your models, as long as you (CRUD) operations on your models, as long as you follow certain conventions about naming classes follow certain conventions about naming classes and variables. and variables.

Engineering Software as a Service, Patterson & Fox

Page 5: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 5 – CSCE 740 Spring 2014

Rails’ ActionView and ActionController packages Rails’ ActionView and ActionController packages provide help for creating Web pages, dealing with provide help for creating Web pages, dealing with fill-in forms, and setting up the routes that map URIs fill-in forms, and setting up the routes that map URIs to controller actions (code in your app). to controller actions (code in your app).

A properly-constructed Rails app can be easily A properly-constructed Rails app can be easily adapted to work in a service-oriented architecture, adapted to work in a service-oriented architecture, communicating with external services rather than communicating with external services rather than with a human using a browser. with a human using a browser.

Debugging SaaS requires understanding the Debugging SaaS requires understanding the different places something could go wrong during different places something could go wrong during the flow of a SaaS request, and making that the flow of a SaaS request, and making that information visible to the developer.information visible to the developer.

Engineering Software as a Service, Patterson & Fox

Page 6: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 6 – CSCE 740 Spring 2014

4.1 Rails Basics: From Zero to CRUD4.1 Rails Basics: From Zero to CRUDRails is a SaaS application framework that defines a Rails is a SaaS application framework that defines a

particular structure for organizing your application’s particular structure for organizing your application’s code and provides an interface to a Rails application code and provides an interface to a Rails application server such as Rack. server such as Rack.

The app server waits for a Web browser to contact your The app server waits for a Web browser to contact your app and maps every incoming request (URI and app and maps every incoming request (URI and HTTP method) to a particular action in one of your HTTP method) to a particular action in one of your app’s controllers.app’s controllers.

Three main modules make up the heart of Rails’ Three main modules make up the heart of Rails’ support for MVC: support for MVC:

ActiveRecord for creating models, ActiveRecord for creating models,

ActionView for creating views, and ActionView for creating views, and

ActionController for creating controllers.ActionController for creating controllers.Engineering Software as a Service, Patterson & Fox

Page 7: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 7 – CSCE 740 Spring 2014

Basic Rails application with a single modelBasic Rails application with a single model1.1. Creating the skeleton of a new app Creating the skeleton of a new app

2.2. Routing Routing

3.3. The database and migrations The database and migrations

4.4. Models and Active Record Models and Active Record

5.5. Controllers, views, forms, and CRUDControllers, views, forms, and CRUD

Engineering Software as a Service, Patterson & Fox

Page 8: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 8 – CSCE 740 Spring 2014

Rails commandRails command

apt-get install rails or aptitude install rails to installapt-get install rails or aptitude install rails to install

rails –helprails –help

rails –versionrails –version

rails new myrottenpotatoes -Trails new myrottenpotatoes -T

Engineering Software as a Service, Patterson & Fox

Page 9: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 9 – CSCE 740 Spring 2014

Ruby libraries are called RubygemsRuby libraries are called Rubygems

Rubygems is a system for managing external user-Rubygems is a system for managing external user-contributed Ruby libraries or gems. contributed Ruby libraries or gems.

Bundler, a gem, looks for a Gemfile in the app’s root Bundler, a gem, looks for a Gemfile in the app’s root directory that specifies not only what gems your app directory that specifies not only what gems your app depends on, but what versions of those gemsdepends on, but what versions of those gems

rails is itself a gemrails is itself a gem

Engineering Software as a Service, Patterson & Fox

Page 10: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 10 – CSCE 740 Spring 2014

Configuring your GemfileConfiguring your Gemfile

# use Haml for templates# use Haml for templates

gem 'haml'gem 'haml'

# use Ruby debugger# use Ruby debugger

group :development, :test dogroup :development, :test do

    gem 'debugger'gem 'debugger'

endend

ThenThen

bundle install --without production,bundle install --without production,

Engineering Software as a Service, Patterson & Fox

Page 11: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 11 – CSCE 740 Spring 2014

automation for repeatabilityautomation for repeatability

automation for repeatability: automation for repeatability:

rather than manually installing the gems your app rather than manually installing the gems your app needs, needs,

listing them in the Gemfile and letting Bundler install listing them in the Gemfile and letting Bundler install them automatically ensures that the task can be them automatically ensures that the task can be repeated consistently in a variety of environments,repeated consistently in a variety of environments,

Engineering Software as a Service, Patterson & Fox

Page 12: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 12 – CSCE 740 Spring 2014

RESTful routesRESTful routes

routes – specify the mapping from HTTP method to routes – specify the mapping from HTTP method to controller actioncontroller action

rake routes – prints the routesrake routes – prints the routes

RESTful routes specify self-contained requests of what RESTful routes specify self-contained requests of what operation to perform and what entity, or resource, to operation to perform and what entity, or resource, to perform it on perform it on

Rails shortcut that creates RESTful routes for the four Rails shortcut that creates RESTful routes for the four basic CRUD actions (Create, Read, Update, Delete) basic CRUD actions (Create, Read, Update, Delete) on a modelon a model

http://guides.rubyonrails.org/http://guides.rubyonrails.org/routing.html#connecting-urls-to-coderouting.html#connecting-urls-to-code

Engineering Software as a Service, Patterson & Fox

Page 13: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 13 – CSCE 740 Spring 2014

log/development.loglog/development.log

log/development.log is where you look to find detailed log/development.log is where you look to find detailed error information when something goes wrong.error information when something goes wrong.

Engineering Software as a Service, Patterson & Fox

Page 14: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 14 – CSCE 740 Spring 2014

Edit config/routes.rb, replace withEdit config/routes.rb, replace with

Myrottenpotatoes::Application.routes.draw doMyrottenpotatoes::Application.routes.draw do

    resources :moviesresources :movies

    root :to => redirect('/movies')root :to => redirect('/movies')

endend

run rake routes againrun rake routes again

Engineering Software as a Service, Patterson & Fox

Page 15: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 15 – CSCE 740 Spring 2014

Using convention over configurationUsing convention over configuration

Rails will expect this controller’s actions to be Rails will expect this controller’s actions to be defined in the class MoviesController,defined in the class MoviesController,

if that class isn’t defined at application start time, if that class isn’t defined at application start time, Rails will try to load it from the file Rails will try to load it from the file app/controllers/movies_controller.rb. app/controllers/movies_controller.rb.

Sure enough, if you now reload the page Sure enough, if you now reload the page http://localhost:3000/movies in your browser, you http://localhost:3000/movies in your browser, you should see a different error: uninitialized constant should see a different error: uninitialized constant MoviesController.MoviesController.

Engineering Software as a Service, Patterson & Fox

Page 16: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 16 – CSCE 740 Spring 2014

The root route ’/’, RottenPotatoes’ “home page,” will The root route ’/’, RottenPotatoes’ “home page,” will take us to the main Movie listings page by a take us to the main Movie listings page by a mechanism we’ll soon see called an HTTP redirect.mechanism we’ll soon see called an HTTP redirect.

Engineering Software as a Service, Patterson & Fox

Page 17: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 17 – CSCE 740 Spring 2014

Summary:Summary: commands to set up a new Rails app: commands to set up a new Rails app:

rails new sets up the new app; the rails command also has subcommands to run the app locally with WEBrick (rails server) and other management tasks.

Rails and the other gems your app depends on (we added the Haml templating system and the Ruby debugger) are listed in the app’s Gemfile, which

Bundler uses to automate the process of creating a consistent environment for your app whether in development or production mode.

To add routes in config/routes.rb, the one-line resources method provided by the Rails routing system allowed us to set up a group of related routes for CRUD actions on a RESTful resource.

The log files in the log directory collect error information when something goes wrong.

Engineering Software as a Service, Patterson & Fox

Page 18: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 18 – CSCE 740 Spring 2014

ELABORATION: Automatically reloading the appELABORATION: Automatically reloading the appAfter changing routes.rb, you don’t have to stop and After changing routes.rb, you don’t have to stop and

restart the app in order for the changes to take restart the app in order for the changes to take effect. effect.

In development mode, Rails reloads all of the app’s In development mode, Rails reloads all of the app’s classes on every new request, so that your changes classes on every new request, so that your changes take effect immediately. take effect immediately.

In production this would cause serious performance In production this would cause serious performance problems, so Rails provides ways to change various problems, so Rails provides ways to change various app behaviors between development and production app behaviors between development and production mode,mode,

Engineering Software as a Service, Patterson & Fox

Page 19: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 19 – CSCE 740 Spring 2014

Non-resource based routesNon-resource based routes

Route: get ’:controller/:action/:id’ or Route: get ’:controller/:action/:id’ or get ’photos/preview/:id’ get ’photos/preview/:id’

Example URI: /photos/preview/3 Example URI: /photos/preview/3

Behavior: call PhotosController#preview params[]: Behavior: call PhotosController#preview params[]: {:id=>3}{:id=>3}

Route: get ’photos/preview/ :id’ Route: get ’photos/preview/ :id’

Example URI: /photos/look/3?color=true Example URI: /photos/look/3?color=true

Behavior: no route will match (look doesn’t match Behavior: no route will match (look doesn’t match preview) preview)

Engineering Software as a Service, Patterson & Fox

Page 20: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 20 – CSCE 740 Spring 2014

Route: get ’photos/:action/:id’ Route: get ’photos/:action/:id’

Example URI: /photos/look/3?color=true Example URI: /photos/look/3?color=true

Behavior: call PhotosController#look (look Behavior: call PhotosController#look (look matches :action) params[]: {:id=>3, :color=>’true’} matches :action) params[]: {:id=>3, :color=>’true’}

Route: get ’:controller/:action/:vol/:num’ Route: get ’:controller/:action/:vol/:num’

Example URI: /magazines/buy/3/5?Example URI: /magazines/buy/3/5?newuser=true&discount=2 newuser=true&discount=2

Behavior: call MagazinesController#buy params[]: {:vol=>3, Behavior: call MagazinesController#buy params[]: {:vol=>3, :num=>5, :newuser=>’true’, :discount=>’2’:num=>5, :newuser=>’true’, :discount=>’2’

. . Engineering Software as a Service, Patterson & Fox

Page 21: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 21 – CSCE 740 Spring 2014

Rails Routing from the Outside InRails Routing from the Outside In

This guide covers the user-facing features of Rails This guide covers the user-facing features of Rails routing.routing.

After reading this guide, you will know:After reading this guide, you will know:

How to interpret the code in routes.rb.How to interpret the code in routes.rb.

How to construct your own routes, using either the How to construct your own routes, using either the preferred resourceful style or the match method.preferred resourceful style or the match method.

What parameters to expect an action to receiveWhat parameters to expect an action to receive

How to automatically create paths and URLs using How to automatically create paths and URLs using route helpers. route helpers.

Advanced techniques such as constraints and Rack Advanced techniques such as constraints and Rack endpoints.…endpoints.…

http://guides.rubyonrails.org/routing.html

Page 22: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 22 – CSCE 740 Spring 2014

Databases and MigrationsDatabases and Migrations

persistence tier uses relational DBMSpersistence tier uses relational DBMS

sqlite3 for developmentsqlite3 for development

(note WEBrick is webserver-lite)(note WEBrick is webserver-lite)

Each environment has its own separate DBEach environment has its own separate DB

specified in config/database.ymlspecified in config/database.yml

testing DB only for automated tests (hands off)testing DB only for automated tests (hands off)

The production environment shouldThe production environment should

be a higher performance DB (postgress)be a higher performance DB (postgress)

mirror changes to the development DB mirror changes to the development DB

Engineering Software as a Service, Patterson & Fox

Page 23: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 23 – CSCE 740 Spring 2014

MigrationsMigrations

So how do we update the production DB in same way So how do we update the production DB in same way as the development DB?as the development DB?

Remember DRY? Remember DRY?

A migration is a portable script for changing the DB A migration is a portable script for changing the DB schema in a consistent and repeatable wayschema in a consistent and repeatable way

Engineering Software as a Service, Patterson & Fox

Page 24: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 24 – CSCE 740 Spring 2014

Migration a 3-Step ProcessMigration a 3-Step Process

1.1. Create a migration describing what changes to Create a migration describing what changes to make. As with rails new, Rails provides a migration make. As with rails new, Rails provides a migration generator that gives you the boilerplate code, plus generator that gives you the boilerplate code, plus various helper methods to describe the migration. various helper methods to describe the migration.

2.2. Apply the migration to the development database. Apply the migration to the development database. Rails defines a rake task for this. Rails defines a rake task for this.

3.3. Assuming the migration succeeded, update the test Assuming the migration succeeded, update the test database’s schema by running rake db:test:prepare. database’s schema by running rake db:test:prepare.

4.4. Run your tests, and if all is well, apply the migration Run your tests, and if all is well, apply the migration to the production database and deploy the new code to the production database and deploy the new code to production. to production.

The process for applying migrations in production The process for applying migrations in production depends on the deployment environment;depends on the deployment environment;

Engineering Software as a Service, Patterson & Fox

Page 25: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 25 – CSCE 740 Spring 2014

Migration ManagementMigration Management

db/migratedb/migrate

name = time-created + user-supplied-namename = time-created + user-supplied-name

meaningful to both you and railsmeaningful to both you and rails

Engineering Software as a Service, Patterson & Fox

Page 26: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 26 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox

Page 27: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 27 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox

Page 28: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 28 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox

Page 29: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 29 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox

Page 30: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 30 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox

Page 31: Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering

– 31 – CSCE 740 Spring 2014Engineering Software as a Service, Patterson & Fox