Setting Up Ubuntu 9.10 for Ruby On Rails Development

February 1st, 2010 by

This is a document I put together at the beginning of 2010 while building yet another Ubuntu VM, digging through our internal documentation to try and find out what I needed. We’ve got the answers, and generally Ruby, Rails, and Ubuntu are pretty good about telling you how to install tools if you don’t have them yet.

But the answers are spread out and distributed randomly. Plus, I can only see “The program ‘______’ is currently not installed. You can install it by typing: sudo apt-get install ______” so many times before I lose interest and put off the task.

Read the rest of this entry »

Including external .rake files in your project’s Rakefile — keep your rake tasks organized!

May 26th, 2009 by

Perhaps you have a non-Rails project (let’s say it’s in ruby and maybe some other languages, too) and you use Rake tasks to automate some of the dirty work. So you’ve got a bunch of methods that you wish to keep neatly sorted into .rake files in some dir’s (probably a sub folder of lib like lib/tasks) and a single Rakefile in your project’s root directory. Including those external .rake files in your project’s Rakefile via require statements won’t work :

require 'lib/some_rake_file' # or require 'lib/some_rake_file.rake'

=> rake aborted!
no such file to load — /home/your_project/lib/some_rake_file

Rake assumes the ‘require’d files end in .rb, so it won’t find your .rake files. You need to import rake files:

import 'lib/some_rake_file'

This is fine for individual files, but I wanted to include all files that end in .rake in my tasks dir:

Dir.glob('tasks/*.rake').each { |r| import r }

…and there you go. While dead simple, this exemplifies an important distinction between require and import that I found to be poorly documented. Keep your rake tasks organized and remember that Rake isn’t just for rails apps!

Find the Unique Sessions for a Rails Application

May 4th, 2009 by

Today we’re going to look at how to find the number of unique sessions over a specific time frame for a rails application. We’ll be using the slow-actions gem.

Read the rest of this entry »

Reintroducing sanitize_email | Work with Production Email without Fear

April 25th, 2009 by

An issue I have to tackle on almost every project I encounter is developing, QA’ing, and troubleshooting email generated from my app. Rails provides a couple of options to help during the development and test phases (setting action_mailer.delivery_method = :test), but there aren’t any great mechanisms for performing QA with a client or troubleshooting production email issues (formatting, content, etc).

Not long ago I stumbled upon Peter Boling’s plugin implementation of sanitize_email. I’ve since forked it, turned it into a gem, and continued to apply maintenance commits to it as rails has marched forward through versions 2.2 and 2.3.

The primary value sanitize_email provides is that it allows you to reroute all email without having to alter any application code. It’s environment-specific and doesn’t introduce any production/deployment dependencies.

Read the rest of this entry »

environmentalist 0.2.3 released — supports rails 2.3.2

April 4th, 2009 by

Just a heads up that I’ve released environmentalist 0.2.3. You can update with:


  sudo gem install environmentalist

The only substantial change in this release is that we moved the loading of postboot.rb to the bottom of the boot.rb file as opposed to the top of config/environment.rb. This was necessary because common db rake tasks like db:create and db:drop stopped loading the environment in 2.3.2.

As a recap, environmentalist provides an executable that converts a rails app’s config structure. The basic idea is that environments themselves are now first-class citizens, allowing you to create several environments (e.g. staging, prodtest, demo, etc.) in a clean, organized fashion. Each environment is given its own folder where it can store its own set of configuration files (think mongrel configs, apache configs, etc.) without polluting the top-level config/ directory.

Rails 2.3 Nested Object Forms: I’m not Crazy about Them

February 24th, 2009 by

I’m a couple of weeks late, but I just finished reviewing Rails 2.3 Nested Object Forms. While a very nice and “magical” feature, I’ve got to admit that I’m really not that crazy about how it works.

Let me be the first to admit that there’s no one right way to do things. In fact, I’m writing this post particularly because I have a few objections to how this functionality is ultimately exposed, and I’d like to hear arguments from those who disagree. In other words, let me acknowledge the possibility that I am the misguided one.

Read the rest of this entry »

AASM + interning empty string error

February 13th, 2009 by

Second time I’ve run into this in the past week, so I thought I’d share it with you guys. I fumbled for a short while with the extremely unhelpful error message “interning empty string error” after a re-deployment to my staging server. It seemed to be affecting all of my pages, and I couldn’t really load any page.

The Problem

This blog post is valid for rails 2.1.2 and rubyist-aasm 2.0.5. It may apply to other version combinations, but I make no guarantees.

First, let’s dive into the error itself. This error message occurs when you call to_sym on an empty string:

john-mbp:trunk john$ irb
>> "".to_sym
ArgumentError: interning empty string
	from (irb):1:in `to_sym'
	from (irb):1

Now that we know what’s up, let’s dig into my stack trace.

/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active_record_persistence.rb:233:in `to_sym’
/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active_record_persistence.rb:233:in `aasm_read_state’
/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/persistence/active_record_persistence.rb:135:in `aasm_current_state’
/opt/ruby-enterprise-1.8.6-20090113/lib/ruby/gems/1.8/gems/rubyist-aasm-2.0.5/lib/aasm.rb:50:in `disabled?’
/var/vhosts/discovered/releases/20090213213806/app/helpers/base_admin_helper.rb:4:in `disable_link’
/var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:16:in `_run_erb_47app47views47schools47index46rhtml’
/var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:12:in `each’
/var/vhosts/discovered/releases/20090213213806/app/views/schools/index.rhtml:12:in `_run_erb_47app47views47schools47index46rhtml’


The cause of this bug is the fact that my newest build added an aasm_state column to the School model, but my migration did not properly default schools to a particular state. As such, when an instance of the model was asked for its state, the AASM codebase raised an error. And this occurred on every page because I was rendering a link to a school in a common layout.

The Solution

After the fact, in SQL:

UPDATE schools SET aasm_state = 'active';

Or better yet, before the fact, in my migration:

def self.up
  add_column :schools, :aasm_state, :string
  School.reset_column_information
  School.update_all("aasm_state", "active")
end

The Lesson

Don’t forget that there’s a difference between a migration for a yet-to-be-deployed application and a live running application. Live apps require you to take into account that data already exists.

Properly Setting HTTP_REFERER in a Rails Integration Test for a File Upload

February 3rd, 2009 by

This one had me frustrated for the past hour. I’ve been writing integration tests for a Rails project and got stuck on an error when I was trying to test that a file upload worked successfully and asserted a redirection was occuring correctly, but ran into the following error:

Expected response to be a <:redirect>, but was <500>
<"No HTTP_REFERER was set in the request to this action, so
redirect_to :back could not be called successfully. If this
is a test, make sure to specify
request.env[\"HTTP_REFERER\"].">

What a lovely error message to send me on a goose chase trying to set HTTP_REFERER directly on the @request as instructed.

Did Not Work

@request.env["HTTP_REFERER"] = '/'
post upload_path, { :file => fixture_file_upload("worddoc.docx", "application/msword") },
  { :html => {:multipart => true} }
assert_redirected_to '/', 'index'

This continued to spit out the same error. I finally stumbled across a post back from 2006 that held the answer. The HTTP_REFERER is not set the same way in an integration test:

Success!

post upload_path, { :file => fixture_file_upload("worddoc.docx", "application/msword") },
  { :html => {:multipart => true}, :referer => '/' }
assert_redirected_to '/', 'index'

Hope that saves anyone else some time if you encounter this error.

Timecop: Freeze Time in Ruby for Better Testing

November 19th, 2008 by
The API mentioned in this blog post is specific to v0.1.0. The latest version is v0.2.0. See the blog post announcement, the documentation, or the github home page.

This past weekend I released v0.1.0 of the Timecop gem. Timecop makes it dead simple to travel through or freeze time for the sake of creating a predictable and ultimately testable scenario.

The gem is derived from a plugin I wrote a while back to achieve more or less the same functionality for an extremely time-sensitive application. My goals for the gem included:

  1. Drop-in-ability: The primary goal is to allow your app to continue to use Time.now, Date.today and DateTime.now as normal within your application. No overloading of functions with optional arguments (a la today=Date.today) just so you can write test cases.
  2. Environment independence: I wanted the gem to work (a) w/ rails (ActiveSupport actually), (b) w/ plain ruby when the ‘date’ library has been loaded, and (c) w/ plain ruby when the ‘date’ library had not been loaded.
  3. Library independence: I could have utilized mocha to achieve the mocking functionality found under the hood, but because I wanted this to work with plain vanilla ruby, libraries like mocha are out.
  4. Short-term time travel: I wanted to expose the ability to temporarily change the concept of “now.” This is particularly helpful when writing tests where time needs to pass.
  5. Long-range time travel: I wanted to expose the ability to change the concept of “now” for an indeterminate period of time. This is particularly helpful when setting up a rails test environment along with the test data.
  6. Nested time travel: I wanted to provide the ability to nest traveling, allowing the state to be kept within each block (we’ll see an example later).

The gem is hosted on RubyForge and can be installed by simply running:

sudo gem install timecop

Read the rest of this entry »

Using ActiveRecord’s to_xml to produce custom xml including deep level associations

September 10th, 2008 by

ActiveRecord provides a powerful method to all its records called to_xml. Most web developers using RoR should be familiar with its usage and hopefully use it for their simple xml production needs. But what do you do when you need to produce xml that is selective, includes associations, or contains custom tags? There are many ways to manipulate to_xml’s output and I’ll explain a few below.
Read the rest of this entry »


preferably ready-to-order coverage characteristics of auto insurance thing secure