I use Ernie Miller's excellent meta_search gem for searching the contents of tables in my Rails 3 Apps.
I have a lot of index views that are tables of data and each has a search form at the top of the page that uses meta search and the column headers use the sort links provided by meta_search to reorder the rows.
Typically I also have custom columns, such as the number of objects that are associated with a given row via some relationship. Some of those relationships can be quite complex and in some cases there is no simple way to express them in a way that works with meta_search.
I handle the sorting in the controller. It's outside the database but with my current application the amount of data makes this perfectly manageable.
I've come up with a way to let my custom column sorting coexist with meta_search and I reckon it might be useful to others.
All the code can be found at this gist on Github :https://gist.github.com/1019358
It works by adding another parameter called 'custom_sort' to the existing search[] parameters. The index action in the controller sees this, performs the custom sort and passes a custom_sort value to the view.
The view calls the custom_sort_helper with a set of arguments and that creates the column header link with the correct parameters to pass back to the controller.
So far it works well and allows me to sort on my custom columns with a set of rows selected by meta_search searches.
I'm sure the code could be more elegant - leave me a column and tell my how.
A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.
Subscribe to:
Post Comments (Atom)
Contributors
Archive of Tips
-
▼
2011
(44)
-
►
November
(11)
- strsplit in R
- Running R scripts on the Command Line
- Deleting a File that starts with '-' on UNIX
- Plotting a simple bar plot in R
- Captain Beefheart Song Titles
- When jQuery $.ajax() keeps returning the same valu...
- Updating a DIV with jQuery and Sinatra
- Latest version of jQuery on Google APIs
- Sorting on multiple String keys in Ruby
- Manipulating Model/Table/Controller Names in Rails...
- Parsing 96 / 384 well Plate Maps
-
►
February
(14)
- Rails, UTF-8 and Heroku
- Tables named with reserved words in MySQL
- Setting up PostgreSQL on Mac OS X 10.6
- Problem with sqlite when installing the taps ruby ...
- Rails, Devise and custom User models
- Basic image rollover effect in jQuery
- Authentication in Mongo and Mongoid
- Always index columns that you want to sort on in M...
- Using Mongoid in Ruby applications outside of Rail...
- Using Twitter for System Notifications
- PATSY - a web service that makes patents easier to...
- JavaScript Bookmarklet that can create a New Windo...
- Running a Ruby 1.9 Sinatra app on Heroku
- Ruby 1.9 and incompatible character encodings
-
►
November
(11)
0 comments:
Post a Comment