4/07/2009

Workshop 3-part 2

Review previous work:

From workshop 3-part1, we have already created:
1.) A web application named taxiapp
2.) A model named passenger
3.) A table named passengers in taxiapp_test, taxiapp_production and taxiapp_development

Use of Scaffolding in online taxi booking system

I will try to use scaffolding in online taxi booking in this time.

1.) Install plugin named scaffold for online taxi booking system in the taxiapp directory by issuing the command [ruby script\plugin install scaffolding ]

2.) Install plug-in named paginate in the taxiapp directory by issuing the command
[ruby script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination]

REMARK: if the web site is not available, you can download the classic_pagination package from web and place it in yourApplicationDir\vendor\plugins\., or send a package request mail to me at
samkwongfw@gmail.com

3. Install plugin named validates_date_time and date_time_text_field_helpers.

4.) Code generation of explicit controller and view for customizable interface by issuing the command in the taxiapp directory - "ruby script/generate scaffold passenger".

5.) Modify the controller file named passengers_controller.rb from using erb format to rhtml format, for example:
format.html # index.html.erb to format.html # index.rhtml
format.html # show.html.erb to format.html # show.rhtml
format.html # new.html.erb to format.html # new.rhtml

6.) Rename the view file from *.html.erb to *.rhtml.

7.) Configure Dreamweaver to support rhtml format in order to create a beautiful interface.




8.) Create a simple search engine for this web application

  • In the model, defines a fuction with following code:

def self.search(search)

if search find(:all, :conditions => ['name LIKE ?', "%#{search}%"]) else find(:all) end

end

  • In the controller, tell it to use that search function in index page

@passengers = Passenger.search(params[:search])

  • In the index page of view, call the search function in the defined interface with following code

text_field_tag :search, params[:search]

submit_tag "Search", :name => nil

9.) Create a Job ID auto-generator with following code

  • @getResultSetJobID = Passenger.find(:all).last.job_id
  • @newJobID = @getResultSetJobID+1 %>

10.) Define Input Validation in model

validates_date :date

validates_time :time_required

validates_presence_of :name,:contact_number,:suburb_origin,:street,:street_number,:suburb_destination,:message=>'Error Message: Field cannot be empty'


11.) Test the application by
  • 1. Create a new order - ok
  • a.) Auto create the job ID -ok
  • b.) Input Vaildation -k
  • 2. Show the new order - ok
  • 3. Edit the new order 0k
  • 4. Search the new order ok
  • 5. Destory the new order -ok

Please see the video for referance

Workshop 4-part 1

TO-DO

1. Spend some time moving your way through the 46 Ruby coding examples in the Ruby Tutorial with Code from
http://www.fincher.org/tips/Languages/Ruby/

Partially Done!

2. What are the syntax differences in the way that Ruby and Javascript use the if statement?

The syntax of Ruby's If statement which refers to tutorialspoint(2009) at http://www.tutorialspoint.com/ruby/ruby_if_else.htm, that is..

=================
if conditional [then]

code...
[elsif conditional [then]
code...]...
[else code...]
end
=================


"If" expressions are used for conditional execution. The values false and nil are false, and everything else are true. Notice Ruby uses elsif, not else if nor elif.

Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.

An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon.

For example:

#!/usr/bin/ruby
samAge=20
joeAge=20
if samAge > joeAge
puts "Sam is older than Joe"
elsif samAge < joeAge
puts "Sam is younger than Joe"
else
puts "Sam and Joe are in same age"
end

The syntax of Javascript's If statement which refers to tutorialspoint(2009) at
http://www.tutorialspoint.com/javascript/javascript_ifelse.htm, that is

=============================================
if (expression 1){
Statement(s) to be executed if expression 1 is true
}else if (expression 2){
Statement(s) to be executed if expression 2 is true
}else{ Statement(s) to be executed if no expression is true}
==============================================

It is just a series of if statements, where each if is part of the else clause of the previous statement. Statement(s) are executed based on the true condition, if non of the condition is true then else block is executed.

