Friday, October 30, 2009

Update rubygems using gem


sudo gem install rubygems-update; sudo update_rubygems

Wednesday, October 28, 2009

String value not displayed in input text field in a form

If you have the following,

<% form_tag '/index' do %>
<% text_field_tag 'date', '2009-01-01' %>
<% submit_tag 'Go' %>
<% end %>

You would think the textfield above would have "2009-01-01" string displayed. But it is not so. Need to specify "GET" method.

<% form_tag '/index', :method => :get do %>
...

Sunday, October 25, 2009

Format string for titles

How to change "ClassTest" to "Class Tests"?

"ClassTest".titlecase.pluralize

or

"ClassTest".titleize.pluralize

Specify form method when generating url route for edit form

Scratch head moment when I didn't specify ":method => :put" for edit form coz the form keeps leading me to "create" action.

<% form_for @class_test, :url => class_test_path(@class_test) do |f| -%>
<%= render :partial => f %>
<%= f.submit "Save" %>
<% end -%>

This was resolved after specifying the "put" method.

<% form_for @class_test, :url => class_test_path(@class_test), :html => {:method => :put} do |f| -%>
<%= render :partial => f %>
<%= f.submit "Save" %>
<% end -%>

Monday, October 19, 2009

Error using validation hooks with before_save callback


# This will throw "Schedule can't be blank" error as any changes made in before_save callback somehow gets lost
class Assessment < ActiveRecord
validates_presence_of :schedule

attr_accessor :date, :time
before_save :set_schedule_from_date_and_time

private

def set_schedule_from_date_and_time
if self.date && self.time
self.schedule = Time.parse("#{date} #{time}")
end
end

# The trick is to use before_validation
before_validation :set_schedule_from_date_and_time

Sunday, October 18, 2009

Deleting a word backwards in Bash and Vim


# To delete one word back
CTRL + w

# To paste deleted word
CTRL + y

Swap CAPS LOCK with CONTROL key

I've been using swapping CAPS LOCK with ESC key configuration. Trying out swapping CAPS LOCK with CONTROL key now.

Advantage:
- not having my pinkie wrangled to reach CONTROL Key at the bottom --> easier to do CTRL-C, CTRL-V, CTRL-A

