Monday, August 31, 2009

Open previously closed tabs in Firefox


@cheeaun Hmm.. how do I undo close window on Firefox?

@chuyeow @cheeaun Look in your History menu or hit Cmd-Shift-N.

@sengming @cheeaun on windows and linux its control-shift T. Just
one finger away from add new tab.

Using Mutt to manage your Gmail


sudo apt-get install openssl mutt

# ~/.muttrc

set imap_user = "username@gmail.com"
set imap_pass = "password"

set smtp_url = "smtp://username@smtp.gmail.com:587/"
set smtp_pass = "password"
set from = "username@gmail.com"
set realname = "Your Real Name"

set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"

set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates

set move = no

# Keymaps
j = next message
k = prev message
/ = search emails
c = change mail folder

Thursday, August 27, 2009

Create & serve a Sinatra app on your iPhone

Taking tips from this article on "Building a iPhone web app in under 50 lines with Sinatra and iUI", I've toyed around with building and serving one right from my iPhone.

ssh root@IPHONE_IP_ADDRESS

gem install sinatra -v 0.9.1.1 --no-ri --no-rdoc
# Latest gem needs fcgi gem which cannot be compiled properly

BUILD YOUR sinatra_app.rb

ruby -rubygems sinatra_app.rb


Question: So far I've got the V & C in MVC. What's the best way to build the M using Sinatra on iPhone? ActiveRecord?

Tuesday, August 18, 2009

Search and Replace in VIM

Syntax

:s/search/replace/
:[range]s/search/replace/

Examples

# Search & replace in current line
:s/entry/interview

# Search & replace in lines 8 to 10
:8,10 s/search/replace/

# Global search & replace
:%s/entry/interview/g

# Global search & replace with prompt
:%s/entry/interview/gc

More awesome search and replace tips at http://linux.com/learn/tutorials/8255-vim-tips-the-basics-of-search-and-replace

Breaking Cucumber feature tests into separate smaller files

Previous I had the following Cucumber feature tests in a single file

# features/webrat/edit_enrollment_table

@edit_enrollment_details
Feature: Editing of enrollment details from enrollment table
In order to edit enrollment details easily
As an admin
I want to edit enrollment details from enrollment table

Scenario: Admin should be able to enter entry test date
....
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...
Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

Pretty massive. Why not split them up into manageable bits?

# features/webrat/edit_entry_test_details

@edit_enrollment_details
@edit_entry_test_details
Feature: Editing of entry test details from enrollment table
In order to edit entry test details easily
As an admin
I want to edit entry test details from enrollment table

Scenario: Admin should be able to enter entry test date
...
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...

# features/webrat/edit_interview_test_details

@edit_enrollment_details
@edit_interview_test_details
Feature: Editing of interview test details from enrollment table
In order to edit interview test details easily
As an admin
I want to edit interview test details from enrollment table

Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

So here's what I think the advantages are:

- Cleaner code in a single file. Well, at least not so chunky!
- Running small number of tests is much FASTER,
- Can still run full quite with proper tagging. ie. @edit_enrollment_details

Calendar Date Select gem for Rails


sudo gem install "calendar_date_select"

# config/environment.rb
config.gem "calendar_date_select"

# app/views/layouts/application.erb
<%= calendar_date_select_includes 'silver' %>

# app/views/assessments/_form.html.erb
<%= calendar_date_select_tag 'assessment[schedule]', Time.now, :time => true, :id => 'assessment_schedule' %>

Project homepage: http://code.google.com/p/calendardateselect/
Demos: http://electronicholas.com/calendar

Sunday, August 16, 2009

Reload current file in VIM


# To reload current file in Vim
:edit or :e

# To reload and discard modifications
:edit! or :e!

# To autoload every time file gets modified
:set autoread

Bluetooth tethering your iPhone to Ubuntu (works on multiple machines too)

1. Check out your iPhone bluetooth address

hcitool scan

2. Edit "/etc/default/bluetooth"

PAND_ENABLED=1
PAND_OPTIONS="--listen --role=PANU --devup /etc/bluetooth/pan/dev-up --devdown /etc/bluetooth/pan/dev-down"

