spec/controllers/generic_rest_controller_spec.rb


			
describe GenericRestController do
  before :each do
    # removing the stub! requires me to send :resource_name 
    # in the the action parameters.
    controller.stub!(:resource_class).and_return(Post)
  end

  describe 'responding to GET index' do
    it 'should expose all find records as @collection' do
      Post.should_receive(:find).and_return([mock_post])
      get :index #, :resource_name => 'post'
      assigns[:collection].should == [mock_post]
    end
  end
end

app/controllers/generic_rest_controller.rb


			
class GenericRestController < ApplicationController
  def index
    @collection = resource_class.find(:all)
  end

  private
  def resource_class
    @resource_class ||= params['resource_name'].camelize.constantize
  end
end