For example:


From the above "if" statement examples for Ruby and JavaScript, there are some different between them.

Ø An if expression's conditional, Rudy is separated from code by the reserved word then, a newline, or a semicolon, but Javascript needs to use bracket( ) for condition(s) and and use a symbol { } for action(s)
Ø Ruby does not need to declare the data type of variable, but Javascipt needs it.
Ø Ruby uses “elsif”, but Javascript uses “else if”, for handling the other condition between if -else statement.
Ø Ruby uses "puts" and the string needs inside double quote, but Javascript uses document.write(" string here ") for printing out the message.
Ø Ruby need not use semicolon at the end of each statement, but Javascript must require.

3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?

Darell(2009)(http://tore.darell.no/pages/javascript_eye_for_the_ruby_guy) stated there are some similarities between Ruby and Javascript which are listed in the following:

Ø Both languages are highly dynamic, allowing you to change objects and methods at runtime.
Ø Both languages are very object-oriented, but with different approaches. Ruby is a class-based, object-oriented language with functional aspects while JavaScript is a functional, object-oriented language with some class-like functionality.
Ø Variables in JavaScript, like Ruby, hold data. In the case of basic types, they hold the value of the type itself, but in the case of objects it holds a reference to the object itself.
Ø JavaScript, like Ruby, is heavily object-oriented, and objects are used everywhere.
Ø Functions in JavaScript, like blocks in Ruby, provide closure. That is, the variables available in the scope where the function is defined are always available in the function, even if it’s called from another scope.JavaScript is prototype-based, but it has the concept of classes. Kind of. There is a new keyword which is very familiar to those having used class-based languages. In Ruby this is not a keyword, but a class method, but the concept is the same: It instantiates a new object from that class.

Workshop 3-part 1

To DO

1. Set up the MySQL tools on your computer as described in section 6 above.
a.) Download MYSQL GUI ToolsI. Download mysql-gui-tools-5.0-r17-win32.msi from http://dev.mysql.com/downloads/gui-tools/5.0.htmlII.

b.) Install MYSQL GUI Tools by following the prompted instruction of MYSQL.

2. Rails will setup a new application directory for each of your Web application projects. Get InstantRails (Windows) or Locomotive (MacOS) running on your machine. Both packages install Ruby, Rails, a Web server or one called ‘Mongrel’ or another small Ruby Web server called ‘WEBrick’, and MySQL “inside a bubble” as I call it so that others parts of your system are not modified (Similarly ZOPE does with installing its own Web server and Python versions).

a.) Download InstantRails-2.0-win.zip from http://rubyforge.org/projects/instantrails/
b.) Extract all zip files from InstantRails-2.0-win.zip to c:\
c.) Check that my instantrails 2.0 covers




  • ruby verison is 1.86 patch level 111
  • rails verison is 2.0.2
  • mysql version is 5.0.27
  • mongrel version is 1.1.5
  • PHP 4.3.10
d.) Create a new application by issuing the command -"rails -d mysql C:\rails_apps\taxiapp"

3. Once Rails is running you at http://localhost:3000, you need to configure database access. Connection to the database is specified in the config/database.yml file.

a.) Start the web application by issuing the command -"C:\rails_apps\taxiapp>mongrel_rails start -e development" and test by accessing the web page at http://localhost:3000/ by internet browser.

b.) First, you should create three database manually for test, development and production environments. Second, make sure the root's password does correctly input in config/database.yml file and make sure the info of database is mapping with current existed database in mysql.

4. Generate the Passenger model by creating the MySQL database and ‘passengers’ table from the information above.

a.) Create a model named passenger in the taxiapp directory by issuing the command "ruby script\generate model passenger".

b.) Create three new schema in mysql, they are taxiapp_production, taxiapp_development and taxiapp_test by MYSQl Query Browser.

c.) Edit the database.yml file which is located at C:\rails_apps\taxiapp\app\config by adding the root's password in development, test and production database configuration field. Please the below configuration:

