1. August 9, 2010

    Rails, Ajax and Exceptions? Bring it on.

    Recently I’ve been working on a content management system that’s very Ajax heavy. I think Ajax is great, but if your request raises an exception you’re going to get some unexpected results. In the case where your Ajax request is expecting a JavaScript response to evaluate the enduser will sit there with no response and no indication of an error. We can do better than that.

    If your application uses Ajax in two or three places, it’s not hard to code for those specific use cases, but what about three or four instances per page? We’ll need to DRY this up.

    Read more...

  2. June 25, 2010

    Application Settings through ActiveRecord

    After a few side projects I’ve noticed that I’d use this approach for a settings object pretty often. My settings table is very basic, just two columns, name and value. The magic happens inside the Settings model.

    # app/model/settings.rb
    class Settings < ActiveRecord::Base
      class << self
        def setup
          return if @settings
          @settings = {}
          all.each { |s| @settings[s.name.to_sym] = s.value }
        end
    
        def [](key)
          setup
          @settings[key]
        end
      end
    end
    

    I gave myself a setup method that cycles through every entry in my settings table and stores them in the @settings hash. Notice I call return if @settings is defined because there’s no need in rebuilding @settings. Next, I define the [] method to make the model act like a hash. Inside the [] method I return the value @settings at the position of the provided key.

    It allows me to do stuff like this:

    puts Settings[:name]
    puts Settings[:version]
    

    It’s really simple, but I like it. What about you?

  3. June 21, 2010

    Rake Tasks 102

    This is a follow up post to Rake Tasks 101. In the 101 post we created Rake tasks, setup dependencies and made our tasks reusable by passing in parameters. In Rake Tasks 102 we’ll be building on those practices, interfacing with a Rails environment and leveraging the cron to automate our Rake task.

    Read more...

  4. June 6, 2010

    Rake Tasks 101

    I’ve been working with Rake quite a bit on my current project so I thought I’d share some beginner tips.

    Before I go into Rake, what is it? Rake is a Ruby-based build program. Ruby on Rails uses Rake quite a bit in it’s process. If you’ve worked on a Rails project you’ll used one, some or all of the following: rake db:create, rake gems:unpack, rake db:migrate, and rake test. Now that’s not all of Rail’s Rake tasks, just some common ones.

    You’re here to make your own Rake tasks so lets get started!

    Read more...

  5. November 29, 2009

    Authlogic, RSpec and :priority_record=>nil... wtf.

    I think Authlogic is great. I think RSpec is great, but damn did I ever give myself a confusing error. My view specs were passing so I moved onto the controllers. The controller specs passed so I moved onto the models. After the models passed I tested all of the specs and found that I had broken the controllers some how.

    When running my SessionsController tests I’d get an error like this:

    <UserSession (class)> received :new with unexpected arguments expected: ({:password=>"valid password", :login=>"dane"})
      got: ([{:priority_record=>nil}, nil])
    

    At other times I’d get:

    Mock "UserSession_1050" received unexpected message :priority_record= with (nil)
    

    Read more...