Favourite Photos
electronicholas.com

Photos, Thoughts and Software from a worldly Entrepreneur

Rails Generator NamedBase Auto Attributes

02 May 2008 - Ruby on Rails
If you've headed out to create your own generator and become a little confused by how to use the Rails::Generator::NamedBase then your not the only one. I created a simple generator to demonstrate the available attributes and actions.
test_attributes/test_attributes_generator.rb: class TestAttributesGenerator < Rails::Generator::NamedBase def manifest record do |m| view_path = File.join('app/views', 'test_attributes') m.directory view_path path = File.join(view_path, "test.html") m.template 'views/test.html.erb', path #, :assigns => {:actions => actions} end end end
test_attributes/templates/views/test.html.erb: <h1>Attributes</h1> <h2>class_name => <%= class_name %></h2> <h2>class_nesting => <%= class_nesting %></h2> <h2>class_nesting_depth => <%= class_nesting_depth %></h2> <h2>class_path => <%= class_path %></h2> <h2>file_path => <%= file_path %></h2> <h2>name => <%= name %></h2> <h2>plural_name => <%= plural_name %></h2> <h2>singular_name => <%= singular_name %></h2> <h2>table_name => <%= table_name %></h2> <h1>Actions:</h1> <% actions.each do |action| %> <h2><%= action %></h2> <% end %>

Running: 'script/generate test_attributes jim/bob/cathy mike eric'

gives:

Attributes

class_name => Jim::Bob::Cathy
class_nesting => Jim::Bob
class_nesting_depth => 2
class_path => jimbob
file_path => jim/bob/cathy
name => jim/bob/cathy
plural_name => cathies
singular_name => cathy
table_name => jim/bob_cathies

Actions:

mike
eric
Click here to leave a comment.

Calendar Date Select Plugin

26 Feb 2008 - Ruby on Rails
I have made available a fully functioning Demo of Calendar Date Select, the Javascript based visual date selector plugin for rails:

Calendar Date Select Rails Plugin

Source: http://code.google.com/p/calendardateselect/
Click here to leave a comment.

Installing Rmagick on Mac OS X

09 Nov 2007 - Ruby on Rails

It took a long time, but finally I managed to get the ruby rmagick library installed on Mac OS X 10.5 (this should work on 10.4 too). Thanks to Solomon for this. To install, simply copy the code below into a new document, and save as a shell script (install.sh). Finally make the script executable and run with sudo permissions:

chmod 755 /path/to/install.sh sudo sh /path/to/install.sh

#!/bin/sh curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz tar xzvf freetype-2.3.5.tar.gz cd freetype-2.3.5 ./configure --prefix=/usr/local make sudo make install cd .. curl -O http://superb-west.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.22.tar.bz2 tar jxvf libpng-1.2.22.tar.bz2 cd libpng-1.2.22 ./configure --prefix=/usr/local make sudo make install cd .. curl -O ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz tar xzvf jpegsrc.v6b.tar.gz cd jpeg-6b ln -s `which glibtool` ./libtool export MACOSX_DEPLOYMENT_TARGET=10.5 ./configure --enable-shared --prefix=/usr/local make sudo make install cd .. curl -O ftp://ftp.remotesensing.org/libtiff/tiff-3.8.2.tar.gz tar xzvf tiff-3.8.2.tar.gz cd tiff-3.8.2 ./configure --prefix=/usr/local make sudo make install cd .. curl -O http://jaist.dl.sourceforge.net/sourceforge/wvware/libwmf-0.2.8.4.tar.gz tar xzvf libwmf-0.2.8.4.tar.gz cd libwmf-0.2.8.4 make clean ./configure make sudo make install cd .. curl -O http://www.littlecms.com/lcms-1.17.tar.gz tar xzvf lcms-1.17.tar.gz cd lcms-1.17 make clean ./configure make sudo make install cd .. curl -O ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/gs860/ghostscript-8.60.tar.gz tar zxvf ghostscript-8.60.tar.gz cd ghostscript-8.60/ ./configure --prefix=/usr/local make sudo make install cd .. curl -O ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/current/ghostscript-fonts-std-8.11.tar.gz tar zxvf ghostscript-fonts-std-8.11.tar.gz sudo mv fonts /usr/local/share/ghostscript curl -O http://imagemagick.site2nd.org/imagemagick/ImageMagick-6.3.5-9.tar.gz tar xzvf ImageMagick-6.3.5-9.tar.gz cd ImageMagick-6.3.5 export CPPFLAGS=-I/usr/local/include export LDFLAGS=-L/usr/local/lib ./configure --prefix=/usr/local --disable-static --with-modules --without-perl | --without-magick-plus-plus --with-quantum-depth=8 | --with-gs-font-dir=/usr/local/share/ghostscript/fonts make sudo make install cd .. sudo gem install RMagick
Click here to leave a comment.

