Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Sort HTML tables in your Rails app.
Ruby
tree: bc412cefd5

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
rails
shoulda_macros
test/rails_root
.gitignore
MIT-LICENSE
README.textile
Rakefile
VERSION.yml
init.rb
notes.rb
sortable_table.gemspec

README.textile

Sortable Table

Sort HTML tables in a Rails app.

Install

script/plugin install git://github.com/dancroak/sortable_table.git

In app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base include SortableTable::App::Controllers::ApplicationController end

In app/helpers/application_helper.rb:

module ApplicationHelper include SortableTable::App::Helpers::ApplicationHelper end

Conventions

params[:sort] and params[:order]

Test with shoulda macros:

context “enough Users to sort” do setup { 5.times { Factory :user } } should_sort_by_attributes :name, :email, :age context “GET to #index” do setup { get :index } should_display_sortable_table_header_for :name, :email, :age end end

If you need to test an action other than a simple “get :index,” you can pass a
block to #should_sort_by_attributes:

context ‘GET to show with id, sort, and order params’ do setup do 5.times { Factory :user } @search = Factory(:search, :name => ‘name’) end should_sort_by_attributes :name, :email do |sort, order| get :show, :id => @search.to_param, :sort => sort, :order => order end end

Controller

Use the sortable_attributes macro to list the… sortable attributes.

Then, in your index action, pass the sort_order method to your usual
order parameter for will paginate or named scope call.

class UsersController < Admin::BaseController sortable_attributes :name, :email def index @users = User.paginate :page => params[:page], :order => sort_order end end

View

Add some sugar for your views:

%h1 Users %table %tr = sortable_table_header :name => ‘Name’, :sort => ‘name’ = sortable_table_header :name => ‘E-mail’, :sort => ‘email’ - @users.each do |each| %tr %td= each.name %td= each.email

Authors
-——

Dan Croak, Joe Ferris, and Boston.rb.

Copyright © 2008 Dan Croak, released under the MIT license

Something went wrong with that request. Please try again.