Garbage Burrito

Trash Talk About Ruby On Rails & Web Development

Rails: Super Cool Simple Column Sorting

Rails: Super Cool Simple Column Sorting
Ben Kittrell - 04 19, 2007 @ 12:17PM
Comments: 9

Of all the things I hate doing, column sorting has to be top on my list.  It always seems to be more difficult than it should be.  Here's my take on a really simple way to implement column sorting for your Rails application.  I call it Super Cool Simple Column Sorting, or SCSCS.  That's pronounced 'ssssssss'.

The first part is the HTML helper.

  def sort_link(title, column, options = {})
    condition = options[:unless] if options.has_key?(:unless)
    sort_dir = params[:d] == 'up' ? 'down' : 'up'
    link_to_unless condition, title, request.parameters.merge( {:c => column, :d => sort_dir} )
  end

    
It takes the title you wish to display, the name of the column you wish to sort, and any extra options you wish to add to the link.  You can also pass an :unless flag to turn the link on or off.  Put this in a helper somewhere, maybe application_helper.rb.

To use it...

  <%= sort_link 'Company', :company_name %>

 
This will default to sorting up, or ASC, and will toggle up or down automatically.

Now you just need the SQL helper.

  def sort_order(default)
      "#{(params[:c] || default.to_s).gsub(/[\s;'\"]/,'')} #{params[:d] == 'down' ? 'DESC' : 'ASC'}"
  end

    
Put this somewhere the controller can see it, maybe application.rb.  Then just call it in the query in your controller.

  def list
    @applications = Application.find(:all, :order => sort_order('created_at'))
  end

 
The sort_order method takes the default column you wish to sort on.  

I really like this simple helper based approach.  It's not quite as slick as making a plugin, but it's easier to tweak it for different cases, and less than 10 lines.

Tags: Rails, helpers
Comments: 9

Comments

1. Herb - 05 28, 2007 @ 01:22PM

Seeing as its the first google hit and no one has posted a comment... thanks works great for me!

2. Simon - 06 13, 2007 @ 04:27AM

Isn't this basically asking for injection attacks?

3. Ben Kittrell - 06 13, 2007 @ 07:41AM

You're right, I should have clarified that I'm using this in a secured admin environment where that's not an issue. I've added a fix to the code.

4. Evan - 06 13, 2007 @ 11:06AM

Worked great for me too. Thanks!

5. Jon - 07 05, 2007 @ 12:31PM

This worked well for me also. Is the fix for the injection already attacks included above?

6. Ben Kittrell - 07 05, 2007 @ 01:15PM

@Jon

Yes, the gsub call will protect against injection.

7. Rich Allen - 12 16, 2007 @ 05:59AM

Exactly what I have been looking for, thanks!

8. Derrick - 12 21, 2007 @ 09:37AM

Great work, I was getting very fustrated searching feverishly for a simple solution that I could understand upon first clance, instead of the more complex and convoluted plugins everyone else is tring to viral around.

9. Thai - 03 20, 2008 @ 04:51PM

"Yes, the gsub call will protect against injection."

Can you explain how this would prevent injection?

Great code, btw. I was able to integrate this with faster pagination too!

Post a Comment




powered by : Doodlekit Online Free Website Builder : developed by : Doodlebit™ Website Company