Project

seeker

0.0
No commit activity in last 3 years
No release in over 3 years
This Ruby on Rails gem provides an elegant way to separate search logic out of models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Seeker

This Ruby on Rails gem provides an elegant way to separate search logic out of models and build search forms just like create/update ones.

Right now it supports only sunspot and was tested on Rails 3.2.13.

Installation

gem 'seeker'

Usage

Given you have Product model:

class Product < ActiveRecord::Base
  mount_searcher ProductSearcher
end

Create corresponding searcher:

# app/searchers/product_searcher.rb
class ProductSearcher < Seeker::Base
  attr_accessor :name, :max_price

  searchable do
    text :name
    integer :price
  end

  def search(query)
    query.fulltext(:name, @name) if @name.present?
    query.with(:price).less_than_or_equal_to(@max_price) if @max_price.present?
  end
end

Use it in your controller:

class ProductsController < ApplicationController
  def index
    @searcher = Product.searcher params[:product]
  end
end

And use it in your view with form helper, just like model:

# app/views/products/index.html.slim
= form_for @searcher do |f|
  = f.label :name
  = f.text_field :name

  = f.label :max_price
  = f.number_field :max_price

  = f.submit

h1 Search results
ul
  = @searcher.results.each do |product|
    li #{product.name} - #{product.price}

TODO