Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch 4.1-4.5 February 24 2014 CSCE 740 Software Engineering."— Presentation transcript:

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

2 – 2 – CSCE 740 Spring 2014 Tools - Last Time  Ruby Basics  Ruby Regexp New Test 1 Revisit Chapter 2  Classes  Objects Rails  xxx Next Time:

3 – 3 – CSCE 740 Spring 2014 http://gorails.com/setup/ubuntu/13.10 github.com:saasbook/courseware

4 – 4 – CSCE 740 Spring 2014  Rails exposes the client-server, three-tier architecture, and model–view–controller patterns, all of which are common in SaaS apps.  Rails’ ActiveRecord package uses Ruby’s metaprogramming and convention over configuration to free you from writing any code at all to perform the basic Create, Read, Update and Delete (CRUD) operations on your models, as long as you follow certain conventions about naming classes and variables. Engineering Software as a Service, Patterson & Fox

5 – 5 – CSCE 740 Spring 2014  Rails’ ActionView and ActionController packages provide help for creating Web pages, dealing with fill- in forms, and setting up the routes that map URIs to controller actions (code in your app).  A properly-constructed Rails app can be easily adapted to work in a service-oriented architecture, communicating with external services rather than with a human using a browser.  Debugging SaaS requires understanding the different places something could go wrong during the flow of a SaaS request, and making that information visible to the developer. Engineering Software as a Service, Patterson & Fox

6 – 6 – CSCE 740 Spring 2014 4.1 Rails Basics: From Zero to CRUD Rails is a SaaS application framework that defines a particular structure for organizing your application’s code and provides an interface to a Rails application server such as Rack. The app server waits for a Web browser to contact your app and maps every incoming request (URI and HTTP method) to a particular action in one of your app’s controllers. Three main modules make up the heart of Rails’ support for MVC:  ActiveRecord for creating models,  ActionView for creating views, and  ActionController for creating controllers. Engineering Software as a Service, Patterson & Fox

7 – 7 – CSCE 740 Spring 2014 Basic Rails application with a single model 1.Creating the skeleton of a new app 2.Routing 3.The database and migrations 4.Models and Active Record 5.Controllers, views, forms, and CRUD Engineering Software as a Service, Patterson & Fox

8 – 8 – CSCE 740 Spring 2014 Rails command apt-get install rails or aptitude install rails to install rails –help rails –version rails new myrottenpotatoes -T Engineering Software as a Service, Patterson & Fox

9 – 9 – CSCE 740 Spring 2014 Ruby libraries are called Rubygems Rubygems is a system for managing external user- contributed Ruby libraries or gems. Bundler, a gem, looks for a Gemfile in the app’s root directory that specifies not only what gems your app depends on, but what versions of those gems rails is itself a gem Engineering Software as a Service, Patterson & Fox

10 – 10 – CSCE 740 Spring 2014 Configuring your Gemfile # use Haml for templates gem 'haml' # use Ruby debugger group :development, :test do gem 'debugger' gem 'debugger'endThen bundle install --without production, Engineering Software as a Service, Patterson & Fox

11 – 11 – CSCE 740 Spring 2014 automation for repeatability automation for repeatability:  rather than manually installing the gems your app needs,  listing them in the Gemfile and letting Bundler install them automatically ensures that the task can be repeated consistently in a variety of environments, Engineering Software as a Service, Patterson & Fox

12 – 12 – CSCE 740 Spring 2014 RESTful routes routes – specify the mapping from HTTP method to controller action  rake routes – prints the routes RESTful routes specify self-contained requests of what operation to perform and what entity, or resource, to perform it on Rails shortcut that creates RESTful routes for the four basic CRUD actions (Create, Read, Update, Delete) on a model  http://guides.rubyonrails.org/routing.html#connectin g-urls-to-code Engineering Software as a Service, Patterson & Fox

13 – 13 – CSCE 740 Spring 2014 log/development.log log/development.log is where you look to find detailed error information when something goes wrong. Engineering Software as a Service, Patterson & Fox

14 – 14 – CSCE 740 Spring 2014 Edit config/routes.rb, replace with Myrottenpotatoes::Application.routes.draw do resources :movies resources :movies root :to => redirect('/movies') root :to => redirect('/movies')end run rake routes again Engineering Software as a Service, Patterson & Fox

