• Stars
    star
    264
  • Rank 149,502 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 16 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily search you ActiveRecord models with a simple query language that converts to SQL.

The scoped_search gem makes it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope search_for. Based on a definition in what fields to look, it will build query conditions and return those as a named scope.

Scoped search is great if you want to offer a simple yet powerful search box to your users and build a query based on the search string they enter. It comes with a built-in search syntax auto-completer and a value auto-completer. It also comes with a set of helpers that makes it easy to create a clean web UI with sorting and an ajax auto-completer.

Preview¶ ↑

A demo application using the scoped search can be found here: github.com/abenari/scoped_search_demo_app A running version of the demo application can be found here: scope-search-demo.heroku.com A Rails 3.2 demo with assets pipeline and twitter bootstrap: theme github.com/abenari/search-demo2

Installing¶ ↑

Add the following line in your Gemfile, and run bundle install:

gem "scoped_search"

Scoped search 4.x supports Rails 4.2 to 5.0, with Ruby 2.0.0 or higher. Use previous versions, e.g. 3.x to support older versions of Rails or Ruby.

Scoped search requires you to define the fields you want to search in:

class User < ActiveRecord::Base
  scoped_search on: [:first_name, :last_name]
end

For more information about options and using fields from relations, see the project wiki on search definitions: github.com/wvanbergen/scoped_search/wiki/search-definition

Now, the search_for scope is available for queries. You should pass a query string to the scope. This can be empty or nil, in which case all no search conditions are set (and all records will be returned).

User.search_for('my search string').each { |user| ... }

The result is returned as named_scope. Because of this, you can actually chain the call with other scopes, or with will_paginate. An example:

class Project < ActiveRecord::Base
  scoped_search on: [:name, :description]
  named_scope :public, conditions: { public: true }
end

# using chained named_scopes and will_paginate in your controller
Project.public.search_for(params[:q]).paginate(page: params[:page], include: :tasks)

Search profiles¶ ↑

If you include a :profile option to the scoped_search call, the fields specified will only be searched when you include this :profile into the search_for command as well:

class User < ActiveRecord::Base
  scoped_search on: :public_information
  scoped_search on: :private_information, profile: :members
end

This will only search the :public_information column:

User.search_for('blah blah blah')

And this will only search the :private_information column:

User.search_for('blah blah blah', profile: :members)

More information¶ ↑

More information about usage can be found in the project wiki: github.com/wvanbergen/scoped_search/wiki/usage

Query language¶ ↑

The search query language is simple, but supports several constructs to support more complex queries:

words

require every word to be present, e.g.: some search keywords

phrases

use quotes for multi-word phrases, e.g. "police car"

negation

look for “everything but”, e.g. police -uniform, -"police car", police NOT car

logical keywords

make logical constructs using AND, OR, &&, ||, &, | operators, e.g. uniform OR car, scoped && search

parentheses

to structure logic e.g. "police AND (uniform OR car)"

comparison operators

to search in numerical or temporal fields, e.g. > 22, < 2009-01-01

explicit fields

search only in the given field. e.g. username = root, created_at > 2009-01-01

NULL checks

using the set? and null? operator with a field name, e.g. null? graduated_at, set? parent_id

A complex query example to look for Ruby on Rails programmers without cobol experience, over 18 years old, with a recently updated record and a non-lame nickname:

("Ruby" OR "Rails") -cobol, age >= 18, updated_at > 2009-01-01 && nickname !~ l33t

For more info, see the the project wiki: github.com/wvanbergen/scoped_search/wiki/query-language

Additional resources¶ ↑

License¶ ↑

This plugin is released under the MIT license (see LICENSE).

This plugin was originally developed for Floorplanner.com by Willem van Bergen (github.com/wvanbergen) with help from Wes Hays (github.com/weshays). The current maintainer is Amos Benari (github.com/abenari).

More Repositories

1

request-log-analyzer

Create reports based on your log files. Supports Rails, Apache, MySQL, Delayed::Job, and other formats.
Ruby
2,245
star
2

chunky_png

Read/write access to PNG images in pure Ruby.
Ruby
1,035
star
3

kafka

Load-balancing, resuming Kafka consumer for go, backed by Zookeeper.
Go
373
star
4

state_machine-audit_trail

Log transitions on a state machine to support business process analytics. Superseded by
Ruby
277
star
5

oily_png

Native mixin to speed up ChunkyPNG.
C
174
star
6

adyen

Package to simplify including the Adyen payments services into a Ruby application.
Ruby
132
star
7

kazoo-go

Go library to access Kafka metadata in Zookeeper
Go
100
star
8

vertica

Ruby adapter for Vertica databases
Ruby
79
star
9

active_olap

OLAP extensions for ActiveRecord
Ruby
55
star
10

node-vertica

Pure javascript Vertica database client. Except it is written in CoffeeScript.
CoffeeScript
44
star
11

http_status_exceptions

A Rails plugin to use exceptions for generating HTTP status responses.
Ruby
28
star
12

activerecord-databasevalidations

Add validations to your ActiveRecord models based on your database constraints.
Ruby
26
star
13

clieop

A pure Ruby implementation for writing CLIEOP files to submit payments or direct charges to Dutch banks.
Ruby
14
star
14

sql_tree

Outdated, replaced by a much more complete SQL toolkit: https://github.com/wvanbergen/sql_toolkit
Ruby
14
star
15

kafka-consumer

High-level Ruby consumer for Kafka.
Ruby
12
star
16

ottawa-trash

This is for hoping that Ottawa's Garbage Collection works out better than Ruby's.
Ruby
6
star
17

schema_registry

Ruby client for Confluent Inc.'s schema-registry
Ruby
6
star
18

uuml

Instant Germanification of your web app!
Ruby
6
star
19

SysVIPC

Fork of the SysVIPC gem to fix packaging issues
C
6
star
20

dropbox-sync

Tool to synchronize folders between machiens using Dropbox and symbolic links.
Ruby
6
star
21

selekt

Toolkit to parse an manipulate SQL queries for testing and monitoring purposes.
Ruby
5
star
22

chunky_bits

Collection of image processing algorithms built on top of ChunkyPNG
Ruby
5
star
23

kazoo

Ruby library to access Kafka metadata in Zookeeper
Ruby
4
star
24

blogposts

Staging area for my blogposts
4
star
25

newrelic-vertica

Enable SQL monitoring in NewRelic RPM for the Vertica driver
Ruby
3
star
26

love

Ruby library to access the Tender REST API
Ruby
3
star
27

HackReduce2

3
star
28

tros

Cleaned up AVRO implementation
Ruby
2
star
29

guillaume-consulting

Guillaume Consulting website
Ruby
2
star
30

dreamcatcher

Simple gem to catch exceptions in Ruby code and send notifications.
Ruby
2
star
31

scoped_search-h2

H2 database support for scoped_search
Ruby
1
star
32

scoped_search-oracle_enhanced

Oracle enhanced support for scoped_search
Ruby
1
star