The latest on MetaSearch

Version 0.3.0 of MetaSearch is out. Changes since the last time I posted include additional view helpers for gathering array input, the addition of unit tests, and easier attribute and association exclusion that honors excluded attributes through associations.

check_boxes and collection_check_boxes

If you need to get an array into your where, and you don’t care about parameter order, you might choose to use a select or collection_select with multiple selection enabled, but everyone hates multiple selection boxes. MetaSearch adds a couple of additional helpers, check_boxes and collection_check_boxes to handle multiple selections in a more visually appealing manner. They can be called with or without a block, so something like this is possible when you want additional formatting around your check boxes:

  <h3>How many heads?</h3>
  <ul>
    <% f.check_boxes :number_of_heads_in,
       [['One', 1], ['Two', 2], ['Three', 3]],
       :class => 'checkboxy' do |c| %>
    <li>
      <%= c[:check_box] %>
      <%= c[:label] %>
    </li>
    <% end %>
  </ul>

You can read more in the rdocs.

Excluding attributes and associations

If you’d like to prevent certain associations or attributes from being searchable, you can control this inside your models:

  class Article < ActiveRecord::Base
    metasearch_exclude_attr :some_private_data,
                            :another_private_column
    metasearch_exclude_assoc :an_association_that_should_not_be_searched,
                             :and_another
  end

You get the idea. Excluded attributes on a model will be honored across associations, so if an Article has_many :comments and the Comment model looks something like this:

  class Comment < ActiveRecord::Base
    validates_presence_of :user_id, :body
    metasearch_exclude_attr :user_id
  end

Then your call to Article.search will allow :comments_body_contains but not :comments_user_id_equals to be passed.

Was this post interesting? Maybe even helpful? Please share it!


About this entry