15 – 15 – CSCE 740 Spring 2014 Using convention over configuration  Rails will expect this controller’s actions to be defined in the class MoviesController,  if that class isn’t defined at application start time, Rails will try to load it from the file app/controllers/movies_controller.rb.  Sure enough, if you now reload the page http://localhost:3000/movies in your browser, you should see a different error: uninitialized constant MoviesController. Engineering Software as a Service, Patterson & Fox

16 – 16 – CSCE 740 Spring 2014 The root route ’/’, RottenPotatoes’ “home page,” will take us to the main Movie listings page by a mechanism we’ll soon see called an HTTP redirect. Engineering Software as a Service, Patterson & Fox

17 – 17 – CSCE 740 Spring 2014 Summary:  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

18 – 18 – CSCE 740 Spring 2014 ELABORATION: Automatically reloading the app After changing routes.rb, you don’t have to stop and restart the app in order for the changes to take effect. In development mode, Rails reloads all of the app’s classes on every new request, so that your changes take effect immediately. In production this would cause serious performance problems, so Rails provides ways to change various app behaviors between development and production mode, Engineering Software as a Service, Patterson & Fox

19 – 19 – CSCE 740 Spring 2014 Non-resource based routes Route: get ’:controller/:action/:id’ or get ’photos/preview/:id’ Example URI: /photos/preview/3 Behavior: call PhotosController#preview params[]: {:id=>3} Route: get ’photos/preview/ :id’ Example URI: /photos/look/3?color=true Behavior: no route will match (look doesn’t match preview) Engineering Software as a Service, Patterson & Fox

20 – 20 – CSCE 740 Spring 2014 Route: get ’photos/:action/:id’ Example URI: /photos/look/3?color=true Behavior: call PhotosController#look (look matches :action) params[]: {:id=>3, :color=>’true’} Route: get ’:controller/:action/:vol/:num’ Example URI: /magazines/buy/3/5?newuser=true&discount=2 Behavior: call MagazinesController#buy params[]: {:vol=>3, :num=>5, :newuser=>’true’, :discount=>’2’. Engineering Software as a Service, Patterson & Fox

21 – 21 – CSCE 740 Spring 2014 Rails Routing from the Outside In This guide covers the user-facing features of Rails routing. After reading this guide, you will know:  How to interpret the code in routes.rb.  How to construct your own routes, using either the preferred resourceful style or the match method.  What parameters to expect an action to receive  How to automatically create paths and URLs using route helpers.  Advanced techniques such as constraints and Rack endpoints.… http://guides.rubyonrails.org/routing.html

22 – 22 – CSCE 740 Spring 2014 Databases and Migrations persistence tier uses relational DBMS sqlite3 for development (note WEBrick is webserver-lite) Each environment has its own separate DB  specified in config/database.yml  testing DB only for automated tests (hands off) The production environment should  be a higher performance DB (postgress)  mirror changes to the development DB Engineering Software as a Service, Patterson & Fox

23 – 23 – CSCE 740 Spring 2014 Migrations So how do we update the production DB in same way as the development DB? Remember DRY? A migration is a portable script for changing the DB schema in a consistent and repeatable way Engineering Software as a Service, Patterson & Fox

24 – 24 – CSCE 740 Spring 2014 Migration a 3-Step Process 1.Create a migration describing what changes to make. As with rails new, Rails provides a migration generator that gives you the boilerplate code, plus various helper methods to describe the migration. 2.Apply the migration to the development database. Rails defines a rake task for this. 3.Assuming the migration succeeded, update the test database’s schema by running rake db:test:prepare. 4.Run your tests, and if all is well, apply the migration to the production database and deploy the new code to production.  The process for applying migrations in production depends on the deployment environment; Engineering Software as a Service, Patterson & Fox

25 – 25 – CSCE 740 Spring 2014 Migration Management db/migrate name = time-created + user-supplied-name meaningful to both you and rails Engineering Software as a Service, Patterson & Fox

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

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

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

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

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

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


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

Similar presentations


Ads by Google