Where'd that month go?

OK, so it’s been a while since I posted a blog update. I’ve been busy- Band of Brothers declared war against the Northern Coalition so I’ve gotten swept up in the fighting and commanding. EVE Metrics has progressed well with constant work, and my holiday in Scotland was a success on all counts. And now it’s August. Blimey.

Anyway, back to Rails I go. I’ve been pondering the usual problems. REST is great for doing stuff on publicly accessible models- news posts and the like, for example. And scaffolds/make_resourceful can do that really, really well. But what about when you want to cater for private resources?

Take the API key implementation for IntelServe, an open-source EVE intelligence and scout management app I’ve been scraping together.

class ApiKeysController < ApplicationController
  before_filter :login_required
  # GET /api_keys
  # GET /api_keys.xml
  def index
    @api_keys = ApiKey.own_keys(current_user.id)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @api_keys }
    end
  end

  # GET /api_keys/1
  # GET /api_keys/1.xml
  def show
    @api_key = ApiKey.key(current_user.id,params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @api_key }
    end
  end
  # Other methods omitted for brevity, you get the idea.
end
# API key class
class ApiKey < ActiveRecord::Base
  named_scope :own_keys, lambda { |user_id| { :conditions => {:user_id => user_id}, :order => 'created_at DESC' } }
  named_scope :key, lambda { |user_id,api_key_id| { :conditions => {:user_id => user_id, :api_key_id => api_key_id} } }
end
So the question is, how can you do this better? Named scopes tidy things up, but it’s not exactly great. Has anyone else out there come up with a neater fix for the problem of non-DRY code and repeated applications of current_user.id and useless arguments?

Posted by James Harrison on Sunday, August 03, 2008

Salvis wrote:

Hey, my alliance is taking out a BoB pet for you. Least you could do is update eve-metrics!

Add a comment