Skip to content

Instantly share code, notes, and snippets.

@house9
Created March 23, 2010 15:23
Show Gist options
  • Save house9/341290 to your computer and use it in GitHub Desktop.
Save house9/341290 to your computer and use it in GitHub Desktop.
# see http://gist.github.com/341278 (sort_index.rb)
# see http://gist.github.com/341295 (sort_index_view_usage.html.erb)
# this example is an employees controller and using the will_paginate plugin
# however code should work fine with a standard ActiveRecord#find(:all, :order => ...
# put the sort_index.rb code in your rails lib directory
class EmployeesController < ApplicationController
# index sort constant config for sorting index action
# default is order by updated_at DESC
INDEX_SORT = SortIndex::Config.new(
{'updated_at' => 'updated_at'},
{
'full_name' => 'UPPER(first_name), UPPER(last_name)',
'email' => 'email'
}
#, optionally SortIndex::SORT_KEY_ASC
)
def index
@sortable = SortIndex::Sortable.new(params, INDEX_SORT)
@employees = Employee.paginate :page => params[:page], :order => @sortable.order
# render index.html.erb
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment