https://github.com/majidimanzade/searchkon
Advanced active_record Search/Filter Command
https://github.com/majidimanzade/searchkon
activerecord ruby ruby-on-rails search
Last synced: about 1 year ago
JSON representation
Advanced active_record Search/Filter Command
- Host: GitHub
- URL: https://github.com/majidimanzade/searchkon
- Owner: majidimanzade
- License: mit
- Created: 2020-05-24T06:31:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-12T10:07:40.000Z (about 6 years ago)
- Last Synced: 2025-03-24T16:38:48.560Z (about 1 year ago)
- Topics: activerecord, ruby, ruby-on-rails, search
- Language: Ruby
- Homepage:
- Size: 24.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Searchkon
Searchkon is Advanced active record search(filter) command that makes easy to search throw models and their relationships.
## Introduction
Lets say we want to return a list of products filtered by multiple parameters. our request contain below parameters:
```
{
title: 'foobar',
id: [1, 2, 3, 4],
created_at: '(2012-12-21..2019-12-21)'
categories.name: 'mobile'
}
```
Filter above parameters with Searchkon gem:
```rb
Searchkon::QueryBuilder.filter('Product', filters)
```
## Getting Start
Add Searchkon to your Gemfile:
```sh
gem 'searchkon'
```
### Searchable columns
at the first we should determine witch columns of model can be filter in Searchkon
```rb
class Product < ActiveRecord::Base
has_many :coupons
has_many :payments
def self.searchable_columns
{
like: ['title', 'coupons.code'],
exact: [
'created_at',
'coupons.title', ## relational filter on coupons table
'payments.id', ## relational filter on payments table
'id'
]
}
end
end
```
like: if you add specific column in your like scope, your query will be like below sql.
```sql
select * from products where title like %foo%
```
exact: Searchkon create query using equal operation.
```sql
select * from products where created_at = foo
```
### Simple where query
```rb
params = {
id: 1,
title: 'foobar'
}
```
```rb
Searchkon::QueryBuilder.filter('Product', params)
```
sql result:
```sql
SELECT "products".* FROM "products" WHERE (products.title like '%foobar%') AND "products"."id" = 1
```
### Search Range
```rb
params = {
id: '(1..10)',
created_at: '(2012-12-21..2019-12-21)'
}
```
sql result:
```sql
SELECT "products".* FROM "products" WHERE (products.title like '%foobar%') AND "products"."id" = 1
```
### Search in relational table
```rb
params = {
'coupons.id': [1,4,8]
}
```
sql result:
```sql
SELECT "products".* FROM "products" INNER JOIN "coupons" ON "coupons"."product_id" = "products"."id" WHERE "coupons"."id" IN (1, 4, 8)
```
### Invalid Column in query
if your filter parameters contain invalid column name, Searchkon skip it and create query without that column.
```rb
invalid_mock_params = {
id: 1,
foo: 'foobar'
}
```
```rb
Searchkon::QueryBuilder.filter('Product', invalid_mock_params)
```
sql result:
```sql
SELECT "products".* FROM "products" WHERE "products"."id" = 1
```