codahale.com٭blog

Coda Hale lives in Berkeley, CA, where he writes about Ruby on Rails, usability, web design and development, and the occasional bit about bicycles.

Rails Plugin: http_caching

Allows your Rails application to take advantage of the caching mechanisms built into HTTP 1.1 (i.e., 304 Not Modified return code). Inspired by a Rails cookbook entry (manuals.rubyonrails.com/read/chapter/62). If it fits into your app’s architecture, HTTP caching can make a real difference in user-perceived speed. “And it must pop pop pop!”

Installation

If your project is source-controlled by Subversion (which it should be, really), the easiest way to install this is via Rails’ plugin script:

./script/plugin install -x http://svn.codahale.com/http_caching

If you’re not using Subversion, or if you don’t want it adding svn:externals in your project, remove the -x switch:

./script/plugin install http://svn.codahale.com/http_caching

Usage

An example using send_data:

  def get_orders
    @photo = Photo.find(params[:id])
    unless_cached_after @photo.updated_at do
      send_data @photo.data, :type => @photo.mime_type, :disposition => 'inline'
    end
  end

If the photo hasn’t been updated since the last time the client requested it, the photo isn’t sent, saving you time and bandwidth.

Another example, this time using a simple RHTML view:

  def get_user_profile
    @user = User.find(params[:id])
    render_unless_cached_after @user.updated_at
  end

A slightly more complicated RHTML example:

  def get_orders
    render_unless_cached_after Order.maximum(:updated_at), :layout => 'orders_feed', :action => 'all_orders'
  end

Or you can keep frequent refreshers from swamping your server:

  def shiny_object
    unless_cached_after 10.minutes.ago do
      @shiny_object = ShinyObject.complicated_database_call(:woo => :hah)
    end
  end

You get the idea.

(And yes, all I do is make Rails plugins.)

3 comments »

Rails Plugin: xhtml_content_type

xhtml_content_type allows you to set the default MIME type for rendered .rhtml views to application/xhtml+xml if the client supports it, and only falling back to text/html for older clients.

For more information as to why this is good behavior, read this: http://hixie.ch/advocacy/xhtml

Installation

If your project is source-controlled by Subversion (which it should be, really), the easiest way to install this is via Rails’ plugin script:

./script/plugin install -x http://svn.codahale.com/xhtml_content_type

If you’re not using Subversion, or if you don’t want it adding
svn:externals in your project, remove the -x switch:

./script/plugin install http://svn.codahale.com/xhtml_content_type

Alternatively, you can just check the trunk out from the repository, if you’re super-DIY.

cd path_to_rails_app
cd vendor/plugins
svn co http://svn.codahale.com/xhtml_content_type

Usage

xhtml_content_type is super easy to use. Add the method sends_xhtml_with_correct_content_type to a specific controller, or to ApplicationController to make all controllers send XHTML properly:

  class ApplicationController < ActionController::Base
    sends_xhtml_with_correct_content_type
  end

sends_xhtml_with_correct_content_type also accepts standard filter-style conditions, if you need them:

  class MySpecialController < ApplicationController
    sends_xhtml_with_correct_content_type :except => [:seriously_weird_action]
  end
  class MyOtherSpecialController < ApplicationController
    sends_xhtml_with_correct_content_type :only => [:the_only_regular_action]
  end

You can also explicitly specify an XHTML content type using this plugin:

  def xhtml_only_action
  	render :content_type => :xhtml
  end

Have fun!

15 comments »