Running Rails 3.2 on a shared Dreamhost with Passenger

Update: While it’s good to understand what you’re doing, this template file will get you so that after you get your app running, cap deploy:setup and cap deploy:cold works, assuming you have the host set up properly and have your app’s main directory cleared of all files. Everybody Loves Dreamhost, but… Dreamhost is a great little hosting company, but it really lags when it comes to keeping the latest software up to date. It’s version of Ruby is 1.8.7 and the latest stable version of Rails they have running is 3.0.3. That release is technically in “security fix” mode. Ugh. ...

July 20, 2012 · 3 min · Mark Simoneau

ImageMagick, JPEGs, and Orientation, Oh My

I’ve been working on a new project and decided to use CarrierWave to handle image uploads (and minor automatic manipulation). Everything looked great, but then my new boss, who has a penchant for finding the one or two things wrong with your latest well-tested feature uploaded a JPEG that looked fine in Preview, but automatically rotated on upload. After checking through the directory, it seemed that only the processed images were getting rotated. Actually, ‘rotated’ is a misnomer, they were actually just losing their orientation. JPEGs have EXIF meta data that can contain the orientation of a picture, no matter how it’s stored. This allows cameras to store everything as a 640x480, but display some in the reverse (480x640). The only thing different when the camera writes the image is what orientation the gyroscope adds to the meta data. ...

June 13, 2012 · 2 min · Mark Simoneau

DCI Generators in Rails

Recently, in my new project, I decided to take the DCI approach that Mike Pack outlined, which has been really cool. I’ve been able to keep my tests fast, and have very distinct buckets to put data (models), specific roles of that data (rather than cluttering up the models), and an easy way to take a use case and map it out programatically (contexts). I noticed that I was generating roles and contexts regularly and copying from previously written code examples so I decided to make role and context generators. The process wasn’t bad, but it did take a couple of steps that took a little digging to understand. ...

May 22, 2012 · 3 min · Mark Simoneau

CanCan and Devise with nil current_user on POST

I had a Rails 2.3.X app that I migrated to Rails 3. When I did, I upgraded CanCan and Devise. Suddenly, my delete links weren’t working. I remembered that unobtrusive JS was the default so I hopped into the application.html.erb template and added the relevant javascript tags. ... <head> ... <%= javascript_include_tag 'prototype', 'rails', 'application' %> ... </head> ... Great! Now my delete links weren’t just links to the show action, but when I did click them I noticed that a CanCan error appeared. After some debug statements, it became clear that current_user wasn’t set, but only on the POST/DELETE methods. Hmmm. What could be causing this? ...

April 11, 2012 · 1 min · Mark Simoneau

Don't Use DateTime in Rails 3

Rails 3 has good TimeZone support built in, but you have to use the right Date and Time classes to get full support. If you have this set in your application.rb config.active_record.default_timezone = :local, then you really need to use Time so that it properly identifies itself as being in the local timezone and not in UTC when passing to the database insert. ree-1.8.7-2011.03 :001 > DateTime.parse('2011-11-27 12:00:00 +0000') => Sun, 27 Nov 2011 12:00:00 +0000 ree-1.8.7-2011.03 :002 > DateTime.parse('2011-11-27 12:00:00') => Sun, 27 Nov 2011 12:00:00 +0000 ree-1.8.7-2011.03 :003 > Time.parse('2011-11-27 12:00:00 +0000') => Sun Nov 27 06:00:00 -0600 2011 ree-1.8.7-2011.03 :004 > Time.parse('2011-11-27 12:00:00') => Sun Nov 27 12:00:00 -0600 2011 So basically, Time.parse always returns the value in the local timezone, DateTime.parse always returns the value in UTC. To get complete compatibility, always use Time.parse. Trust me, I learned the hard way :)

November 29, 2011 · 1 min · Mark Simoneau

Migrating Serialized Columns in Rails

I recently switched a serialized column in Rails from one type (Hash) to another (OpenStruct) and ran into a little problem when I tried to migrate, namely, that loading the model threw a SerializationTypeMismatch error. Hmm… how am I going to get at the base YAML and translate all of these without being brittle? The answer is to copy the column, nullify it, and the load the raw YAML manually: ...

March 3, 2011 · 1 min · Mark Simoneau

Optional Heirarchal Checkbox Selection with Nested Attributes in Rails

I had a process where I wanted users to fill out a survey which had hierarchal categories AND be able to specify some additional data for specific capabilities that the user had. Now, you could easily do this for a small subset and hand-code every item, but I wanted a flexible survey system that allowed true hierarchy and generalized code. Let’s start off with the basic survey and capabilities models and relationships: ...

June 4, 2010 · 4 min · Mark Simoneau

Rails Controller Specs with users, roles and nested routes

I’ve long put off testing my controllers because of user authentication and nested controllers, dealing with stubs, etc. But today, a fully working test! As background, Advertisers have many trackers and the routes look like this: ActionController::Routing::Routes.draw do |map| map.resources :advertisers do |advertisers| advertisers.resources :trackers end end To set everything up in the specs, I included all the files in the spec/support directory and used Mocha as my mock framework Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} Spec::Runner.configure do |config| config.mock_with :mocha end Then I set up my factories (rather than fixtures) using Factory Girl ...

March 3, 2010 · 3 min · Mark Simoneau

PDF Generation in Rails... The Right Way

As long as we’re talking about efficiency here, one of the ways to be more efficient is to use the right tool for the job. I’ve done PDF generation on 3 different projects but the PDF generation I did yesterday was by far the easiest. What I thought would take me 2 days ended up taking about 3 hours (with research, etc). If you’re not using Ruby to automate some part of your job or life, I feel sad for you (at least a little). The next time you need to generate PDFs, why not try out the excellent Prawn library? Not familiar, you say? Well, let’s dive right in, shall we? ...

February 12, 2010 · 3 min · Mark Simoneau

The Path to Rails 3: Greenfielding new apps with the Rails 3 beta

Upgrading applications is good sport and all, but everyone knows that greenfielding is where the real fun is. At least, I love greenfielding stuff a lot more than dealing with old ghetto cruft that has 1,900 test failures and 300 errors, 20,000 line controllers, and code that I’m pretty sure is actually a demon-brand of PHP. Building a totally new app in Rails 3 is relatively simple especially if you’ve done it in previous Rails versions, but there a few changes that can trip you up. In the interest of not missing a step someone may need, this post is a simple walkthrough of building a new app with Rails 3. ...

February 9, 2010 · 1 min · Mark Simoneau