1 / 31

Lecture 11 Rails

Lecture 11 Rails. CSCE 740 Software Engineering. Topics SaaS Readings: SaaS book Ch 4.1-4.5. February 24 2014. Tools -. Last Time Ruby Basics Ruby Regexp. New Test 1 Revisit Chapter 2 Classes Objects Rails xxx Next Time :. http://gorails.com/setup/ubuntu/13.10

sonyav
Download Presentation

Lecture 11 Rails

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Tools - • Last Time • Ruby Basics • Ruby Regexp • New • Test 1 • Revisit Chapter 2 • Classes • Objects • Rails • xxx • Next Time:

  3. http://gorails.com/setup/ubuntu/13.10 • github.com:saasbook/courseware

  4. 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. 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. 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. Basic Rails application with a single model • Creating the skeleton of a new app • Routing • The database and migrations • Models and Active Record • Controllers, views, forms, and CRUD Engineering Software as a Service, Patterson & Fox

  8. 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. 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. Configuring your Gemfile • # use Haml for templates • gem 'haml' • # use Ruby debugger • group :development, :test do •   gem 'debugger' • end • Then • bundle install --without production, Engineering Software as a Service, Patterson & Fox

  11. 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. 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#connecting-urls-to-code Engineering Software as a Service, Patterson & Fox

  13. 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. Edit config/routes.rb, replace with • Myrottenpotatoes::Application.routes.draw do •   resources :movies •   root :to => redirect('/movies') • end • run rake routes again Engineering Software as a Service, Patterson & Fox

  15. 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. 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. 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 Hamltemplating 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. 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. Non-resource based routes • Route: get ’:controller/:action/:id’ or get ’photos/preview/:id’ • Example URI: /photos/preview/3 • Behavior: call PhotosController#previewparams[]: {: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. 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#buyparams[]: {:vol=>3, :num=>5, :newuser=>’true’, :discount=>’2’ • . Engineering Software as a Service, Patterson & Fox

  21. 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. 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. 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. Migration a 3-Step Process • 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. • Apply the migration to the development database. Rails defines a rake task for this. • Assuming the migration succeeded, update the test database’s schema by running rake db:test:prepare. • 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. Migration Management • db/migrate • name = time-created + user-supplied-name • meaningful to both you and rails Engineering Software as a Service, Patterson & Fox

  26. Engineering Software as a Service, Patterson & Fox

  27. Engineering Software as a Service, Patterson & Fox

  28. Engineering Software as a Service, Patterson & Fox

  29. Engineering Software as a Service, Patterson & Fox

  30. Engineering Software as a Service, Patterson & Fox

  31. Engineering Software as a Service, Patterson & Fox

More Related