Reload your rails helpers in between Spork runs.

06 Sep 2013

Although they have become persona non grata in Rails 4 (without the help of a gem). Rails Observers are a great way to move callback code out of your model. I personally think it is one of the most overlooked features of Rails 3.

Anywho. I digress.

If you are using Observers in guard-spork. You may notice that that changes you make to your observer classes don't get picked up without a restart.

The reason for this is because the observer require is defined in the config/application.rb file. So essentially its required once at initialization. So that will be the only time spork will reload these classes too.

One solution I've found is to reload the classes in your Spork.each_run block. Note your results may vary. I personally consider this "hackish", but since we these changes only pertain to the 'TEST' environment, I hesitantly say it gets the job done. So channel your inner "Cowboy Coder" and start shooting, Tex.

Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
    # the observers are loaded in application.rb we need to reload them here to
    # avoid restarting. May slow down application
    load "#{Rails.root}/app/models/my_evaluation_observer.rb"
    load "#{Rails.root}/app/models/my360_evaluation_observer.rb"
    load "#{Rails.root}/app/models/peer_evaluation_observer.rb"
  end
  FactoryGirl.reload
  RSpec.configure do |config|
    def svg_filename
      "#{Rails.root}/tmp/profile-#{Time.now.localtime.strftime("%H%M%S")}.svg"
    end
  end
end

David Southard a ruby guy

Contact Info: