Nov 24, 2010

Rails's cookies

Here is some strange behaviour:

def index
cookies[:key] = "val"
puts cookies[:key]
redirect_to :second_index
end

def second_index
cookies[:key] = "newval"
puts cookies[:key]
end

## Output :
## Open index page for the first time
# nil
# val

## Open index page the second time
# newval
# val



cookies[] is to retrieve the current cookies
but cookies[]= is to assign outgoing cookies, not to re-assign the current cookies.
Actually, you can never re-assign the current cookies.

-----------------------------------------

Here is another different behaviour of using sign and string as indices:

## cookies and request.cookies are different
cookies.class #=> ActionController::CookieJar
request.cookies #=> Hash

## how to access value from cookies and request.cookies
# First set some value in cookies
cookies[:key] = "value"

cookies["key"] #=> "value"
cookies[:key] #=> "value"
# both the output are of type String

request.cookies["key"] #=> "value"
request.cookies["key"].class #=> CGI::Cookie

request.cookies[:key].empty? #=> true
request.cookies[:key].class #=> Array

Ref: http://www.quarkruby.com/2007/10/21/sessions-and-cookies-in-ruby-on-rails#scinrails

First use of Git to obtain whowish project

Assuming you have installed msysgit, you need to follow these steps in order to obtain whowish project.

1. First of all, you need to generate SSH Key for your computer by:
Run Git GUI > Help > Show SSH Key > then generate SSH key.

No need to set passphrase, just press 'enter'.

After you see the SSH key in the textarea, copy it and send it to me by any mean.

2. After I approve your key, you can test it by:

ssh git@github.com

If you see congratulation message, then you're fine.

3. Now you can check out by:

Right-click at a destination folder, select Git Bash,

Then:

git clone git@github.com:tanin47/whowish.git

* Don't forget to set username and email by:

git config --global user.name "your name"
git config --global user.email email@email.com

And there you go, the project will be downloaded to your computer.

Nov 23, 2010

How to access Github

Install msysgit.

How to use Git, really

1. When one wants to start working, he/she NEEDS to fork a new branch by using

git checkout -b branch_name

2. After making some changes, you can:

git commit -a -m "message here"

3. You can push your updates to the server, but if your friend worked on the same branch before, you might want to pull first:

git pull

Then:

git push

4. After sometimes, you might want to merge your branch with the main branch:

git checkout master (switch to the main branch)
git pull (get new updates)
git pull branch_name (merge with your branch)

Then after merging, you NEED to fork again in order to make another change.

Nov 22, 2010

The specified module could not be found ... mysql_api.so

But mysql_api.so does exist.

You just have to make sure that MySQL's bin path is in PATH environment variable.

The specified module could not be found ... mysql_api.so

But mysql_api.so does exist.

You just have to make sure that MySQL's bin path is in PATH environment variable.

Aptana RadRails Setting up for whoWish

  1. Create 3 databases (schema) in MySQL: whowish_development, whowish_test, whowish_production
  2. Get whoWish project folder and import it to Aptana RadRails
  3. Modify config/database.yml so that the username and password are valid for accessing the database
  4. Right-Click on the project and choose Rake > db > schema > load (All the tables will be created)
  5. Right-Click on the project and choose Run As > Run Configurations ...
  6. Right-Click on Ruby Application and Choose 'New'
  7. In the name textbox, type 'Run'
  8. In the Project textbox, browse and choose 'whoWish'
  9. In the File textbox, browse and choose 'script/server'
  10. Click on Apply and Click on Run
  11. Open browser and go to http://localhost:3000
You also need to install:
  • recaptcha plugin, by running 'gem install ruby-recaptcha'.
  • mysql plugin, by running 'gem install mysql'

If you don't have Rake, please run 'gem install --remote rake' in command line.

If you do not have Gem, then download Gem here (we use 1.3.5). and type 'ruby setup.rb' in command line.

If you do not have Ruby, then please install Ruby 1.8.6.

And we use Rails version 2.3.4.

Nov 10, 2010

DbUnit tutorial

Setting Up Lift Environment

Lift enviroment

Lift is a very new web framework and, inevitably, unstable. Therefore, to make Lift work, the versions of Scala, Apache Maven, and Lift itself “must” be compatible.

For the website whowish.com; Lift 2.1, Scala 2.8.0, and Apache Maven 2.2.1 are chosen.

Installation
The installation steps are:

1. Install Netbeans 6.9
2. Install Scala 2.8.0 final (Please read README)
3. Install Scala Plugin for Netbeans 6.9 (Please read README.png)
4. Install Apache Maven 2.2.1 (Please read README)
5. Create a project that uses Lift 2.1 with Scala 2.8.0 by creating manual archetype with these parameters:

* archetype_group_id = net.liftweb
* archetype_artifact_id = lift-archetype-basic_2.8.0
* archetype_version = 2.1
* archetype_repository = http://scala-tools.org/repo-releases
* remote_repositories = http://scala-tools.org/repo-releases
* group_id = your group id
* artifact_id = your artifact id (which is project identity)
* version = your project version

6. Add DbUnit and MySQL dependencies by add these lines to pom.xml under dependencies tag:

* add dbunit 2.2
* add mysql-connector-java 5.1.13

7. That’s it... Congratulation !

Configure it to run with jetty

1. Right-click on Project and select Propeties
2. Go to Actions
3. Select Run Project and, under Execute Goals, type jetty:run
4. Select Debug Project and, under Execute Goals, type jetty:run

5. Go to pom.xml, find <plugin> of Jetty, and change scanIntervalSeconds to 0.

(Right now continuous recompilation on Jetty does not work ...)

Testing

Right-click on the project and choose either Run or Debug

Unit Testing with database mock-up

We are using DbUnit to set the database (MySQL) to a predefined state (coded in XML) and, after running a test code, DbUnit can compare the present state of the database with an expected state (coded in XML)