CMPE/SE 131 Software Engineering February 2 Class Meeting

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Advertisements

The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Chapter 15 © 2010 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
CS 174: Web Programming April 28 Class Meeting
What is MySQL? MySQL is a relational database management system (A relational database stores data in separate tables rather than putting all the data.
Introduction to Model-View-Controller (MVC) Web Programming with TurboGears Leif Oppermann,
27-Jun-15 Rails. What is Rails? Rails is a framework for building web applications This involves: Getting information from the user (client), using HTML.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
Rails and Grails. To get started Make sure you have java installed You can get the sdk and jre at:
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Web Application Programming Carol Wolf Computer Science.
CS 160: Software Engineering October 8 Class Meeting
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch February CSCE 740 Software Engineering.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
The Active Record Paradigm Databases in Database-Centric Web Site Development.
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
© 2012 LogiGear Corporation. All Rights Reserved FitNesseFitNesse Authors: Nghia Pham 1.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS 160 and CMPE/SE 131 Software Engineering February 2 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
CMPE 226 Database Systems April 19 Class Meeting Department of Computer Engineering San Jose State University Spring 2016 Instructor: Ron Mak
Advanced Migration By Aye Mon Tun.  To change database schema in consistent and easy way  In ruby code Migration? 11/25/2013 2Web Application Engineering.
START Application Spencer Johnson Jonathan Barella Cohner Marker.
JRuby on Rails Presenter Name
JRuby on Rails Brian Leonard ブライアン レオナルド
CMPE/SE 131 Software Engineering September 1 Class Meeting Department of Computer Engineering San José State University Fall 2016 Instructor: Ron Mak
2nd year Computer Science & Engineer
Web Database Programming Using PHP
Fundamental of Databases
Development Environment
MVC Architecture, Symfony Framework for PHP Web Apps
Web Database Programming Using PHP
Basics on DB access Elke A. Rundensteiner.
Ruby Tooling in NetBeans
Introduction to Web programming
Introduction to Triggers
CMPE 280 Web UI Design and Development October 24 Class Meeting
PHP / MySQL Introduction
BASIC PHP and MYSQL Edward S. Flores.
SQL – Application Persistence Design Patterns
Cookies BIS1523 – Lecture 23.
…and web frameworks in general
Testing REST IPA using POSTMAN
CS 174: Server-Side Web Programming February 12 Class Meeting
Principles of report writing
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 15 Introduction to Rails.
Computer Science Projects Database Theory / Prototypes
Tutorial 6 PHP & MySQL Li Xu
…and web frameworks in general
CMPE/SE 131 Software Engineering February 7 Class Meeting
SQL – Application Persistence Design Patterns
CMPE/SE 131 Software Engineering March 7 Class Meeting
Web Application Development Using PHP
Presentation transcript:

CMPE/SE 131 Software Engineering February 2 Class Meeting Department of Computer Engineering San José State University Spring 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Finish Team Formation!

Ruby Batch Programs You can put a Ruby program into a text file. Run the program on the command line with the ruby command. Example: ruby Hello.rb

Simple Ruby Text Output Ruby has a printf function similar to C. Example: irb(main):009:0> i = 10 => 10 irb(main):010:0> str = "Foo" => "Foo" irb(main):011:0> printf("%5d %s\n", i, str) 10 Foo => nil

Simple Ruby Text Input Use File.open to open a text file for reading. Example: Use readline to read the next text line. irb(main):012:0> input = File.open("widgets.csv", "r") => #<File:widgets.csv> irb(main):013:0> input.readline => "STATE,PLANT,DEPT,EMPID,NAME,COUNT\n"

Simple Ruby Text I/O Loop to read and process one text line after another. Example: irb(main):014:0> input.each do |line| irb(main):015:1* puts line irb(main):016:1> end 12,34,56,789,George Carter,4 12,34,56,799,Mary Clinton,6 12,34,57,639,Alfred Lincoln,8 12,40,57,710,Kim Kennedy,8 12,40,57,990,Jina Johnson,6 12,40,75,426,Ruby Roosevelt,10 12,40,75,551,John Washington,7 33,22,11,297,Hilda Hoover,10 33,22,11,428,Ted Truman,11 33,22,11,808,Nora Nixon,3 33,22,14,629,Mabel Bush,9 33,27,19,321,Chris Adams,5 => #<File:widgets.csv>

Rails: A Full Stack Framework Includes everything you need to build a web application that: accepts user input queries databases responds with dynamically generated web pages

Assignment #1 Team assignment. Use Ruby on Rails to generate the first draft of your web project. home page plus other pages at least one form model, view, and controller components at least two database tables The application should be functional. You can change things later.

Assignment #1, cont’d Create a zip file containing: Rails commands to generate your application Brief instructions on how to run the application Screen shots of your web pages in action Submit the zip file into Canvas: Assignment #1 I will create group accounts for team submissions. Due Friday, Feb. 10 at 11:59 PM

Rails Mantra Convention over configuration Don’t repeat yourself (DRY) Rails expects a certain directory structure. Rails expects you to follow its naming conventions. If you do, “Everything should just work.” Don’t repeat yourself (DRY) Each part of a Rails web application has a single, specific place where it belongs. Things that can be inferred from another source don’t need to be specified anywhere.

Model-View-Controller Architecture (MVC) The Model-View-Controller (MVC) architecture is used for client-server applications that include a user interface. Well-designed web applications use the MVC architecture.

Three Types of MVC Objects Model objects Maintain the data and knowledge of your application. View objects Display the model to the user. The presentation layer. Controller objects Manage the application flow. Handle user interactions.

Model-View-Controller Operation The user interacts with the controller to send it commands. Via buttons, text fields, mouse actions, etc. The commands may tell the controller to modify the view directly, or the controller may alter the state of the model. The altered model causes the view to update how it displays the model’s data. The user may react to changes in the view by interacting with the controller to send new commands. The user never manipulates the model directly, only through the controller.

Model-View-Controller Example update update alter state send command VIEW #1 VIEW #2 User

Advantages of the MVC Architecture Well-established industry convention. Well-defined roles for the objects of a GUI-based client-server application. Once interfaces are designed and agreed upon, developers can work independently. MVC architectures are supported by web application frameworks like Rails.

Fast Start Rails will get the initial version of your web application up and running within minutes. Traditionally, getting that first version to work has been very difficult. Once you have something working, you can incrementally build the rest of the application. Always build on code that already works. And keep that code working!

The Rails Crash Course Example mkdir code cd code rails new blog We create a weblog app called blog. This will create the Rails directory structure and run bundle install to download all the required gems (Ruby packages). This takes several minutes.

Rails Directory Structure Rails has created a directory structure for us. Top level subdirectories of the main blog directory: Folder Purpose app Where most application files are kept. bin Scripts to run bundle, rails, rake, and other command-line programs. config For those unavoidable configuration files. db Files to create and populate database tables. lib Any reusable library code that you write. log Log files generated when your application is running. public Files that are sent to users as if they were in the application root directory. test Automated tests. tmp Temporary files. vendor Third-party gems.

The Rails Crash Course Example, cont’d cd blog bin/rails server Start Ruby’s built-in WEBrick web server. Windows: ruby bin/rails server http://localhost:3000

Rails Commands gem bundle rake Installs Ruby gems. Gems dependencies are maintained by the Bundler. bundle Interact with the Bundler to install and update gems needed by your application. bin/bundle list rake Automated build tool (think Unix make).

Rails Commands, cont’d rails Create an application. Start the web server. Generate new code. Start an interactive Ruby console that runs within the application’s environment. Start a command-line interface to the application’s database.

Database Migrations Ruby scripts that create and modify your database tables. Example: Add a column to a table. Stored in directory db/migrate. Each script is time-stamped (GMT) so that Rails can keep track of which migrations have already been run. bin/rake db:migrate to run migrations. File db/schema.rb contains the current database schema.

Generate Scaffolding Code A fast way to bootstrap an application! Generate a scaffold for a model called Post that will represent blog posts. Each post will have a title (a string) and a body (a text field). bin/rails generate scaffold Post title:string body:text

Generate Scaffolding Code, cont’d bin/rake db:migrate Run the initial database migration. Create a database table with fields: Go to: http://localhost:3000/posts The first version of the application is already up and running! id title body created_at updated_at

SQLite Rails by default uses the SQLite relational database manager (RDBM). The other popular web RDBM is MySQL. External tools to manage your databases: SQLite Studio SQLite Manager (Firefox browser add-on)

Object-Relational Mapping Ruby is object-oriented. The database is table row-oriented. Object-relational mapping (ORM) maps data between ruby objects and database table rows. Rails uses the Active Record design pattern to implement ORM for your application. There is a Ruby class for each database. Example: the Post class

The Rails Console bin/rails console Start an interactive Ruby console (the IRB). Your application’s environment is automatically loaded.

Create a Database Record The Ruby Post class represents our posts database table. Use the create method: irb(main):006:0> Post.create title: "Third post!", body: "This is my third post." (1.5ms) begin transaction SQL (0.4ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Third post!"], ["body", "This is my third post."], ["created_at", "2016-02-04 08:47:56.397971"], ["updated_at", "2016-02-04 08:47:56.397971"]] (3.1ms) commit transaction => #<Post id: 4, title: "Third post!", body: "This is my third post.", created_at: "2016-02-04 08:47:56", updated_at: "2016-02-04 08:47:56">

Create a Database Record, cont’d The create method is actually a shortcut. Don’t forget to call the save method. save returns true upon success. irb(main):007:0> post = Post.new => #<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil> irb(main):008:0> post.title = "Fourth post" => "Fourth post" irb(main):009:0> post.body = "This is my fourth post." => "This is my fourth post." irb(main):010:0> post.save (0.1ms) begin transaction SQL (19.9ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Fourth post"], ["body", "This is my fourth post."], ["created_at", "2016-02-04 08:52:05.818904"], ["updated_at", "2016-02-04 08:52:05.818904"]] (2.6ms) commit transaction => true

Read (Query) the Database The all method returns an array of records: Ask for the first record: irb(main):011:0> posts = Post.all Post Load (0.2ms) SELECT "posts".* FROM "posts" => #<ActiveRecord::Relation [#<Post id: 1, title: "First post!", body: "This is my very first post!", created_at: "2016-02-04 07:53:07", updated_at: "2016-02-04 07:53:15">, #<Post id: 2, title: "Second post!", body: "This is my second post.", created_at: "2016-02-04 07:53:37", updated_at: "2016-02-04 07:53:37">, #<Post id: 4, title: "Third post!", body: "This is my third post.", created_at: "2016-02-04 08:47:56", updated_at: "2016-02-04 08:47:56">, #<Post id: 5, title: "Fourth post", body: "This is my fourth post.", created_at: "2016-02-04 08:52:05", updated_at: "2016-02-04 08:52:05">]> irb(main):013:0> Post.first Post Load (0.8ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT 1 => #<Post id: 1, title: "First post!", body: "This is my very first post!", created_at: "2016-02-04 07:53:07", updated_at: "2016-02-04 07:53:15">

Read (Query) the Database, cont’d Ask for the last record: Query by record id: irb(main):015:0> Post.last Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1 => #<Post id: 5, title: "Fourth post", body: "This is my fourth post.", created_at: "2016-02-04 08:52:05", updated_at: "2016-02-04 08:52:05"> irb(main):012:0> post = Post.find 2 Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]] => #<Post id: 2, title: "Second post!", body: "This is my second post.", created_at: "2016-02-04 07:53:37", updated_at: "2016-02-04 07:53:37">

Read (Query) the Database, cont’d Query using the where method. Returns a relation, so also use the all, first, or last method. Query using the find_by shortcut method: irb(main):017:0> post = Post.where(title: "First post!").first Post Load (1.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."title" = ? ORDER BY "posts"."id" ASC LIMIT 1 [["title", "First post!"]] => #<Post id: 1, title: "First post!", body: "This is my very first post!", created_at: "2016-02-04 07:53:07", updated_at: "2016-02-04 07:53:15"> irb(main):018:0> post = Post.find_by title: "Second post!" Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."title" = ? LIMIT 1 [["title", "Second post!"]] => #<Post id: 2, title: "Second post!", body: "This is my second post.", created_at: "2016-02-04 07:53:37", updated_at: "2016-02-04 07:53:37">

Update the Database Make changes to the record variable, and then call the save method. The save method returns true upon success. irb(main):020:0> post = Post.find 4 Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 4]] => #<Post id: 4, title: "Third post!", body: "This is my third post.", created_at: "2016-02-04 08:47:56", updated_at: "2016-02-04 08:47:56"> irb(main):021:0> post.body = "My 3rd post." => "My 3rd post." irb(main):022:0> post.save (0.1ms) begin transaction SQL (18.5ms) UPDATE "posts" SET "body" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["body", "My 3rd post."], ["updated_at", "2016-02-04 09:22:53.133946"], ["id", 4]] (6.2ms) commit transaction => true

Update the Database, cont’d The update method does the save. Returns true upon success. irb(main):023:0> post = Post.find 5 Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 5]] => #<Post id: 5, title: "Fourth post", body: "This is my fourth post.", created_at: "2016-02-04 08:52:05", updated_at: "2016-02-04 08:52:05"> irb(main):024:0> post.update body: "My 4th post" (0.1ms) begin transaction SQL (19.2ms) UPDATE "posts" SET "body" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["body", "My 4th post"], ["updated_at", "2016-02-04 09:30:23.755547"], ["id", 5]] (2.8ms) commit transaction => true

