The simplest way to search the data in ActiveRecord models.
It offers simple but useful features:
- Search on the default attributes
- Override default search attributes to specific attributes (Credit goes to @abdullahtariq1171)
- Search using patterns
- Ruby block support to extend the search query
- Simple search returns an
ActiveRecord::Relation
object
Mostly on the admin side, we do have a standard text field to search the data on the table.
Sometimes we want to search through the attributes like title, content and ratings on the
post model or email, username and description on the user model. For those searches, we use
MySQL's or PostgreSQL's LIKE
operator to get the results. While doing the same thing again
and again on the different models, you add lots of duplication in your code.
Add this line to your application's Gemfile:
gem 'ruby_simple_search'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install ruby_simple_search
Define attributes that you want to search on it
class Post < ActiveActiveRecord::Base
include RubySimpleSearch
simple_search_attributes :title, :description
end
class User < ActiveActiveRecord::Base
include RubySimpleSearch
simple_search_attributes :email, :username, :address, :age
end
If you don't provide any attribute at the time of searching, it will use simple_search_attributes
from the model.
class User < ActiveActiveRecord::Base
include RubySimpleSearch
simple_search_attributes :email, :username, :address
end
Post.simple_search('york')
# It will search in :email, :username and :address only
If you want to perform a specific search on particular attributes, you can pass specific attributes with attributes
option.
class User < ActiveActiveRecord::Base
include RubySimpleSearch
simple_search_attributes :email, :username, :address
end
Post.simple_search('york')
# It will search in :email, :username and :address only
Post.simple_search('york', attributes: :address)
# It will search in :address only
User.simple_search('york', pattern: :ending, attributes: [:email, :address])
# It will search in :email and :address only with 'ending' pattern
You can pass a LIKE
pattern to the simple_search
method.
Patterns:
- beginning
- ending
- containing (Default pattern)
- plain
Post.simple_search('york', pattern: :beginning)
# It will search like 'york%' and finds any values that start with "york"
Post.simple_search('york', pattern: :ending)
# It will search like '%york' and finds any values that end with "york"
Post.simple_search('york', pattern: :containing)
# It will search like '%york%' and finds any values that have "york" in any position
Post.simple_search('york', pattern: :plain)
# It will search like 'york' and finds any values that have "york" word
User.simple_search('35') do |search_term|
["AND age = ?", search_term]
end
Block should return an array of search condition and values.
Model.simple_search('string') # => ActiveRecord::Relation object
Model.simple_search('string').to_sql
# OR
User.simple_search('mechanicles') do |search_term|
["AND address != ?", search_term]
end.to_sql
# => It will return an SQL query in string format
- 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