March 05 2009

Searchable Models - Now With Searchlogicby railsdog

The upcoming release of Spree is now using searchlogic to improve the way we do searching and paging. Checkout the very excellent README for more details on how searchlogic works. Unfortunately there is currently a small error in the date searching. I’ve fixed the error but we need to release before this patch can be accepted. Normally I would just vendor a hacked gem but lately we’ve been having problems with vendored gems. So we’ve created our own gem. This means there are a few additional steps you will need to do to get Spree running.

Special Steps For Obtaining the Modified Searchlogic Gem

*1.* Add the GitHub Gem Repository to your Gem Resources

gem sources -a

2. Instal this gem (and any other dependencies) the usual way

rake gems:install

Possible Changes Needed for your Extension Controllers and Views

There are some important implications for the controllers and views in your custom extensions. If you have any views where you are using paging you are going to need to change your paging controls slightly.

The Old Way

Inside of your index.html.erb you would have something like this in your index.html.erb

<%= render :partial => 'shared/paginate', :locals => {:collection => @orders, :options => generate_search_options(@filter)} unless @orders.empty? %>

You then had a big giant pile of code in the shared/paginate partial

<% if collection.page_count != collection.first_page -%>

<% end -%>         

The New Way

Inside of your index.html.erb you have this

<%= page_links(:prev => "« #{t('previous')}", :next => "#{t('next')} »") if @search.page_count > 1 %>

That’s it. No more shared partials. Now you’re starting to see how this simplifies things.

Now lets move on to the controllers. Each controller that needs to have any kind of search or paging needs to have a special collection method which is used by resource_controller.

The Old Way

<%= render :partial => 'shared/paginate', :locals => {:collection => @orders, :options => generate_search_options(@filter)} unless @orders.empty? %>   

The New Way

def collection
  @search = Order.new_search(params[:search])
  if params[:search].nil? || params[:search][:conditions].nil?
    @search.conditions.checkout_complete = true
  @search.order_by ||= :created_at
  @search.order_as ||= "DESC"
  @search.per_page = Spree::Config[:orders_per_page]
  @collection = @search.find(:all, :include => [:user, :shipments, {:creditcards => :address}] )

Obviously the new way is simpler with less lines of code to maintain. The searchlogic gem also takes care of a lot of what we used to do ourselves in terms of performing searches. All existing Spree searches have been overhauled to work with searchlogic. So if you’re doing any custom searching in your applications you will want to look at admin/orders_controller.rb or other relevant controllers for some ideas on how to pull it off. Just to repeat, the searchlogic README is very detailed. It will explain everything you need to know about how searchlogic works.

Most applications will only have to make minor changes. Again, you don’t need to do anything unless you are overriding a Spree view that uses paging or a Spree controller that provides a searchable list and/or paging. This should also allow us to build a very powerful advanced search form for the customer facing portion. We’re steadily working towards that and other great features so stay tuned.