3. Create directory "/etc/bluetooth/pan"

4. Create file "/etc/bluetooth/pan/dev-up"

#!/bin/bash
ifup bnep0

5. Create file "/etc/bluetooth/pan/dev-down"

#!/bin/bash
ifdown bnep0

6. Install pand & create PAN connection

sudo apt-get install bluez-compat
sudo pand -c IPHONE BLUETOOTH ADDRESS

7. Assign IP address to iPhone

sudo dhclient3 bnep0

8. To disconnect tethering

sudo pand -K

You can use the same method to tethering to multiple machines at the same time.

Thursday, August 13, 2009

Common regular expresssions








Taken from http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/

Cucumber > 0.3.7 freezes hash table and changes loading of shoulda


diff --git a/config/environments/test.rb b/config/environments/test.rb

-config.gem "thoughtbot-shoulda", :lib => "shoulda/rails", :source => "http://gems.github.com"
+config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"


diff --git a/features/step_definitions/enrollment_steps.rb b/features/step_definitions/enrollment_steps.rb

- enrollments.hashes.each do |hash|
+ enrollments.hashes.each do |h|
+ hash = h.dup # Hash is frozen by cucumber. Not sure why.

Wednesday, August 12, 2009

Tech user groups, websites, events and calendars in Singapore

Dev Communities

Singapore Ruby Brigade
http://groups.google.com/group/singapore-rb
http://singaporerubybrigade.pbworks.com

Malaysia Ruby Brigade
http://groups.google.com/group/malaysia-rb

Singapore Perl Mongers
http://groups.google.com/group/singapore-pm

Singapore Google Tech User Group
http://groups.google.com/group/sgtug
http://sg-gtug.org/

Java Meetup Singapore
http://groups.google.com/group/java_meetup_singapore
http://www.meetup.com/JUG-Singapore

Singapore Linux User Group
http://www.meetup.com/sg-linux/

Linux NUS
http://linuxnus.org/

Singapore Flex User Group
http://fug.sg/

Ph.P Society (PHP User Group)
http://www.php.org.sg/

Cocoaheads Singapore
http://www.facebook.com/group.php?gid=29368309964

Code Android (Android User Group)
http://www.codeandroid.org/

SgDotNet (.Net User Group)
http://www.sgdotnet.org/Pages/Default.aspx

Python User Group Singapore
http://www.facebook.com/group.php?gid=20088734289

WebSG (Web Standards User Group)
http://websg.org/

Hackerspace SG
http://www.facebook.com/group.php?gid=121463986679

Dev Related Communities

e27
http://www.e27.sg/

SG Entrepreneurs
http://sgentrepreneurs.com/

Mengcubator
http://groups.google.com/group/mengcubator/

Websites

SIGINT (Hacker News)
http://sigint.sg/

Events

Geekcamp SG
http://geekcamp.pbworks.com

Calendars

Patrick Haller's google calendar (it's got most of the events here)
http://www.google.com/calendar/embed?src=95mcn1nlh3lv9lkn8rcgj8ujf8%40group.calendar.google.com&ctz=Asia/Singapore

Shortcut key maps for Vim in normal mode

Just created for myself a bunch of shortcut keys to access commands that I commonly use in VIM.

## Entries in my ~/.vimrc

:nmap tt :tabnew
:nmap tn :tabnext
:nmap tp :tabprevious
:nmap tf :tabfirst
:nmap tl :tablast
:nmap t1 :tabnext 1
:nmap t2 :tabnext 2
:nmap t3 :tabnext 3
:nmap t4 :tabnext 4
:nmap t5 :tabnext 5
:nmap t6 :tabnext 6
:nmap qq :q!
:nmap ww :w
:nmap zz :wq
:nmap ff
:nmap gg
:nmap ;w :set wrap
:nmap ;nw :set wrap!
:nmap ;;as :e ~/projects/aims
:nmap ;;jt :e ~/projects/jtab
:nmap ;;rs :e ~/projects/rsvp-api

