Tuesday, December 30, 2008

DEPOT

Damn, my Sony mobile phone can't work in SMSD mode. So I'll abandon this project until I buy a proper GSM Modem.

So now, I'm reading Pragmatic Agile Web Development with RoR. The book is written for Rails 1.2 and I'm using the latest Rails 2.2.2

I'm using InstantRails on Windows Vista
#in c:\ror\rails_app\depot

-----------------------------------------Depot ---------------------------
# to create depot project
ruby rails depot -d mysql

# to create the products model
ruby script/generate model product

# edit the migration file db/migration/xxxx

class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end

def self.down
drop_table :products
end
end


#save the file and run db:migrate
rake db:migrate

# we need admin function to control the product
# we need to create admin controller
ruby script/generate controller admin


...to be continued...seems my ror 2.2.2 is not compatibe with my current RoR Ebook

Monday, December 29, 2008

Building the Web Interface for SMSD

1.Dump this sql schema into sms_development database
/usr/share/doc/gnokii-smsd-mysql/sms.tables.mysql.sql

mysql -uroot -pabc123 sms_development < sms.tables.mysql.sql

2.I'm going to generate the interface for inbox table.But first lets identify the table structure

+------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| number | varchar(20) | NO | | | |
| smsdate | datetime | NO | | 0000-00-00 00:00:00 | |
| insertdate | timestamp | NO | | CURRENT_TIMESTAMP | |
| text | text | YES | | NULL | |
| phone | tinyint(4) | YES | | NULL | |
| processed | tinyint(4) | NO | | 0 | |
+------------+------------------+------+-----+---------------------+----------------+


3.We are going to create the scaffold for number,text and smsdate
script/generate scaffold inbox number:string text:string smsdate:timestamp

4. Now repeat that for outbox table
script/generate scaffold outbox number:string text:string smsdate:timestamp

5. Since we are using non standard RoR table name, we need to change both models for inbox and outbox.

edit app/models/inbox.rb

class Inbox < ActiveRecord::Base
# gnokii legacy table inbox
set_table_name "inbox"
set_primary_key "id"
end


edit app/models/outbox.rb

class Outbox < ActiveRecord::Base
# gnokii legacy table outbox
set_table_name "outbox"
set_primary_key "id"
end


6. Now point your browser to http://localhost:3000/inboxes and http://localhost:3000/outboxes

7. Job's done

Setup SMS Phone

I'm using Sony K770i and Sony VGN-CR11GH laptop and running Ubuntu 8.04 Hardy Heron on this project.
We are going to use the mobile phone as SMS sending/receiving device and use MySQL as data storage and use Ruby On Rails to manage the data.

1.Install the needed softwares
sudo apt-get install gnokii
sudo apt-get install gnokii-smsd
sudo apt-get install gnokii-smsd-mysql

2.Connect the phone
Connect the phone via USB and type dmesg, In my case, my phone connected either in /dev/ttyACM0 or /dev/ttyACM1.

3. Edit /etc/gnokiirc to change the phone device and model to AT
# Set port to the physical port used to connect to your phone.
# Linux version is:
port = /dev/ttyACM0

# Set model to the model number of your phone. For the
# Symbian phones use:
# model = symbian
# For other non-Nokia phones and when you want to use AT
# mode use:
model = AT

Save the file

4. Test the phone
gnokii --identify

bazet@bazet:~$ gnokii --identify
GNOKII Version 0.6.22
Couldn't read /home/bazet/.gnokiirc config file.
Couldn't read /home/bazet/.gnokiirc config file.
IMEI : 351600030678710
Manufacturer : Sony Ericsson
Model : AAD-3022082-BV
Product name : AAD-3022082-BV
Revision : R8BC004 071211 1434


5. Try to send SMS
echo "test " | gnokii --sendsms 0146412911


6. If you can execute step 1 to 5 without any problems, you're on track.

Pagination with Search




Quite hard, I've spent 2 days figuring this out ( since I'me new to RoR World )

Here is what to do.

1. Install Mislav Will Paginate Plug in ( http://github.com/mislav/will_paginate/wikis/home )
gem install mislav-will_paginate

2. Edit config/environment.rb and add this sentence to very bottom of the page
require 'will_paginate'

3. restart webbrick
script/server

4. Change the index method to include paginate
edit app/controllers/employees_controller.rb

# GET /employees
# GET /employees.xml
def index
if params[:term]
@employees = Employee.paginate(:all,
:conditions => ['first_name LIKE ? OR
last_name LIKE ? OR
email LIKE ? OR
mobile_number LIKE ? OR
description LIKE ? ', "%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%"],
:page => params[:page], :per_page => 2 )
else

@employees = Employee.paginate(:page => params[:page], :per_page => 2 )
end

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employees }
end
end



5. Edit the view and add this
<%= will_paginate @employees %>

6.Enjoy

Updated RoR to 2.2.2 on my Ubuntu


I have to restart the PC, and issue these commands

sudo gem install rails

and..updated

Searh On Multiple Fields

Since I can't get Ferret working on my ubuntu, here's my solution to search on multiple fields

Well CakePHP find is way much cooler from RoR


def index
if params[:term]
@employees = Employee.find(:all,
:conditions => ['first_name LIKE ? OR
last_name LIKE ? OR
email LIKE ? OR
mobile_number LIKE ? OR
description LIKE ? ', "%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%",
"%#{params[:term]}%"])
else
@employees = Employee.find(:all)
end

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employees }
end
end

Department hasMany Employees

Below is my employee migration class

class CreateEmployees < ActiveRecord::Migration
def self.up
create_table :employees do |t|
t.references :department
t.string :first_name
t.string :last_name
t.string :mobile_number
t.string :email
t.text :description

t.timestamps
end
end

def self.down
drop_table :employees
end
end

notice the "t.references :department" which is actually a reference to department model