Archive for the 'Ruby on Rails' Category

Posted on February 10, 2006 at 6:18 pm

I didn’t have to wait long for assurance_Why is putting (pudding?) on one of his “performances” at RubyConf 2006. Via Loudthinking.

Posted on February 1, 2006 at 11:27 pm

Railsconf 2006 was announced today. I’m signing up and booking everything for the trip tomorrow.

Update: I couldn’t bear the thought of not being one of the first 100 to sign up. I’m booked. Now… I want to be assured that _why is going to put on one of his audio/visual extravaganza’s.

Posted on January 5, 2006 at 6:40 pm

So, you’re developing Ruby on Rails apps on your shiny Mac and you want to get Growl notifications when your rake tasks are complete. Below, I will illustrate a fairly simple way to get Growl notifications when the test_units and test_functional tasks are complete. This method can be easily extended to any of the other tasks as well.

Prerequisites

  • Growl is installed and running.
  • The RubyCocoa framework is installed.
  • You also need a copy of the Ruby binding from the Growl source distribution. Go to the Growl Developer Downloads page and download the latest tarball or bzball. Extract the files and grab a copy of Growl.rb from the Bindings/ruby directory.

Integration with Rails

First, copy the Growl.rb file that you extracted from the Growl source into your Rails application’s lib directory. Name it growl.rb (all lowercase).

Next, create a new file called growl_notifications.rake in your Rails application’s lib/tasks directory. Copy and paste the following code into this file:

task :test_units do
  task_notifier 'Rake Task', 'Unit testing has completed.'
end

task :test_functional do |t|
  task_notifier 'Rake Task', 'Functional testing has completed.'
end

def task_notifier(title, message)
  begin
    require 'growl'
    n = GrowlNotifier.new('Rake',['RakeTask'],nil)
    n.register()
    n.notify('RakeTask', title, message)
  rescue
  end
end

Growl’s Ruby bindings allow you, among other things, to customize the icon that is displayed in the notification. Take a peek inside the growl.rb file for details.

I would like to extend this further to show the results from the tests in the notification message body. The results are part of a TestResult object, but I’m not sure if there is a way to get at this object by the time I create the notification. If you how I can do this, please let me know!

Posted on December 6, 2005 at 6:20 pm

Yesterday, DHH posted a link to his Pursuit of Beauty slides from the Snakes and Rubies event this weekend.

I went through each of the slides looking for new stuff and found several great new things. If you look at slide 14, you’ll see a new :through parameter on one of the associations. There’s no documentation on this yet, but I did a little experimentation by checking out the latest edge_rails.

To see how this new type of association works, let’s look at the traditional way to handle many-to-many relationships when we want to store additional attributes about the join.

Lets take a simple example to illustrate how we can use the new functionality. Let’s say that we publish several newsletters and we let users sign up for as many of these newsletters as they want. We also need to to track several things about each subscription, such as the email format the user would like to receive that newsletter in. What we want to do in this case is make the join table a model. Let’s call it Subscription. We would have three tables: users, subscriptions, newsletters. The models would be set up like this:

class User < ActiveRecord::Base
  has_many :subscriptions
end

class Newsletter < ActiveRecord::Base
  has_many :subscriptions
end

class Subscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :newsletter
end

Let’s say that we want to see a list of what newsletters John Doe has subscribed to:

@newsletters = []
User.find_by_name("John Doe").subscriptions.each do |s|
  @newsletters < < s.newsletter
end

This works great, but it isn’t very elegant. It would be much nicer if we could just get all the newsletters without having to walk through the subscriptions.

Let’s add the :through associations to the models:

class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :newsletters, :through => :subscriptions
end

class Newsletter < ActiveRecord::Base
  has_many :subscriptions
  has_many :users, :through => :subscriptions
end

class Subscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :newsletter
end

Now we can just access the associated models directly:

@newsletters = User.find_by_name("John Doe").newsletters

There truly is beauty in simplicity.

Update
I’ve updated this article with some better examples based on user feedback.

Posted on November 28, 2005 at 1:57 pm

A couple of weeks ago, I hosted a 2.5 hour interactive presentation on Ruby and Ruby on Rails. The purpose of the presentation was to show the various ways in which Rails can improve our ability to build complex applications with small agile teams rather than large hierarchical teams.

There are 30 slides in the presentation, which I interspersed with live demonstrations in which we drilled down into some of the technologies and methodologies. The first section of slides is a quick intro to Ruby, which I borrowed heavily/outright copied from the Get to the Point! slides by Ryan Platt and John W. Long.

The slides are available in two flavors: