1

What's the best way to implement an interface that looks like this in rails?

interface example

Currently I'm using Searchlogic, and it's a tad painful. Problems include:

  • Making sure certain operations remain orthogonal -- for example, if you select "Short Posts" and then search, your search results should be restricted to short posts.

  • Making sure the correct link gets the "selected" class. Right now the links are <a>'s, so maintaining this state client-side is tricky. I'm hacking it by having the AJAX response to, say, sorting return a new sort links section with the correct link "selected". Using radio buttons instead of <a> tags would make it easier to maintain state client-side -- maybe I should do that?

Spooky
  • 2,966
  • 8
  • 27
  • 41
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272

1 Answers1

4

I recently solved a similar problem using named_scopes and some ruby metaprogramming that I rolled up into a plugin called find_by_filter.

find_by_filter accepts a hash of scope names and values, and chains them into parametised scope calls. If the model has a named_scope that matches the provided name, this is called.If no named_scope is found, an anonymous scope is created.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
  • Cool! Does find_by_filter deal with SQL injection etc? – Tom Lehman May 21 '09 at 04:18
  • Parameters are bound in your named scopes - so if you handle that correctly you should be fine. The internals are quite simple - feel free to just rip off the idea :P. Scopes can be chained to form complex queries - that's really the core of the approach. I also validate against anticipated filter options (using something like "if accepted.include?(params[:filter])" where accepted is an array of accepted strings. – Toby Hede May 22 '09 at 00:10