development:
adapter: mysql encoding: utf8 database: taxiapp_development username: root password: 123456 host: localhost

test:
adapter: mysql encoding: utf8 database: taxiapp_test username: root password: 123456 host: localhost

production:
adapter: mysql encoding: utf8 database: taxiapp_production username: root password: 123456 host: localhost

d.) Edit C:\rails_apps\taxiapp\db\migrate\001_create_passengers.rb file with following statement

def self.up
create_table :passengers do t
t.column :name, :string
t.column :job_id, :integer
t.column :contact_number, :string
t.column :suburb_origin, :string
t.column :street, :string
t.column :street_number, :string
t.column :building, :string
t.column :suburb_destination, :string
t.column :passenger_number, :string
t.column :taxi_type, :string
t.column :date, :date
t.column :time_required, :time
end

e.) Create the table in taxiapp_production database by issuing the command "set RAILS_ENV=production" and then "rake db:migrate"

f.) Create the table in taxiapp_test database by issuing the command "set RAILS_ENV=test" and then "rake db:migrate"

g.) Create the table in taxiapp_development database by issuing the command "set RAILS_ENV=development" and then "rake db:migrate"

h.) Use MySQL query browser to check the table named passengers whether exits in taxiapp_test, taxiapp_production and taxiapp_development database.

From point d to h is telling you how to use the MVC “push-based architecture” in the Ruby on Rails development environment



5. Further work on understanding MySQL under Rails by David Mertz:

a. See “Fast-track your Web apps with Ruby on Rails” at http://www-128.ibm.com/developerworks/linux/library/l-rubyrails/

In this web site, it tells me how to use "scaffolding" to work with mysql. It is exactly same as my work in workshop 1.

b. The “Rolling with Ruby on Rails” series and “Cookbook recipes by Curt Hibbs and others beginning at http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html

It is a good tutorial web site for teaching me how to install and work with Rails.

Workshop 2-part 2

Challenge Problems:

1. How is Rails structured to follow the MVC pattern?
I draw the diagram to explain how is Rails stuctured to follow the MVC patten in my taxi booking system, please refer the below diagram, by the way, you can enlarge the diagram by double-click it.
























Diagram 1. MVC on ROR

2. Apply the MVC design approach to our Project: Online Taxi Booking System.

HINT: Begin with a single model, single view and single controller classes. This will give you a head start to the next workshop: Online Taxi Booking System: SQL and Database design.

Step 1. Create web appliaction project named app by issuing the command "rails -d mysql C:\rails_apps\app".

Step 2. Create a controller named taxi in the app by issuing the command "ruby script\generate controller taxi".

Step 3. Create a model named taxi in the app by issuing the command "ruby script\generate model taxi".

Step 4. Create a rhtml file name index.rhtml in C:\rails_apps\app\app\taxi\list.rhtml.

Step 5. Create three databases in mysql, they are app_production, app_development and app_test.

Step 6. Add the root account password of development, test and production database configuration field in the database.yml file which is located at C:\rails_apps\app\app\config\database.yml, please refer the below diagram

























Diagram 2. Database.yml configuration


Step 7. Create a migration file named taxis in the app by issuing the command "ruby script/generate migration taxis".

This will create the file db\migrate\001_table_name.rb. A migration file contains basic Ruby syntax that describes the data structure of a database table.

Step 8. Edit C:\rails_apps\app\db\migrate\001_create_taxis.rb and add with following code into CreateTaxis class

def self.up
create_table :taxis do t
t.column :suburb, :string
t.column :numpass, :string
t.column :taxitype, :string
t.column :time, :datetime
end
end

def self.down
drop_table :taxis
end

Step 9. Create the table in app_production database by issuing the command

"set RAILS_ENV=production" and "rake db:migrate".

Step 10. Modify the model file of taxi which is located at C:\rails_apps\app\app\models\taxi.rb with following code:

