This module implement Poor Man's Search Engin to a active-record object.
Poor Man's Search Engin is a SQL anti pattern that using LIKE with wildcards search..
Installation
Add this line to your application's Gemfile:
gem 'poor_man_search'
And then execute:
$ bundle
Usage
Example
A master table : users
name | rank | registered_at | |
---|---|---|---|
taro@yamada.example.com | 山田太郎 | 1 | 2013-12-31 23:21:11 |
hana@yamada.example.com | 山田華 | 3 | 2014-2-28 3:15:00 |
chad@miller.example.com | Chad Miller | 1 | 2014-2-28 16:23:15 |
Configure like this.
class User < ActiveRecord::Base
extend PoorManSearch::Searchable
string_search :name, :email
number_search :rank
time_search :registered_at
end
Search!
User.search "yamada"
name | rank | registered_at | |
---|---|---|---|
taro@yamada.example.com | 山田太郎 | 1 | 2013-12-31 23:21:11 |
hana@yamada.example.com | 山田華 | 3 | 2014-2-28 3:15:00 |
User.search "yamada 太郎"
name | rank | registered_at | |
---|---|---|---|
taro@yamada.example.com | 山田太郎 | 1 | 2013-12-31 23:21:11 |
User.search "1"
name | rank | registered_at | |
---|---|---|---|
taro@yamada.example.com | 山田太郎 | 1 | 2013-12-31 23:21:11 |
chad@miller.example.com | Chad Miller | 1 | 2014-2-28 16:23:15 |
User.search "2/28"
name | rank | registered_at | |
---|---|---|---|
hana@yamada.example.com | 山田華 | 3 | 2014-2-28 3:15:00 |
chad@miller.example.com | Chad Miller | 1 | 2014-2-28 16:23:15 |
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request