Giant Robots smashing other giant robots…

March 31st, 2008 tkadom

Interesting blog title to say the least… but more interesting is the paperclip plugin.  If you have been looking for a simpler "attachment_fu", or even if you are nostalgic for the good old days of file_column but you just dont want to bring it up to date with rails 2, then you owe yourself a trip to "Giant Robots Smashing Other Giant Robots" to check out this story: "For attaching files, use paperclip"

paperclip offers a seamless and deceptively simple way  to upload files to your website, get a thumbnail for the image, and do it all without requiring rmagick!  (well you do need to have imagemagic binaries, but no need for the rails extensions!)

Tim

 

Posted in Random | No Comments »

hordazzle

March 28th, 2008 tkadom

I invented a new word. Hordazzle ! Please use it in your daily communication as much as possible, and if someone tells you it was invented by a short italian girl, point them to the entry in urban dictionary…

In other, more interesting news, a macbook air was recently hacked in under two minutes!

 

Posted in Random | 1 Comment »

Rails Kung-Fu

March 27th, 2008 tkadom

Whenever I want to learn a new language, or framework to improve my kung-fu, I like to look at something that has been written in it. You generally get an idea of how much you will like a language just by looking at the syntax up close. With rails, I started with 1.2, and there were lots of examples out there which all seemed to just work. With the release of 2.0.2 having happened just recently, there are lots of rails 1.2 moves that will leave you in a face plant on the floor searching for a new master…

So how do you assimilate the new kung-fu? Well for me, I learn best by example. I like to see code, and I learn the most from good code. I had heard that lots of folks fell victim to setting up a beast forum. In truth, it was not that difficult as there were plenty of signs left by other seekers which could guide your path. If you paid really close attention, then you saw that beast is no longer beast. Beast has become… altered_beast ( and there is hardly a traveler that has traveled this path! )

This altered_beast is a wonderful master for students on the rails 2 path. For one thing, Rick Olson works on rails core. If you were to look for a more experienced teacher, you would be hard pressed to find one. For another thing, altered_beast grew from a forum that was supposed to not exceed 500 lines of code. So there are some pretty sweet moves in the code. And finally, altered_beast is a piece of cake to install. Seriously. If you can’t get altered_beast going, you will need to downgrade to a weenier framework than rails. And I got news for you, I don’t think there is a weenier one!

Just looking at the models in altered_beast is like a having someone hold your hand through how you model ActiveRecord relationships. For a rank beginner, you immediately see that you probably could be using the :dependend attribute on your has_many relationships. Looking through the user model you quickly realize what belongs in a model, and what belongs in a controller. You *may* also start looking for "concerned_with", and come up empty in rails documentation. And thats actually the first bit of altered_beast kung-fu that stumped me a little. At the top of the user model, altered beast presents "concerned_with". After a little digging I found the following definition in the initializer…

  1.  
  2. concerned_with(*concerns)
  3.        concerns.each do |concern|
  4.            require_dependency "#{name.underscore)/#{concern}"
  5.        end
  6.     end  
  7. end  
  8.  

So in rails 2 we run through these initializers when the app starts. and all concerned_with does is give you a nifty shortcut for requiring dependencies. However, If you have tried registering the user observer in altered_beast, this kung-fu will have chopped you in the knees already. It turns out there is a little issue with the order in which items get initialized in the 2.0.2 rails core, and this specific trick will punch you in the gut pretty nicely. I have a suspicion that it is what led to: Ticket 10980. You see when the user observer loads, it tries to instantiate a user. Users are concerned_with stuff, but at initialization time, concerned_with has not yet been defined because the observers are triggered first. So no matter how smart you try to get with defining your observer in the new rails 2 way, you will be out of luck.

A simple fix would be to update the user model and replace concerned_with with the require_dependencies. Another fix would be to freeze rails into altered_beast and monkey_patch it (which I recommend, because it feels a bit like your cheating, and it gets you into more of a rails mindset. If something is broken, don’t go looking for someone to fix it. just fix it!) There is plenty more rails kung-fu in altered_beast, and I think anyone who is looking for an introduction to rails, and who has some programming moves to begin with, could do much worse than to pull down the source and begin to assimilate it. When you can snatch the pebble from the altered_beast, The rails 1.2 moves will seem like child’s play…

 

Posted in Rails, Random | 5 Comments »

Zen Parable…

March 27th, 2008 tkadom