validates_presence_of :suburb,:message=>'Error Message, suburb cannot be null!'
validates_presence_of :numpass,:message=>'Error Message, numpass cannot be null!'
validates_presence_of :taxitype,:message=>'Error Message, taxitype cannot be null!'
validates_presence_of :time,:message=>'Error Message, time cannot be null!'

Step 11. Modify the controller file of taxi which is located at C:\rails_apps\app\app\controllers\taxi_controllers.rb with following code:

def list
@taxis = Taxi.find(:all)
@current_time = Time.now
end

Step 12. Modify the view file named list.rhtml which is located at C:\rails_apps\app\app\views\taxi\list.rhtml. Please refer to the below diagram.
























Diagram 3. List.rhtml

Step 13. Insert some dummy data into app_production.taxis like this :
mysql> insert into taxis (suburb,numpass,taxitype,time) values ("Hong Kong", "2", "standard", 20090130);

Step 14. Modify the route.rb which is located at C:\rails_apps\app\config\routes.rb with the following code:
map.connect 'see/', :controller => 'taxi', :action => 'list'

Step 15. Start the web application named app by issuing "mongrel_rails start -e production" in app directortory

Step 16. Open the internet explorer and access the following url


please refer to the following diagram:

Diagram 4. Result of accessing the http://localhost:3000/see

4/06/2009

Workshop 2 -Part1

To Do:

1. Set up a focus group (like a study group for peer learning) to work on the Ruby on Rails workshops via Interact tools as a class.

Join the focus group at http://railsfocusgroup.blogspot.com/


2. What is meant by “convention over configuration” and how does it reduce coding?

The below Information from Beginning Rails(2007) at http://books.google.com.hk/books?id=0rD_q96srHoC&pg=PA356&dq=%22convention+over+configuration%22+and+%22coding%22&lr=#PPA7,M1.

Convention over configuration means that the programmer needs to define only configuration that is unconventional. Programming is all about making decisions. If you were to write a system from scratch, without the aid of Rails, you would have a lot of decisions to make: how to organize your files, what naming conventions to adopt, and how to handle database access are only a few. If you decided to use a database abstraction layer, you would need to sit down and write it, or at least find an open source implementation that suits your needs. You would need to do all this before you even got down to the business of modeling your domain. Rails lets you get started right away, by encompassing a set of intelligent decisions about how your program should work, alleviating the amount of low-level decision making you need to do up front. As a result, you can focus on the problems you're tying to solve and get the job done quicker.

Rails ships with almost on configuration files. If you're used to other framework, this fact might surprise you. If you’ve never used a framework before, you should be surprised. In some causes, configuring a framework is nearly half the work. Instead of configuration, Rails relies on common structures and naming conventions, all of which employ the often-cited principle of least surprise (POLS). Things behave in a predictable, easy-to-decipher way. There are intelligent defaults for nearly every aspect of the framework, relieving you, the developer, from having to explicitly tell the framework how to behave. This isn’t to say that you can’t tell Rails how to behave. In fact, most behaviors can be customized to your liking and to suit your particular needs. While you can manipulate most things in the Rails setup and environment, the more you accept the defaults, the faster you can develop applications and predict how they will work. The speed with which you can develop without having to do any explicit configuration is one of the key reasons why Rails works so well.

3. Further work on understanding MVC:
a. See the wiki at

http://wiki.rubyonrails.org/rails/pages/UnderstandingMVC - Done!
b. Do the MVC tutorial at http://wiki.squeak.org/squeak/1767 - Done! (Try to access the web page at http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html, however, this website has been obsoluted.)

4. Got a spare hour or so? I recommend the UC Berkeley RAD lab’s Ruby on Rails Short course at
http://youtube.com/watch?v=LADHwoN2LMM - Done!


5. Read the Flash article using ActionScript by Colin Moock titled “The Model-View-Controller Design Pattern “at
http://www.adobe.com/devnet/flash/articles/mv_controller.html - Done!