Skip to content

Scoped search 3.0 is out!

This version is all about infrastructure update, see the release notes:

  • Add support for ActiveRecord 4.1
  • Drop support for Ruby 1.8
  • Drop support for ActiveRecord 2.
  • No longer require jQuery UI automatically; you have to do this yourself now.
  • Auto completion: deprecate javascript helpers in favor of asset pipeline.
  • Remove some deprecated 1.x APIs.
  • Upgraded to RSpec 3.
  • Extract Oracle query builder enhancements to plugin gem: github.com/wvanbergen/scoped_search-oracle_enhanced
  • Extract H2 query builder enhancements to plugin gem: github.com/wvanbergen/scoped_search-h2

The complete list of changes can be found here

A brand new scoped_search 2.3.7 just released.

This time we have a new feature many thanks to cj. The new scoped search release now supports syntax similar to SQL IN and NOT IN. It is now possible to write the following syntax:

auth_level ^ (5,7,14)

instead of having to do

auth_level = 5 OR auth_level = 7 OR auth_level = 14

Same goes for:

first_name !^ ('chris', 'peter')

instead of having

first_name != 'chris' and  first_name != 'peter'

Except for the new feature we have one bug fixed in this release.

Many thanks to Ohad Levy for taking the time to report the bugs.

The release is available to download from rubygems.org . Sources can be found in github

scoped_search 2.3.6 released

Yet another bug fixes release is available to download from rubygems.org. Sources can be found in github.

Changes from last release(2.3.5):

  • when external methods create an empty conditions string,  an invalid sql is produced.
  • suggest value completion after operators without typing a space.
  • safer code to prevent possible null pointer exception in the auto completer.

Many thanks to Brad Backingham and Steve Traylen for taking the time to report the bugs.

New scoped search Release – 2.3.5

Another bug fixes release is available to download from rubygems.org. Sources can be found in github.

All the fixed bugs are related to the auto-completer component.

Changes from last release(2.3.4):

  • value and key-name auto completer may produce abiguoes sql.
  • fixed value auto-completer sql exception when single quote is used.
  • fixed value completion after the first quotes.
  • auto-completer fails when query starts with “or “

Many thanks to Brad Backingham,  Eric Helms  and Jeff Weiss  for taking the time to report the bugs.

scoped search 2.3.4 has been released

A new scoped_search bug fixes release is available to download from rubygems.org. Sources can be found in github.

Changes from last release(2.3.3):

Many thanks to Brad Backingham,  Eric Thomas and Tim Speetjens for taking the time to report bugs, and send patches.

New release of scoped_search

A new scoped_search release is available 2.3.3
http://rubygems.org/gems/scoped_search/versions/2.3.3

2.3.2 was skipped by mistake. I made  an error when running the release script.

Changes from last release(2.3.1):

  • Auto-completer results can now be filtered, For usage see:

permission-model-for-the-search

  • Fixed issue with decimal columns

https://github.com/wvanbergen/scoped_search/issues/12

  • Auto completer ajax controller can now pass a filter parameter to the auto-completer method in addition to the search string.

usage:

<%= auto_complete_field_tag_jquery(
:search,
params[:search],
{:placeholder => 'Type Space for Search Options'},
{:filter => 'new filter option'}) %>

Scoped search comparison operators

Scoped search uses ActiveRecord infrastructure to learn about the database schema. The auto-completer uses the column type to suggest the optional operators. Textual fields will have ‘~’ (like), ‘!~’ (unlike), ‘=’ and ‘!=’. Numerical fields will have ‘>’, ‘<‘, ‘>=’, ‘<=’, ‘=’ and ‘!=’. Usualy this is something a developer using scoped search doesn’t need to worry about, scope search find the database column type and suggest the relevant operators, however there could be some cases where a developer will want to limit the operators set for a particular search term.

The syntax to control the auto-completer suggested operator list is to add :operators => { ‘=’, ‘!=’ } to the scoped_search command.

 

Searching a multiplexed database column

Multiplexing columns is an optimization used in some database  schema. The idea is to store multiple integers or boolean values  in a single integer column. The read and write operations are done using bit wise operators. For example we can store several flags in a single integer column, the first flag value is 1, the second 2, the third value is 4, etc.

Scoped search is able to expose multiplexed filed by specifying the offset and word size. In the common case of multiplexing booleans, the word size is 1.

scoped_search :on => :counters, :offset => 0 , :word_size => 6, :rename => :applied
scoped_search :on => :counters, :offset => 1 , :word_size => 6, :rename => :skipped

The search syntax will expose the multiplexed column as multiple columns.

Using scoped search UI helpers

Scoped search comes packed with some handy GUI helpers. An ajax auto completer that handles the auto-completer drop-down box, and column sort helpers.

The ajax auto-completer drop-down comes in two flavors one for JQuery and the other for Prototype. The code samples in this post will use the JQuery flavor.

Here is an example of the auto-completer ajax drop-down usage, in the view place the following form tag where you want the search box to show:

<%= form_tag books_path, :method => :get do %>
  <%= auto_complete_field_tag_jquery(:search, params[:search], {:placeholder => "Filter ..."}) %>
  <button id='submit_search'">Search</button>
<% end -%>

To control the look of the search box use the following css classes: ui-autocomplete-input, ui-autocomplete-loading and ui-autocomplete-error. The auto_complete_field_tag_jquery accepts additional options hash for further controlling it’s behavior such as delay and min_length.

Another useful helper method is the sort helper. The sort helper draws an arrow at the top of the sorted column pointing to the sort direction, it also adds order parameter to the url. Together with the ability of the scoped_search to add an order-by sql clause, it makes a complete sorting solution. The reason for sending a new request to the database instead of sorting in the client is that many applications uses pagination. When using pagination client side sort may cause wrong results. Here is an example of using the sort helper:

<table>
  <tr>
    <th><%= sort :name %></th>
    <th><%= sort :"author.last", :as => "Author" %></th>
  </tr>
  <% for book in @books %>
    <tr>
      <td><%= book.name %></td>
      <td><%= book.author %></td>
      <td><%= link_to "Show", book %></td>
      <td><%= link_to "Edit", edit_book_path(book) %></td>
      <td><%= link_to "Destroy", book, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

Applying permission model to search results

Enterprise applications usually have a role base access control (RBAC). The search results as well as the auto-completer suggestions must conform to the access control model. The recommended way to control the search results, using scoped_search, is chaining scopes. The scoped_search is a named scope (scope in rails 3), this means that it can be chained with other scopes in a natural way.

For example:

 scope :my_hosts, lambda {
where("owner_id = ?", User.current.id)
}
Host.my_hosts.search_for(query)

In the above code :my_hosts is the RBAC scope (defined in lines 1 to 3). In line 4 the search is chained to the RBAC scope.

For the auto-completer there is a new way of filtering the results in a similar way. In the Model we can define a scope called :compelter_scope , this scope should accept an options hash. The options hash is a way to pass parameters from the controller, such as the current user.

For example:
Model code:

scope :completer_scope, lambda { |options|
  where('owner_id = ?', options[:owner_id])
}

Controller code:

def auto_complete_search
  @items = Host.complete_for(params[:search], {:owner_id => User.current.id})
  render :json => @items
end

To read more about named scopes, here is a link to a detailed post on the subject: Skinny on scopes

Create your own website, free on WordPress.com.Create your own website.
Start free