Rails setup on Site5 shared hosting

18 Jul 2007 - Ruby on Rails

I have taken the plunge and got my hands dirty with rails in a shared hosting environment recently. I chose Site5 because they had a good deal on at the time, I think no more than $140 for 2 years. They are slower than my previous host, 4uhosting, perhaps because I am in the UK, but more likely because Site5 put both CPU and burst bandwidth caps on shared accounts, as most big hosting setups do. Anyway, in this article, I will explain how I got my ssh - svn - rails - capistrano setup going, with the help of a few tips from elsewhere in the ether.

Your Machine

First of all, this post on the Site5 forums was a help, if a little out of date in places. I aim to fill in some of the gaps and share my experiences in following the same rough plan. Ok let's get started. If you haven't already Step 0 is to get Ruby and Rails up and running on your own development machine. This is not something I will go into, as it is well documented - I'm on a Mac and this walkthrough, "Building Ruby, Rails, LightTPD, and MySQL on Tiger;quot;, was straightforward. However you do it, make sure you have mysql, ruby, rails, activerecord etc installed and understand how they all work together before you continue. If you creating a new rails project 'rails projectname' MAKE SURE YOU USE THE SAME VERSION AS YOUR HOST (Site5 currently has Rails 1.1.6 installed)! It's not fun troubleshooting your code trying to work with two different versions of rails.

Next, you will want SVN installed on your machine. If you don't know why, or think don't think it's worth the effort, then head to Wikipedia - Subversion or do some reading around so you understand the basics. Subversion helps you to manage your workcycle and takes a lot of pain out of version control. The best getting started guide for SVN I've found is at Circle Six. Get SVN working, and if you are new to it, try creating a test repository, checking out a version and committing changes.

SSH Public / Private Keys

It is very handy to have a public/private key pair set on your shared hosting account/local account respectively so you don't have to keep typing your password when you perform actions with svn and later capistrano. It also saves time when you need to SSH into your hosting account. You will need SSH access enabled first though, and with Site5 you can do that yourself by going to 'Advanced Tools' in Site Admin page, and clicking 'Get Shell Access'. Mine was enabled by default, but it was not a bash shell, which we need. After sending a ticket requesting bash, I was told simply to disable and re-enable it to enable bash. Excellent. If your host doesn't have a control to do this, request that ssh and bash be enabled for you.

Now we can create an ssh public/private key pair to enable passwordless ssh entry to your hosting account (the private key is associated with your specific login on your own machine, so unless you are worried about who has access to your own user account on your computer then you can simply see this as a simple and secure timesaver). If you are running 'unix you will have ssh installed by default. Open a new local command line and run these commands to create a .ssh folder and the key pair (here we are using rsa encryption, you can use dsa if you wish). Note mkdir will fail if there is already a directory by that name present, in which case just skip that command:

cd ~ mkdir .ssh cd .ssh ssh-keygen -t rsa
Now name the key 'id_rsa'. You don't need a passphrase so just hit return twice. Next we make sure the permissions are correct on the private key, and upload the public key to your hosting account using the 'scp' command - the trailing colon ':' is important.
cd .. chmod 600 .ssh/id_rsa scp .ssh/id_rsa.pub username@server.address:
Next ssh normally into your account (it will ask for your password), create a .ssh directory if needed, and append the public key value to authorized_keys. Finally, delete the now redundant .pub file, and set the correct permissions for the .ssh directory:
ssh username@server.address cd ~ mkdir .ssh cat id_rsa.pub >> .ssh/authorized_keys rm id_rsa.pub chmod 700 .ssh
Finally, you should be able to log out and log back into your remote server without typing a password - Oh the joys!
logout ssh username@server.address $ "Last Login...etc..." but NO password prompt!

Finally create a PRIVATE key on your remote server too. This is to allow Capistrano to use SVN without you having to type your password, in what would be, annoyingly, clear text. Preventing svn asking for a password during Capistrano operations when using svn+ssh:// took me a long time to figure out. If we were using http:// or file:/// we could use svn's password caching to fix the problem, but since we are using ssh and we need to allow capistrano running on the server to authenticate using ssh, and its own public/private key pair. Creating them is much the same as with your local machine, but without the need to 'scp' it up. I actually just copied my local private key to the .ssh directory but this is not advisable so instead, ssh into your server and:

cd ~/.ssh ssh-keygen -t rsa <id_rsa> chmod 600 id_rsa cat id_rsa.pub >> authorized_keys rm id_rsa.pub

SVN

Now let's set up SVN on the remote server. First, if you haven't already, create your rails application template somewhere on the server, or upload it if you have been working locally already. SSH into your server if you haven't already and create the main repository. This will be the repository for your web application. As suggested on the Site5 forum, I just created mine in ~/repos:

ssh username@server.address cd ~ svnadmin create repos
Import your rails app into your repository. The file path should be from the root of your shared hosting server. For Site5 (and the default Linux setup) it is /home/username/. The 'trunk' at the end of the repository path is important for later setup, and as good SVN practice.
svn import /home/username/path_to_rails_app file:///home/username/repos/rails_app_name/trunk -m "Initial Import"
You should now see the console happily list all the files being added to the repository. The structure you just imported is now stored in the database, ready for checkout. Personally, since you probably have imported a fresh template, I see no need to keep the original files, do I deleted them, but you may wish to move them to a backup folder. Those files will NOT be used again by SVN now they have been imported. We will use a working copy - so check one out. I stored mine in ~/rails.
svn checkout file:///home/username/repos/rails_app_name/trunk ~/rails/rails_app_name
Excellent. Now you should be able to check out a working copy for your local machine. Open a new prompt in your machine and:
cd ~ svn checkout svn+ssh://username@server.address/home/username/repos/rails_app_name/trunk path_for_local_rails_app
Note we are using the svnserve path here, NOT an http path. This uses ssh for svn authentication and your password is not cached. We will return to svn+ssh in the Capistrano setup.

Now, this last step is optional and described in the Circle Six article. Obviously you will be making changes to your local copy and committing them to write the changes to remote repository. Incidentally you should try this to make sure it is working. Change some files on your local copy and from your local machine:

cd /path_for_local_rails_app/ svn commit -m 'testing local changes'
You should see 'Sending file whatever_you_changed' then 'Committed revision X'. But these changes are NOT yet echoed to the working copy you checked out on the remote server. The latest version is only updated in the repository. You can automate the updating of the working copy on your SERVER by adding a simple hook to the commit action in the repository. Use an ftp client (or a command line editor if you are familiar with one) and on the server head to ~/repos/hooks. Remove the '.tmpl' extension from the 'post-commit.tmpl' file and add the following to the bottom (comment out everything else unless you want it):
svn up /home/username/path_to_rails_app
This will automatically 'version up' your remote working copy as soon as the changes you have made locally have been committed to the repository. Great!

Capistrano

Capistrano is an automation tool, which facilitates speedy deployment by automating, among other things:

  • Database Migrations
  • Application and Database Server distribution
  • Server restarts
  • SVN updates and rollbacks
Even if you don't yet see the need for Capistrano, you should become familiar with it if you intend to build more complex or higher load applications in the future. First apply the Capistrano magic to your rails app (creates 2 files to get you started with a few rails tasks and recipies). You can do this locally then 'SVN' the new files and the changes you make to them to your remote server later.
cd /path_to_local_rails_app/ cap --apply-to ./
The next part is often glossed over a bit, but if you are new to Capistrano, it can be a little tricky, especially if your setup is not the default. Capistrano needs some variables to be set properly before it will work. Open your newly created rails_app/config/deploy.rb file and set the variables to look something like this:
set :application, "rails_app_name" set :repository, "svn+ssh://username@server.address/home/username/repos/#{application}/trunk" role :web, "electronicholas.com" role :app, "electronicholas.com" role :db, "electronicholas.com", :primary => true #assuming your remote application is in ~/rails/ set :deploy_to, "/home/username/rails/#{application}" set :user, "username" #remember the ssh keys we created earlier - enter the full path to each from the machine roots ssh_options[:keys] = %w(/Users/local_user/.ssh/id_rsa /home/username/.ssh/id_rsa) ssh_options[:host_key] = 'ssh-rsa' ssh_options[:paranoid] = false ssh_options[:username] = 'username'
And towards the bottom of the deploy.rb file, there is a 'restart' task definition. This won't do much on Site5, but I've found this works great:
desc "Restart the web server by killing your fgci instance, forcing it restart upon the next request." task :restart, :roles => :app do run "pkill -9 -u electron -f dispatch.fcgi" end
Excellent. Now you are ready to set up capistrano and use it as you wish, free from SVN password worries and with SSH security. First, though, make sure you commit these changes you've made to this local copy of your app to the remote version. To set up the standard deployment file structure you can now, from your local machine, run the following commands. Be sure you understand what they do beforehand though.
cd path_to_local_rails_app cap setup
And to deploy:
cap deploy

One last thing...

Make sure you amend your rails application's root .htaccess file to tell the server to use FastGCI. I was tearing my hair out for weeks with the slowness of it all, until I realised I had not configured it correctly (I thought I had). It's just a case of adding an 'f' to the end of dispatch.cgi on top of what's already there:

RewriteRule ^$ index.html [QSA] RewriteRule ^([^.]+)$ $1.html [QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Enjoy :-)

Matthew @ 16:48 on 12 Oct 2007
Is there any chance you could work out how to deploy to site5 with webistrano? it should be strait forward but i cant seem to get it to work for me!!
Click here to leave a comment.