Tuesday, August 11, 2009

Smarter visual highlight in VIM

I know one can expand a visual highlight using

"vw" or "ve"

But it doesn't work if you wanna highlight between underscored words.

Eg.
this_is_made_up_of_underscored_words

So to expand highlight use the following

'this_is_made'
"v/_" then cycle thru with "n"

Tuesday, August 4, 2009

Implementing "google search" like search field using Searchlogic

I just wanna a single search field like how it is in google.com. This search field should then search through all the desired columns and return records with matches.

# View
<% form_for @search do |f| %>
<%= f.text_field :keyword %>
<%= f.submit 'Search' %>
<% end %>

# Controller
# Using Seachlogic with Will Paginate
class EnrollmentsController < ApplicationController

def index
@search = Enrollment.search(params[:search])
@enrollments, @enrollments_count = @search.paginate(:include => [{:addresses => :address_type}, :course, :gender], :page => params[:page], :per_page => 20), @search.count
end

end

# Model
# params[:search][:keyword] will trigger keyword namedscope
class Enrollment < ActiveRecord::Base

named_scope :keyword, lambda {|keyword|
{ :conditions => [
"first_name like :word or last_name like :word or nric like :word or date_of_birth like :word or mentor like :word or school_approval_status like :word or student_confirmation_status like :word or genders.name like :word or addresses.handphone like :word or courses.name like :word",
{:word => "%#{keyword}%"}
],
:include => [:addresses, :gender, :course]
}
}

end

Avoid multi-level associations using named scope

I had the a "local_address" method in Enrollment which looks for first local address

class Enrollment < ActiveRecord::Base

has_many :addresses

def local_address
addresses.local.last
end

end

class Address < ActiveRecord::Base

belongs_to :enrollment
belongs_to :address_type

named_scope :local, {
:joins => "LEFT OUTER JOIN address_types ON address_types.id = addresses.address_type_id",
:conditions => "address_types.name = 'Local'"
}

end


Calling the "@enrollment.local_address" then leads to very unoptimized queries. To optimized it I had to call "Address.find" directly from the "local_address" method.

def local_address
- addresses.local.last
+ Address.last :include => :address_type, :conditions => ["address_types.name = 'Local' and enrollment_id = ?", id]
end

Table sort with Searchlogic

Searchlogic comes with a nice rails helper method to assist sorting of your paginated tables

<%= order @search, :by => :created_at, :as => "Created" %>

You don't even have to do anything extra to your controller to get some nice sorting action. Cool!

Usefulness of named scope in remote console

Fancy searching through records in production database in a SSH terminal? You can always query them via good o' SQL but I find that rather cumbersome.

select * from payment_transactions outer left join utility_providers ..... where .....

Too long... what about activerecord objects using script/console?

PaymentTransaction.find(:all, :conditions => [........])

Better but the conditions string can get unweldy. But why not use namedscopes?

PaymentTransaction.vendor('Singtel').amount('30.00').account('123456').date_less_than('2009-08-01')

Short and sweet! Plus if you use the Searchlogic gem, these namedscopes comes for free! :)

Using Searchlogic and Will Paginate

Here's the code to in controller to integrate the searching with binarylogic-searchlogic gem and pagination using mislav-will_paginate gem.

def index
@search = Enrollment.search(params[:search])
@enrollments, @enrollments_count = @search.paginate(:page => params[:page], :per_page => 20), @search.count
end

Check size of folders in terminal

To list the folders and it's size:

du -hs path/to/doc

Sunday, August 2, 2009

Toggling between gnome terminal tabs

Can't believe I haven't used this trick earlier since I use gnome-terminal with multiple tabs EVERYDAY.

Usually I have about 4~5 tabs open for a given gnome-terminal window session. What I did to to go to a particular tab was to hit "CTRL + PAGE UP/PAGE DOWN" to scroll through them. What a silly way because all I need to do was to hit "ALT + tab no." to get to it directly.

Eg.

First tab: ALT + 1
Second tab: ALT + 2
...
Fourth tab: ALT + 4

Blog Archive