Monday, November 21, 2011

How to comment source code in TextMate

Apple-/
Press the apple key and the forward slash, toggle comment

Alt-Apple-/
Comment a block. Also commet subset of a line

"head" tab-key
Type "head" and press tab. Insert a comment header at top of the file

Control-Shift-B
Insert a comment banner on the top of declarations, functions, etc.

"todo" tab-key
Type "todo" and press tab. Insert a todo bloack

Must enable Source and TODO in the Bundle filter list.

Friday, November 4, 2011

Cron + Whenever + Rake

Sample Rake code

namespace :test do
desc "Pick a random participant from database"
task :pick_participant => :environment do
if Rails.env.production?
# MySQL is used as the production DB, its random function is RAND()
p = Participant.find(:first, :order => 'RAND()')
else
# sqlite3 is used as the development DB, its random function is random()
p = Participant.find(:first, :order => 'random()')
end
puts "#{Time.now}"
puts "* environment is: #{Rails.env}"
puts "* picked participant: #{p.first_name}"
end
end


To test the rake task from command line

$ bundle exec rake test:pick_participant

2011-11-04 10:49:08 -0700
* environment is: development
* picked participant: Macy



Add the rake task to Whenever gem's schedule.rb

every 1.minutes do
rake "test:pick_participant"
end


Whenever defaults the environment to "production", to specify a different environment to run:


every 1.minutes do
rake "test:pick_participant RAILS_ENV=development"
#rake "test:pick_participant"
end


Call Whenever to update the crontab

$ whenever --update-crontab app_0515
[write] crontab file updated

$ crontab -l

# Begin Whenever generated tasks for: app_0515
* * * * * /bin/bash -l -c 'cd /Users/[username]/Development/rails_projects/app_0515 && RAILS_ENV=production bundle exec rake test:pick_participant --silent'

# End Whenever generated tasks for: app_0515