Git Product home page Git Product logo

simple_search's Introduction

SimpleSearch

SimpleSearch brings simple search to ActiveRecord. It ads simple_search named scope that accepts query as parameter.

The idea is that you provide the query and plugin does the rest (splits query to keywords and compose where statement).

This can be very useful in case you just want to filter list of records by some query, you have autocomplete field, … or something similar.

*FOR RAILS 2.X SUPPORT YOU SHOULD SWITCH TO “rails2” BRANCH*

Example

View

<% form_tag request.path, :method => 'get' do %>
  <%= text_field_tag :query, params[:query] %>
<% end %>

Model

class User < ActiveRecord::Base
  simply_searchable
end

# Columns: id, login, email, crypted_password, salt

Controller

class UsersController < ApplicationController
  def index
    @users = User.simple_search(params[:query]).all
  end
end

Query examples

Simple query

User.simple_search("vlado")
# => SELECT * FROM "users" WHERE (users.id LIKE '%vlado%' OR users.login LIKE '%vlado%' OR users.email LIKE '%vlado%' OR users.crypted_password LIKE '%vlado%' OR users.salt LIKE '%vlado%')

You can also provide :columns => :column1, :column2 option to limit search only to those columns.

class User < ActiveRecord::Base
  simply_searchable :columns => :login, :email
end

User.simple_search("vlado")
# => SELECT * FROM "users" WHERE (users.login LIKE '%vlado%' OR users.email LIKE '%vlado%')

More complex query

User.simple_search("vlado, cingel")
# will search for users matching 'vlado' and 'cingel' keywords
# => SELECT * FROM "users" WHERE ((users.login LIKE '%vlado%' OR users.email LIKE '%vlado%') AND (users.login LIKE '%cingel%' OR users.email LIKE '%cingel%'))

Installation

rails plugin install git://github.com/cingel/simple_search.git

More info

kolodvor.net/2010/06/26/simple-search-rails-plugin/

Copyright © 2010 Vlado Cingel, released under the MIT license

simple_search's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.