searchkon 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +153 -1
- data/lib/searchkon/query_builder.rb +1 -1
- data/searchkon.gemspec +2 -2
- data/spec/query_builder_spec.rb +15 -27
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c48b564308ba6cfc2179d89383d7ae2602655466a7468631413a0a0472ad6aea
|
|
4
|
+
data.tar.gz: cc8ae626d7cc7861c678c650c4cca4a256e2e35d728eddf8945ac25eb78aaabb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2c04327ac3103a5c5a2498434a6d44b078ec12f6f6d8cef9ad0e425ab644a1a0569d6feabe810f6a3756704ba42d3ead3ae29abbade24423183d6cc6b67291e3
|
|
7
|
+
data.tar.gz: 6a836a104d4110d1423aa2da85a86b8bb4343d2003217a313745ada708e771ed02143f7da9bb1e295012d4c2078e59998bb79d37031f0aa1811a2226dd44dc31
|
data/README.md
CHANGED
|
@@ -1 +1,153 @@
|
|
|
1
|
-
|
|
1
|
+
# Searchkon
|
|
2
|
+
|
|
3
|
+
Searchkon is Advanced active record search(filter) command that makes easy to search throw models and their relationships.
|
|
4
|
+
|
|
5
|
+
## Introduction
|
|
6
|
+
|
|
7
|
+
Lets say we want to return a list of products filtered by multiple parameters. our request contain below parameters:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
{
|
|
11
|
+
filters: {
|
|
12
|
+
title: 'foobar',
|
|
13
|
+
id: [1, 2, 3, 4],
|
|
14
|
+
created_at: '(2012-12-21..2019-12-21)'
|
|
15
|
+
categories.name: 'mobile'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Filter above parameters with Searchkon gem:
|
|
22
|
+
|
|
23
|
+
```rb
|
|
24
|
+
Searchkon::QueryBuilder.filter('Product', filters)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Getting Start
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
Add Searchkon to your Gemfile:
|
|
31
|
+
```sh
|
|
32
|
+
gem 'searchkon'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Searchable columns
|
|
36
|
+
|
|
37
|
+
at the first we should determine witch columns of model can be filter in Searchkon
|
|
38
|
+
|
|
39
|
+
```rb
|
|
40
|
+
class Product < ActiveRecord::Base
|
|
41
|
+
|
|
42
|
+
has_many :coupons
|
|
43
|
+
has_many :payments
|
|
44
|
+
|
|
45
|
+
def self.searchable_columns
|
|
46
|
+
{
|
|
47
|
+
like: ['title', 'coupons.code'],
|
|
48
|
+
exact: [
|
|
49
|
+
'created_at',
|
|
50
|
+
'coupons.title', ## relational filter on coupons table
|
|
51
|
+
'payments.id', ## relational filter on payments table
|
|
52
|
+
'id'
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
<b> like: </b> if you add specific column in your like scope, your query will be like below sql.
|
|
62
|
+
```sql
|
|
63
|
+
select * from products where title like %foo%
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
<b>exact:</b> Searchkon create query using equal operation.
|
|
67
|
+
|
|
68
|
+
```sql
|
|
69
|
+
select * from products where created_at = foo
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Simple where query
|
|
73
|
+
|
|
74
|
+
<b>Important: </b> Searchkon just accept filters key, you can add your filterable columns in filter key.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
```rb
|
|
78
|
+
params = {
|
|
79
|
+
filters: {
|
|
80
|
+
id: 1,
|
|
81
|
+
title: 'foobar'
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```rb
|
|
87
|
+
Searchkon::QueryBuilder.filter('Product', params)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
sql result:
|
|
91
|
+
```sql
|
|
92
|
+
SELECT "products".* FROM "products" WHERE (products.title like '%foobar%') AND "products"."id" = 1
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Search Range
|
|
96
|
+
|
|
97
|
+
```rb
|
|
98
|
+
params = {
|
|
99
|
+
filters: {
|
|
100
|
+
id: '(1..10)',
|
|
101
|
+
created_at: '(2012-12-21..2019-12-21)'
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
sql result:
|
|
107
|
+
|
|
108
|
+
```sql
|
|
109
|
+
SELECT "products".* FROM "products" WHERE (products.title like '%foobar%') AND "products"."id" = 1
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
### Search in relational table
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
```rb
|
|
117
|
+
params = {
|
|
118
|
+
{
|
|
119
|
+
filters: { 'coupons.id': [1,4,8] }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
sql result:
|
|
125
|
+
|
|
126
|
+
```sql
|
|
127
|
+
SELECT "products".* FROM "products" INNER JOIN "coupons" ON "coupons"."product_id" = "products"."id" WHERE "coupons"."id" IN (1, 4, 8)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
### Invalid Column in query
|
|
132
|
+
|
|
133
|
+
if your filter parameters contain invalid column name, Searchkon skip it and create query without that column.
|
|
134
|
+
|
|
135
|
+
```rb
|
|
136
|
+
invalid_mock_params = {
|
|
137
|
+
filters: {
|
|
138
|
+
id: 1,
|
|
139
|
+
foo: 'foobar'
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
```rb
|
|
145
|
+
Searchkon::QueryBuilder.filter('Product', invalid_mock_params)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
sql result:
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
```sql
|
|
152
|
+
SELECT "products".* FROM "products" WHERE "products"."id" = 1
|
|
153
|
+
```
|
|
@@ -7,7 +7,7 @@ module Searchkon
|
|
|
7
7
|
def filter model, params = {}
|
|
8
8
|
@model = model.constantize
|
|
9
9
|
@res = @model.all
|
|
10
|
-
valid_params = validate_params(params
|
|
10
|
+
valid_params = validate_params(params, @model.searchable_columns)
|
|
11
11
|
create_filters valid_params
|
|
12
12
|
@res
|
|
13
13
|
end
|
data/searchkon.gemspec
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
s.name = 'searchkon'
|
|
3
|
-
s.version = '1.0.
|
|
3
|
+
s.version = '1.0.2'
|
|
4
4
|
s.date = '2020-05-24'
|
|
5
5
|
s.summary = "Search Command"
|
|
6
|
-
s.description = "Advanced activerecord Search Command
|
|
6
|
+
s.description = "Advanced activerecord Search Command"
|
|
7
7
|
s.authors = ["Majid Imanzade", 'Amin Samadzade']
|
|
8
8
|
s.email = 'majidimanzade1@gmail.com'
|
|
9
9
|
s.homepage = 'https://rubygems.org/gems/searchkon'
|
data/spec/query_builder_spec.rb
CHANGED
|
@@ -4,28 +4,22 @@ RSpec.describe Searchkon::QueryBuilder do
|
|
|
4
4
|
describe 'query builder for filter' do
|
|
5
5
|
let (:simple_where_params) do
|
|
6
6
|
{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
title: 'foobar'
|
|
10
|
-
}
|
|
7
|
+
id: 1,
|
|
8
|
+
title: 'foobar'
|
|
11
9
|
}
|
|
12
10
|
end
|
|
13
11
|
|
|
14
12
|
let (:simple_range_params) do
|
|
15
13
|
{
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
created_at: '(2012-12-21..2019-12-21)'
|
|
19
|
-
}
|
|
14
|
+
id: '(1..10)',
|
|
15
|
+
created_at: '(2012-12-21..2019-12-21)'
|
|
20
16
|
}
|
|
21
17
|
end
|
|
22
18
|
|
|
23
19
|
let (:range_and_array_where_params) do
|
|
24
20
|
{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
created_at: '(2012-12-21..2019-12-21)'
|
|
28
|
-
}
|
|
21
|
+
id: [1,2,3,4,5],
|
|
22
|
+
created_at: '(2012-12-21..2019-12-21)'
|
|
29
23
|
}
|
|
30
24
|
end
|
|
31
25
|
|
|
@@ -41,21 +35,21 @@ RSpec.describe Searchkon::QueryBuilder do
|
|
|
41
35
|
end
|
|
42
36
|
|
|
43
37
|
it 'should return simple where relational query' do
|
|
44
|
-
relational_params = {
|
|
38
|
+
relational_params = { 'coupons.id': 1 }
|
|
45
39
|
query = "SELECT \"products\".* FROM \"products\" INNER JOIN \"coupons\" ON \"coupons\".\"product_id\" = \"products\".\"id\" WHERE \"coupons\".\"id\" = 1"
|
|
46
40
|
|
|
47
41
|
expect(Searchkon::QueryBuilder.filter('Product', relational_params).to_sql).to eq query
|
|
48
42
|
end
|
|
49
43
|
|
|
50
44
|
it 'should return range relational query' do
|
|
51
|
-
relational_params = {
|
|
45
|
+
relational_params = { 'coupons.id': [1,2,3] }
|
|
52
46
|
query = "SELECT \"products\".* FROM \"products\" INNER JOIN \"coupons\" ON \"coupons\".\"product_id\" = \"products\".\"id\" WHERE \"coupons\".\"id\" IN (1, 2, 3)"
|
|
53
47
|
|
|
54
48
|
expect(Searchkon::QueryBuilder.filter('Product', relational_params).to_sql).to eq query
|
|
55
49
|
end
|
|
56
50
|
|
|
57
51
|
it 'should return simple where relational query with two relations' do
|
|
58
|
-
relational_params = {
|
|
52
|
+
relational_params = { 'coupons.id': 1, 'payments.id': 1 }
|
|
59
53
|
query = "SELECT \"products\".* FROM \"products\" INNER JOIN \"coupons\" ON \"coupons\".\"product_id\" = \"products\".\"id\" INNER JOIN \"payments\" ON \"payments\".\"product_id\" = \"products\".\"id\" WHERE \"coupons\".\"id\" = 1 AND \"payments\".\"id\" = 1"
|
|
60
54
|
expect(Searchkon::QueryBuilder.filter('Product', relational_params).to_sql).to eq query
|
|
61
55
|
end
|
|
@@ -79,10 +73,8 @@ RSpec.describe Searchkon::QueryBuilder do
|
|
|
79
73
|
it 'should return all if all filter column not exist' do
|
|
80
74
|
query = "SELECT \"products\".* FROM \"products\""
|
|
81
75
|
invalid_mock_params = {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
foo: 'foobar'
|
|
85
|
-
}
|
|
76
|
+
blah: 1,
|
|
77
|
+
foo: 'foobar'
|
|
86
78
|
}
|
|
87
79
|
|
|
88
80
|
expect(Searchkon::QueryBuilder.filter('Product', invalid_mock_params).to_sql).to eq query
|
|
@@ -91,10 +83,8 @@ RSpec.describe Searchkon::QueryBuilder do
|
|
|
91
83
|
it 'should return query if one filter column not exist' do
|
|
92
84
|
query = "SELECT \"products\".* FROM \"products\" WHERE \"products\".\"id\" = 1"
|
|
93
85
|
some_invalid_mock_params = {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
foo: 'foobar'
|
|
97
|
-
}
|
|
86
|
+
id: 1,
|
|
87
|
+
foo: 'foobar'
|
|
98
88
|
}
|
|
99
89
|
|
|
100
90
|
expect(Searchkon::QueryBuilder.filter('Product', some_invalid_mock_params).to_sql).to eq query
|
|
@@ -102,10 +92,8 @@ RSpec.describe Searchkon::QueryBuilder do
|
|
|
102
92
|
|
|
103
93
|
it 'should return fulltext query if params has fulltext key' do
|
|
104
94
|
query = "SELECT \"products\".* FROM \"products\" INNER JOIN \"coupons\" ON \"coupons\".\"product_id\" = \"products\".\"id\" WHERE (products.title like '%foobar%' or coupons.code like '%foobar%')"
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
'title,coupons.code': 'foobar'
|
|
108
|
-
}
|
|
95
|
+
params = {
|
|
96
|
+
'title,coupons.code': 'foobar'
|
|
109
97
|
}
|
|
110
98
|
|
|
111
99
|
expect(Searchkon::QueryBuilder.filter('Product', params).to_sql).to eq query
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: searchkon
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Majid Imanzade
|
|
@@ -95,7 +95,7 @@ dependencies:
|
|
|
95
95
|
- - ">="
|
|
96
96
|
- !ruby/object:Gem::Version
|
|
97
97
|
version: '0'
|
|
98
|
-
description: Advanced activerecord Search Command
|
|
98
|
+
description: Advanced activerecord Search Command
|
|
99
99
|
email: majidimanzade1@gmail.com
|
|
100
100
|
executables: []
|
|
101
101
|
extensions: []
|