Delete Records Method destroy deletes records. Returns the records that were deleted. irb(main):025:0> post = Post.find 2 Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 2]] => #<Post id: 2, title: "Second post!", body: "This is my second post.", created_at: "2016-02-04 07:53:37", updated_at: "2016-02-04 07:53:37”> irb(main):026:0> post.destroy (0.1ms) begin transaction SQL (1.7ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 2]] (5.6ms) commit transaction => #<Post id: 2, title: "Second post!", body: "This is my second post.", created_at: "2016-02-04 07:53:37", updated_at: "2016-02-04 07:53:37">

Delete Records Method destroy_all deletes from a relation. Returns the records that were deleted. irb(main):027:0> Post.where(title: "Fourth post").destroy_all Post Load (1.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."title" = ? [["title", "Fourth post"]] (0.1ms) begin transaction SQL (0.3ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 5]] (5.6ms) commit transaction => [#<Post id: 5, title: "Fourth post", body: "My 4th post", created_at: "2016-02-04 08:52:05", updated_at: "2016-02-04 09:30:23">]

Query Calculations Use methods count, minimum, and maximum. irb(main):006:0> Post.count (0.2ms) SELECT COUNT(*) FROM "posts" => 4 irb(main):007:0> Post.minimum :id (0.2ms) SELECT MIN("posts"."id") FROM "posts" => 1 irb(main):008:0> Post.maximum :id (0.2ms) SELECT MAX("posts"."id") FROM "posts"

Database Schema Stores the current state of your database. db/schema.rb ActiveRecord::Schema.define(version: 20160122000915) do create_table "comments", force: :cascade do |t| t.string "author" t.text "body" t.integer "post_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "comments", ["post_id"], name: "index_comments_on_post_id" create_table "posts", force: :cascade do |t| t.string "title"