I came across this a while ago, and for some reason it re-entered my mind today…

A man walked across a field and encountered a tiger. He fled, with the tiger chasing after him. Coming to a cliff, he caught hold of a wild vine and swung himself over the edge. The tiger sniffed at him from above. Terrified, the man looked down to where, far below, another tiger had come, and was waiting to eat him. Two mice, one white, one black, little by little began to gnaw away at the vine. The man saw a luscious strawberry near him. Grasping the vine with one hand, he plucked the strawberry with the other. How sweet it tasted…

Posted in Random | No Comments »

Capistrano and database.yml

March 25th, 2008 admin

Building your app in rails is just the first step. After that comes the grueling task of deploying it. I used to simply copy my project over to my web directory, start my mongrels and leave it at that. If I was thoughtful, I would create a backup of the previous installation, but since it was all tested locally, I would never need that right? :)
Naturally projects grow, and deployments become more complex. Copying a folder over on a single server may not be that bad, but once you start talking about multiple servers, folks start running for the exits. On top of that, this manual approach usually costs a good bit of downtime, which has to be scheduled, and I don’t know about you, but once i have typed the same sequence of commands a few times, I am looking to write a script!

Don’t bother with the scripts! Let Capistrano handle all that hordazzlement. We recently had a demonstration from the Mor.ph guys, and they were showing us how simple it would be to deploy a rails application to one of their ultra nifty free dev shards. The first thing stated was “Deployment is simple! just point your cap file to…”. You can imagine the rest. So the point of that is that capistrano is pretty much the defacto deployment vehicle in the rails community these days.

What is Capistrano

The guys who wrote rails also brought you capistrano, and like rails they were solving some very specific problems. Specifically , they needed a tool that would do the following:

  • Secure update of multiple database servers and websites from a centralized source repository
  • A rollback mechanism that would allow you to undo what you perpetrated if necessary
  • reduce downtime during migration
  • integrate with Rails
  • handle the restarting and configuration of production environment

If you are like me, you do not have your database.yml file checked into source. In fact you have subversion ignoring it entirely. The reason for this is that each developer tends to have their own database.yml, and you don’t want their check-ins to break anyone elses environment. In order to have capistrano deploy my application without a database.yml file to production, I would need capistrano to copy the production database.yml file over into the newly created directory. Furthermore, I did not want the production database connectivity to be in subversion at all, preferring instead to create an external configuration file on the production servers. It turned out that moving such a file with capistrano s a trivial thing to do .
Getting Started

First things first, get the capistrano gem on the machine you will use to initiate deployments.
sudo gem install capistrano

Capistrano does not need to run on the target machine, you only need to have it on your deployment client. This is because Capistrano uses SSH and SFTP to do all its work.

The next thing you need to do is develop a deployment file. cd into your rails application and type

skiptree.com$ capify .
[add] writing ‘./Capfile’
[add] writing ‘./config/deploy.rb’
[done] capified!

This will set you up with two essential files, one of which (deploy.rb) We will modify for our target environment. The default deploy.rb looks like this:

set :application, “set your application name here”
set :repository, “set your repository location here”

# If you aren’t deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
# set :deploy_to, “/var/www/#{application}”

# If you aren’t using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion

role :app, “your app-server here”
role :web, “your web-server here”
role :db, “your db-server here”, :primary => true

The basics are fairly straighforward. Simply provide your connectivity information.

set :application, “skiptree.com”
set :repository, “svn+ssh://my.secret.source/opt/trunk”

set :deploy_to, “/var/www/#{application}”

role :app, “skiptree.com”
role :web, “skiptree.com”
role :db, “skiptree.com”, :primary => true

# forces a checkout from source and copies to remote
set :deploy_via, :copy

task :after_update_code do
run “ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml”
end

The only thing new here is the task :after_update_code. In that task i simply create a command to run on the target server which executes after the code is updated. The {release_path} variable resolves to a “shared” subdirectory. i.e. /var/www/#{application}/shared. or “/var/www/skiptree.com/shared” on the target host. I simply placed my production database.yml into the config subdirectory and called it a day.
now all that i need to do to trigger your first deployment is type:

> cap deploy:cold
The cap commands are all available via “cap -T”, after you have done your initial cold deploy, you can simply tyle “cap deploy” for subsequent release. if you dont like what happened… “cap deploy:rollback”! Have fun!…

Posted in Rails | 2 Comments »