Disadvantage:
- a big rewiring of finger memory as I've been using normal CONTROL key for years
- I need another keystroke for ESC sequence (CAPS LOCK + [) as compared to previous configuration (CAPS LOCK)

Reloading ALSA

I've encountered some errors while running Ardour and Hydrogen. The fix is simple --> restart/reload ALSA.

sudo /sbin/alsa force-reload

Thursday, October 15, 2009

Uncompressing 7zip (.7z) files in Ubuntu


# Install p7zip package
sudo apt-get install p7zip-full

# uncompress a file named production.7z
7z x production.7z

Wednesday, October 14, 2009

Select commits from another branch using git cherry-pick

Scenario:
Assuming the following commits in my 123-feature-blah_blah branch

commit eff75535f3eaa42dbc41cdaa3e8e404298fcce58
Author: NgTzeYang
Date: Tue Oct 13 12:58:09 2009 +0800

[#1467448] Fix unit test failure for course management

commit 19651c61957f2b5cab9614948fe68c4fd505da04
Author: Jason Ong
Date: Thu Oct 15 14:36:08 2009 +0800

Upgraded cucumber gem to version 0.4.2

commit 534585c488628e0c434399d700ae0e8637afb868
Author: NgTzeYang
Date: Tue Oct 13 12:11:20 2009 +0800

[#1467375] Use different database for unit & integration testing

Problem:
I wanna push 2nd commit to origin/master so others can benefit from this commit while I work on the blah blah feature

Solution:
- create another branch from local master
- find SHA id of commit to cherry pick
- use git cherry-pick to select 2nd commit into the branch
- rebase into local master
- push to origin/master

git checkout master && git checkout -b upgrade_cucumber_gem
git log 123-feature-blah_blah
git cherry-pick 19651c61957f2b5cab9614948fe68c4fd505da04
git checkout master && git rebase upgrade_cucumber_gem
git push origin master

Tuesday, October 13, 2009

Share simple internet web browsing via SSH X forwarding

Scenario:
You forgot to bring your access to the world wide web for your laptop. ie. didn't bring iPhone, 3G dongle, etc. But your friend wasn't careless and had brought his.

Solution:
You can get your friend to "share" his internet connection via his firefox. Better for him to create a firefox profile for you in this case
- Connect both yours and your friend's laptop to the same network. Either wifi or lan cable.
- ssh -X friend_login@friend_ip_address
- firefox -P your_profile -no-remote &

Sunday, October 11, 2009

Fix static noise problem in Skype on Ubuntu

I have this problem on my netbook where Skype another Skype user can hear only loud static noises when trying to reconnect to him/her. Not sure what the real problem is (most probably to do with Pulseaudio?) but the solution is simple.

1. Go to Gnome volume applet
2. Click on "Volume Control"
3. Select "HDA Intel (Alsa mixer)" as Device
4. Go to "Recording" tab
5. Make sure none of the capture slider is set to zero

That's all!

Record & playback a Vim macro


# q{register} when in normal mode will start recording into register
qs

# q will stop recording
q

# Playback = @{register}
@s

Thursday, October 8, 2009

Stash changes away in Git to work on urgent matters first

Ref: http://ariejan.net/2008/04/23/git-using-the-stash/


Suppose:
- you've made some changes in your current branch but realized you're in the wrong branch
- you've gone off track in making changes that doesn't relate to the branch story
- you need to drop whatever you're doing right now and apply some urgent patch

Git stash to the rescue!

# Stash changes
git stash save "message for later reference"

# List stashes
git stash list

# Apply stash
git stash apply stash@{STASH_ID}

# Drop/delete a stash
git stash drop stash@{STASH_ID}

# Drop/delete last stash
git stash pop

# Clear all stashes
git stash clear

# Quick & dirty stashing
git stash
...
git stash pop

Insert Pivotal Tracker story id into Git commit messages


# Story id of feature from Pivotal Tracker is in feature file
# Example: features/course_management/admin_deletes_course.feature
# http://www.pivotaltracker.com/story/show/1423733
@ok
Feature: Admin Deletes Course
In order to manage course as a resource
As the admin
I should be able to delete a course

# Using head and sed to insert story id into commit message
git commit -am "[#`head -n 1 features/course_management/admin_deletes_course.feature | sed 's/.*\///'`] Replacing button ids to confirm to convention."

Check git log for a particular branch


# To changelog for upstream origin/master changes
git fetch origin
git log origin/master

View changes made by upstream in Git


# Fetch the changes from origin
git fetch origin

# View difference between local master branch and origin's master branch
git diff master origin/master

Wednesday, October 7, 2009

Restart Gnome session from command line


sudo /etc/init.d/gdm restart

Tuesday, October 6, 2009

Be careful with button_to in RESTful routes


# This will be default use POST method which in this context be sent as a create resource
<% form_for @course do |f| %>
...
<% end %>
<%= button_to 'Cancel', courses_path %>

# Specify GET method explicitly so it will call index resource instead
<%= button_to 'Cancel', courses_path, :method => :get %>

Monday, October 5, 2009

Rename current branch in Git


# Accidentally created branch with name "1234"
git checkout -b 1234

# Renames current branch to "1234-feature-admin_looks_good"
git branch -m 1234-feature-admin_looks_good

Friday, October 2, 2009

Reload vimrc in Vim


# :so or :source
:so ~/.vimrc

Insert contents from clipboard into Vim


# This usually works in insert mode
SHIFT + INSERT

# However sometimes my "CTRL + c" on other application doesn't get contents into Vim's "clipboard".
# The solution is to use Vim's multiline insert mode

:i or :in or :insert

# This will bring up lines which you can paste from Ubuntu's clipboard by "CTRL + SHIFT + v" to you heart's content.
# Finish the multilines with a "." at the end
# NOTE: better to turn off Vim's "autoindent" with "!"

:in!

This is first line
This is 2nd line
This is last line
.

# A similar method would be append instead of insert

:a, :append

Yank all lines in Vim

3 methods.

# 1. Highlight select all lines before yanking
:0, SHIFT + v, G, y

# 2. State range to yank: G - the last line
:0,Gy

# 3. State range to yank: $ - end of text
:0,$y

Thursday, October 1, 2009

Git branching and merging workflow for agile team


# Ref Url: http://reinh.com/blog/2009/03/02/a-git-workflow-for-agile-teams.html
#
# NOTES:
# 1. We use the following convention throughout the rest of this doc:
# * ... id of story/chore/bug as provided by pivotal tracker
# * ... story, chore or bug
# * ... a very short description of the story, chore or bug
# * ... a description of the story, chore or bug
#

1. To start working on a feature:

$ cd
$ git checkout master
$ git fetch origin master
$ git rebase origin/master

a. Assuming i'm working on something new:

$ git checkout -b --

Examples:

$ git checkout -b 889900-feature-admin_reboots_system
$ git checkout -b 889901-chore-cleanup_config_environment
$ git checkout -b 889902-bug-cannot_shutdown_until_all_system_tasks_complete

b. Assuming i'm already onto something:

$ git checkout --
$ git rebase origin/master

NOTES:
* Remember to run test after rebase, nothing new should break

3. Continue to work on my feature & finish it & commit frequently

4. Squash commits with interactive rebase:

$ git rebase -i origin/master

Suggested final commited message after squashing would be:

> [#]

5. Checkout master & merge with branch:

$ git checkout master
$ git merge --
$ git push origin/master

Resize Gnu Screen window

Screen intelligently resize your Screen window to a resolution that suits all machines viewing the screen session. That might result in a window size that's less than optimum for yourself.

# Screen window readjusted to fit my paired programmer viewing my screen session



# To readjust size for my optimum
CTRL + a, F

Setup object first before testing validates_uniqueness_of in Shoulda


# This will complain
class SubjectTest < ActiveSupport::TestCase
should_validate_uniqueness_of :name
end

# Error
1) Failure:
test: Subject should require case sensitive unique value for name. (SubjectTest)
[/usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/assertions.rb:55:in `assert_accepts'
/usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/active_record/macros.rb:74:in `__bind_1254386647_780301'
/usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
/usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: Subject should require case sensitive unique value for name. ']:
Can't find first Subject

# Setup object first
class SubjectTest < ActiveSupport::TestCase
setup { Factory(:subject) }
should_validate_uniqueness_of :name
end

Blog Archive