CodingPrime

<strong>kung fu</strong>Fork me on GitHub

ModelMe...why?

My day job consists of architecting and maintaining several Rails applications. Some of these must deal with custom data sources that were architected before there were standards for inter-application communication (SOAP, REST, XML-RPC, etc). One in particular is is a custom TCP server that responds to method calls (using XML as the transport mechanism). Replacing the service is not an option (as is usually the case with any PITA legacy problem), so I have to communicate with it.

Skipping asset precompile

Waiting on asset precompilation is a PITA, especially if no assets have been changed. Thankfully, Ben Curtis came up with a workable solution. Except...

Ben's method fails on the initial deployment, because current_revision tries to read /path/to/app/current/REVISION, which won't exist yet. After trying several approaches, I have arrived at one which works for me. There are probably other, more elegant approaches, but this one seems clear enough for my purposes. Posting it here for posterity.

RSpec, Autotest, Rails3, and Engines

I've got an app I'm building that is comprised of several engines running in vendor/nsweb. I want each engine to have its own specs, without cluttering up the main app's spec folder. I also want to be able to run tests for ALL engines at once, both using rake and through autotest. To accomplish the first task, I added the following:

# lib/tasks/rspec.rake
RSpec::Core::RakeTask.module_eval do
  def pattern
    extras = []
    Dir[File.join( File.expand_path( 'vendor' ), 'nsweb', '*' )].flatten.each do |dir|
      if File.directory?( dir )
        extras += File.join( dir, 'spec', '**', '*_spec.rb' ).to_s
      end
    end
    [@pattern] | extras
  end
end

Form cancel button (without Javascript)

Found this today, and banged my head on the desk for never thinking of this when trying to add a cancel button to a form, regardless of the language.

Custom RSpec matcher: require_authentication_for

Like everyone, I have a bunch of controller actions that need to be protected from unauthorized users. I've got RSpec tests around these methods that look like this:

describe UsersController, 'as guest' do
  before( :each ) do
    controller.stubs( :current_user ).returns( nil )
  end

  describe 'index' do
    it 'should prevent access' do
      lambda { get :index }.should
        raise_error( Exceptions::PermissionDenied )
    end
  end

  describe 'edit' do
    it 'should prevent access' do
      lambda { get :edit, :id => 1 }.should
        raise_error( Exceptions::PermissionDenied )
    end
  end

  describe 'create' do
    it 'should prevent access' do
      lambda { post :create, :id => 1 }.should
        raise_error( Exceptions::PermissionDenied )
    end
  end
end