Skip to content

This Ruby on Rails gem provides an elegant way to separate search logic out of models.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt
Notifications You must be signed in to change notification settings

LegendSzymon-P/seeker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

This Ruby on Rails gem provides an elegant way to separate search logic out of models.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages