Sort HTML tables in a Rails app.
script/plugin install git://github.com/dancroak/sortable_table.gitIn app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base include SortableTable::App::Controllers::ApplicationController endIn app/helpers/application_helper.rb:
module ApplicationHelper include SortableTable::App::Helpers::ApplicationHelper endparams[:sort] and params[:order]
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 endIf you need to test an action other than a simple “get :index,” you can pass a
block to #should_sort_by_attributes:
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.
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.emailAuthors
-——
Dan Croak, Joe Ferris, and Boston.rb.
Copyright © 2008 Dan Croak, released under the MIT license