what, is it good for? absolutely nothing

Posted by jcarroll

Feb 22

Ever write this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
before_filter :csv?, :only => :index

def index
  respond_to do |format|
    format.html { @users = User.all }
    format.csv { @users = User.to_csv }
  end
end

def csv?
  if request.format == :csv && ! logged_in?
    log_in
  end
end
This would be a lot nicer if we could get rid of that #== in #csv?
1
2
3
4
5
def csv?
  if request.format.csv? && ! logged_in?
    log_in
  end
end
Lil Jon drinks the CRUD juice.

Introducing What

What is a plugin that automatically adds those nice query methods to ActionController::Mime::Type, the type of object returned by Request#format. All the built in mime types are supported. Any custom mime types that you add will also have corresponding query methods.


Comments on this post

Dan

Feb 22

Dan said,

This is a pretty nice idiom. Its probably worth submitting as a patch to Rails, since I think it much cleaner than using == to test the format type.

Pratik

Feb 23

Pratik said,

That’s a nice idea ! In fact, why not just request.csv? instead of request.format.csv? ?

theJareCare

Feb 25

theJareCare said,

@ pratik

i thought about that but decided not to because that request object already has #get?, #post?, #xhr?. so i looked at it as the type of request being the HTTP verb and the type of the request’s format being something like #csv? or #vista_phone?


Sorry, comments are closed for this article.

© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots