I've been working on a gem called fluorescent for a little while, and I just recently cleaned it up and released it. This CMS is using it, so it seems to be working.
Basically, I wanted to be able to highlight search terms in search results and only show them in context for things like article bodies, which could potentially be several pages long. Truncating them to a certain length is certainly beneficial.
Here's a brief rundown:
Add fluorescent to your Gemfile:
Next, you need to pass your search results from ActiveRecord to fluorescent.
I do it like so:
Then, in my model:
Some very simple controller code:
How you display the results in the view is up to you. I simply make sure the query parameter exists, then make sure there are actual results, and then iterate over the array of hashes:
I realize there are several points here that need to be brought up to the Rails 4 code standard, but I think these snippets get the point across. I think it would be wonderful just to be able to call something like
@results = fluorescent(@resultset) and have it just do the right thing, but that's down the road a bit in terms of development and complexity.
You'll also note that I keep track of the raw ActiveRecord resultset. This isn't really used right now, but originally it was in case you needed to get to the non-hash serialized ActiveRecord row objects.
All thoughts and suggestions, feedback, and critique are welcome. Please enjoy!