Monday, October 31, 2011

Rails application deployment using shell script

I have used following shell script to deploy one of my Ruby on Rails applications:
DATES=`date +%Y%m%d%H%I%S`
service apache2 stop &&
mysqldump -u[user] -p[pass] [yourdbname] > "your backup directory"[yourdbname]_${DATES}.sql
git pull &&
bundle install --path [your bundle path] &&
bundle exec rake db:migrate RAILS_ENV=production &&
mv [your application's production log file] [your application's production log backup path]/production_${DATES}.log &&
touch [your application's production log path]/production.log &&
chown [user]:[group] [your application's production log file] &&
service apache2 start
In the above code snippet,
  • Line 01: Used to get the current date time
  • Line 02: Stopped the Apache server
  • Line 03: Used to take database backup in the backup directory
  • Line 04: Getting update source from repository
  • Line 05: Bundle install to update gem list
  • Line 06: Run database migration to migrate latest script if any
  • Line 07: Rotating log in every deployment
  • Line 08: Creating new production log file
  • Line 09: Providing proper user access to write log
  • Line 10: And, then start the server again

That's all. Your application should run without any issue if you configure third bracket's([]) information perfectly.

But, now a days - Capistrano allows you to deploy a Rails applications easily. You can try that and can customize if you need to.

Monday, October 17, 2011

Using helper method from action mailer views

Action Mailer of Ruby on Rails(RoR) allows you to send emails from application using a mailer model and views. Sometimes you may need to use any helper method though it does not allow by default. In that case you have to include helper(module) in the Mailer class.

Say, you may need to use method(s) from ProductsHelper module, then you need to write following line in the Mailer file which will provide accessibility to use all methods written in ProductsHelper