query_helper 0.2.18 → 0.2.26
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/.github/actions/publish_rubygems.yml +20 -0
- data/.gitignore +1 -0
- data/Gemfile.lock +59 -58
- data/README.md +32 -2
- data/lib/query_helper/column_map.rb +1 -1
- data/lib/query_helper/query_helper_concern.rb +2 -2
- data/lib/query_helper/sql_parser.rb +1 -1
- data/lib/query_helper/sql_sort.rb +59 -29
- data/lib/query_helper/version.rb +1 -1
- data/lib/query_helper.rb +28 -24
- data/query_helper.gemspec +4 -4
- metadata +12 -12
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b424d779be28de32e24c3ec31668d775915e0053ddc7099691b51f48ecb1edf4
|
|
4
|
+
data.tar.gz: 43c360e9e47dd3b5b410632f91e58ded247c9ab20848d43eea510ce4c1ba3665
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8cd9187f8a136f15b5ab1f27cd5ba184531f1976a91a7f72149f1b4e1efb3ba8627ed4a8083a6e71420f45d3bf7c58ffdd05099e4cedc43d94ebbb0875b6fe5d
|
|
7
|
+
data.tar.gz: 4beaf1b6c5dffecfe84bf1e7a7caf174c29defe585e3cd6cf195c2b39e045f1fd6900d1e40119c8e6a61f997a6fb7b80c8c126d18d4f45705ec4ade868237e52
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Publish Gem
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- v*
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v1
|
|
13
|
+
|
|
14
|
+
- name: Release Gem
|
|
15
|
+
if: contains(github.ref, 'refs/tags/v')
|
|
16
|
+
uses: cadwallion/publish-rubygems-action@master
|
|
17
|
+
env:
|
|
18
|
+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
|
19
|
+
RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
|
|
20
|
+
RELEASE_COMMAND: rake release
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,97 +1,98 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
query_helper (0.2.
|
|
4
|
+
query_helper (0.2.26)
|
|
5
5
|
activerecord (> 5)
|
|
6
6
|
activesupport (> 5)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
actionpack (6.
|
|
12
|
-
actionview (= 6.
|
|
13
|
-
activesupport (= 6.
|
|
14
|
-
rack (~> 2.0, >= 2.0.
|
|
11
|
+
actionpack (6.1.4.1)
|
|
12
|
+
actionview (= 6.1.4.1)
|
|
13
|
+
activesupport (= 6.1.4.1)
|
|
14
|
+
rack (~> 2.0, >= 2.0.9)
|
|
15
15
|
rack-test (>= 0.6.3)
|
|
16
16
|
rails-dom-testing (~> 2.0)
|
|
17
17
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
18
|
-
actionview (6.
|
|
19
|
-
activesupport (= 6.
|
|
18
|
+
actionview (6.1.4.1)
|
|
19
|
+
activesupport (= 6.1.4.1)
|
|
20
20
|
builder (~> 3.1)
|
|
21
21
|
erubi (~> 1.4)
|
|
22
22
|
rails-dom-testing (~> 2.0)
|
|
23
23
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
24
|
-
activemodel (6.
|
|
25
|
-
activesupport (= 6.
|
|
26
|
-
activerecord (6.
|
|
27
|
-
activemodel (= 6.
|
|
28
|
-
activesupport (= 6.
|
|
29
|
-
activesupport (6.
|
|
24
|
+
activemodel (6.1.4.1)
|
|
25
|
+
activesupport (= 6.1.4.1)
|
|
26
|
+
activerecord (6.1.4.1)
|
|
27
|
+
activemodel (= 6.1.4.1)
|
|
28
|
+
activesupport (= 6.1.4.1)
|
|
29
|
+
activesupport (6.1.4.1)
|
|
30
30
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
31
|
-
i18n (>=
|
|
32
|
-
minitest (
|
|
33
|
-
tzinfo (~>
|
|
34
|
-
zeitwerk (~> 2.
|
|
31
|
+
i18n (>= 1.6, < 2)
|
|
32
|
+
minitest (>= 5.1)
|
|
33
|
+
tzinfo (~> 2.0)
|
|
34
|
+
zeitwerk (~> 2.3)
|
|
35
35
|
builder (3.2.4)
|
|
36
36
|
byebug (11.1.3)
|
|
37
|
-
concurrent-ruby (1.1.
|
|
37
|
+
concurrent-ruby (1.1.9)
|
|
38
38
|
crass (1.0.6)
|
|
39
39
|
diff-lcs (1.4.4)
|
|
40
|
-
erubi (1.
|
|
40
|
+
erubi (1.10.0)
|
|
41
41
|
faker (1.9.6)
|
|
42
42
|
i18n (>= 0.7)
|
|
43
|
-
i18n (1.8.
|
|
43
|
+
i18n (1.8.10)
|
|
44
44
|
concurrent-ruby (~> 1.0)
|
|
45
|
-
loofah (2.
|
|
45
|
+
loofah (2.12.0)
|
|
46
46
|
crass (~> 1.0.2)
|
|
47
47
|
nokogiri (>= 1.5.9)
|
|
48
48
|
method_source (1.0.0)
|
|
49
|
-
mini_portile2 (2.
|
|
50
|
-
minitest (5.14.
|
|
51
|
-
nokogiri (1.
|
|
52
|
-
mini_portile2 (~> 2.
|
|
49
|
+
mini_portile2 (2.6.1)
|
|
50
|
+
minitest (5.14.4)
|
|
51
|
+
nokogiri (1.12.4)
|
|
52
|
+
mini_portile2 (~> 2.6.1)
|
|
53
|
+
racc (~> 1.4)
|
|
54
|
+
racc (1.5.2)
|
|
53
55
|
rack (2.2.3)
|
|
54
56
|
rack-test (1.1.0)
|
|
55
57
|
rack (>= 1.0, < 3)
|
|
56
58
|
rails-dom-testing (2.0.3)
|
|
57
59
|
activesupport (>= 4.2.0)
|
|
58
60
|
nokogiri (>= 1.6)
|
|
59
|
-
rails-html-sanitizer (1.
|
|
61
|
+
rails-html-sanitizer (1.4.2)
|
|
60
62
|
loofah (~> 2.3)
|
|
61
|
-
railties (6.
|
|
62
|
-
actionpack (= 6.
|
|
63
|
-
activesupport (= 6.
|
|
63
|
+
railties (6.1.4.1)
|
|
64
|
+
actionpack (= 6.1.4.1)
|
|
65
|
+
activesupport (= 6.1.4.1)
|
|
64
66
|
method_source
|
|
65
|
-
rake (>= 0.
|
|
66
|
-
thor (
|
|
67
|
-
rake (13.0.
|
|
68
|
-
rspec (3.
|
|
69
|
-
rspec-core (~> 3.
|
|
70
|
-
rspec-expectations (~> 3.
|
|
71
|
-
rspec-mocks (~> 3.
|
|
72
|
-
rspec-core (3.
|
|
73
|
-
rspec-support (~> 3.
|
|
74
|
-
rspec-expectations (3.
|
|
67
|
+
rake (>= 0.13)
|
|
68
|
+
thor (~> 1.0)
|
|
69
|
+
rake (13.0.6)
|
|
70
|
+
rspec (3.10.0)
|
|
71
|
+
rspec-core (~> 3.10.0)
|
|
72
|
+
rspec-expectations (~> 3.10.0)
|
|
73
|
+
rspec-mocks (~> 3.10.0)
|
|
74
|
+
rspec-core (3.10.1)
|
|
75
|
+
rspec-support (~> 3.10.0)
|
|
76
|
+
rspec-expectations (3.10.1)
|
|
75
77
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
76
|
-
rspec-support (~> 3.
|
|
77
|
-
rspec-mocks (3.
|
|
78
|
+
rspec-support (~> 3.10.0)
|
|
79
|
+
rspec-mocks (3.10.2)
|
|
78
80
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
79
|
-
rspec-support (~> 3.
|
|
80
|
-
rspec-rails (
|
|
81
|
-
actionpack (>=
|
|
82
|
-
activesupport (>=
|
|
83
|
-
railties (>=
|
|
84
|
-
rspec-core (~> 3.
|
|
85
|
-
rspec-expectations (~> 3.
|
|
86
|
-
rspec-mocks (~> 3.
|
|
87
|
-
rspec-support (~> 3.
|
|
88
|
-
rspec-support (3.
|
|
81
|
+
rspec-support (~> 3.10.0)
|
|
82
|
+
rspec-rails (5.0.2)
|
|
83
|
+
actionpack (>= 5.2)
|
|
84
|
+
activesupport (>= 5.2)
|
|
85
|
+
railties (>= 5.2)
|
|
86
|
+
rspec-core (~> 3.10)
|
|
87
|
+
rspec-expectations (~> 3.10)
|
|
88
|
+
rspec-mocks (~> 3.10)
|
|
89
|
+
rspec-support (~> 3.10)
|
|
90
|
+
rspec-support (3.10.2)
|
|
89
91
|
sqlite3 (1.3.13)
|
|
90
|
-
thor (1.0
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
zeitwerk (2.4.0)
|
|
92
|
+
thor (1.1.0)
|
|
93
|
+
tzinfo (2.0.4)
|
|
94
|
+
concurrent-ruby (~> 1.0)
|
|
95
|
+
zeitwerk (2.4.2)
|
|
95
96
|
|
|
96
97
|
PLATFORMS
|
|
97
98
|
ruby
|
|
@@ -99,7 +100,7 @@ PLATFORMS
|
|
|
99
100
|
DEPENDENCIES
|
|
100
101
|
actionpack
|
|
101
102
|
activesupport
|
|
102
|
-
bundler (~>
|
|
103
|
+
bundler (~> 2.2.27)
|
|
103
104
|
byebug
|
|
104
105
|
faker (~> 1.9.3)
|
|
105
106
|
query_helper!
|
|
@@ -109,4 +110,4 @@ DEPENDENCIES
|
|
|
109
110
|
sqlite3 (~> 1.3.6)
|
|
110
111
|
|
|
111
112
|
BUNDLED WITH
|
|
112
|
-
|
|
113
|
+
2.2.27
|
data/README.md
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# QueryHelper
|
|
2
|
-
[](https://travis-ci.org/iserve-products/query_helper)
|
|
3
2
|
[](https://badge.fury.io/rb/query_helper)
|
|
4
3
|
|
|
5
4
|
QueryHelper is a ruby gem used to paginate, sort, and filter your API calls in Ruby on Rails using URL params in your HTTP requests. It currently only supports Postgres.
|
|
@@ -88,6 +87,37 @@ Multiple Sorts: `http://www.example.com/resources?sort=resource_name:desc,resour
|
|
|
88
87
|
|
|
89
88
|
Lowercase Sort: `http://www.example.com/resources?sort=resource_name:desc:lowercase`
|
|
90
89
|
|
|
90
|
+
Custom Sort: `http://www.example.com/resources?custom_sort=resource_name:desc`
|
|
91
|
+
Example:
|
|
92
|
+
Custom Sort is basically used for enum based column.
|
|
93
|
+
```
|
|
94
|
+
class Customer < ApplicationRecord
|
|
95
|
+
enum customer_type: {
|
|
96
|
+
enum1: 0,
|
|
97
|
+
enum2: 1,
|
|
98
|
+
enum3: 3
|
|
99
|
+
}
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Usage at Controller
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
class SomeController
|
|
107
|
+
|
|
108
|
+
def index
|
|
109
|
+
sort_column, sort_direction = params[:custom_sort]&.split(':')
|
|
110
|
+
|
|
111
|
+
column_sort_order = {
|
|
112
|
+
column_name: sort_column,
|
|
113
|
+
direction: sort_direction,
|
|
114
|
+
sort_values: Customer.send(sort_column.pluralize).values
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@query_helper.update(query: query, column_sort_order: column_sort_order)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
```
|
|
91
121
|
#### Filtering
|
|
92
122
|
|
|
93
123
|
`filter[column][operator_code]=value`
|
|
@@ -316,7 +346,7 @@ or
|
|
|
316
346
|
|
|
317
347
|
## Contributing
|
|
318
348
|
|
|
319
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
349
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/patterninc/query_helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
320
350
|
|
|
321
351
|
## License
|
|
322
352
|
|
|
@@ -13,7 +13,7 @@ class QueryHelper
|
|
|
13
13
|
|
|
14
14
|
model.attribute_names.each do |attribute|
|
|
15
15
|
if maps.select{|x| x.alias_name == attribute}.empty?
|
|
16
|
-
maps << ColumnMap.new(alias_name: attribute, sql_expression: "#{model.
|
|
16
|
+
maps << ColumnMap.new(alias_name: attribute, sql_expression: "#{model.table_name.pluralize}.#{attribute}")
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
@@ -31,7 +31,7 @@ class QueryHelper
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def create_query_helper_sort
|
|
34
|
-
QueryHelper::SqlSort.new(sort_string: params[:sort])
|
|
34
|
+
QueryHelper::SqlSort.new(sort_string: params[:sort], sort_tiebreak: params[:sort_tiebreak], column_sort_order: params[:column_sort_order])
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def create_query_helper_associations
|
|
@@ -48,7 +48,7 @@ class QueryHelper
|
|
|
48
48
|
def query_helper_params_no_pagination
|
|
49
49
|
helpers = {}
|
|
50
50
|
helpers[:sql_filter] = create_query_helper_filter() if params[:filter]
|
|
51
|
-
helpers[:sql_sort] = create_query_helper_sort() if params[:sort]
|
|
51
|
+
helpers[:sql_sort] = create_query_helper_sort() if params[:sort] || params[:sort_tiebreak] || params[:custom_sort]
|
|
52
52
|
helpers[:associations] = create_query_helper_associations() if params[:include]
|
|
53
53
|
helpers[:search_string] = params[:search_for] if params[:search_for]
|
|
54
54
|
helpers
|
|
@@ -173,7 +173,7 @@ class QueryHelper
|
|
|
173
173
|
ColumnMap.new(
|
|
174
174
|
alias_name: sql_alias,
|
|
175
175
|
sql_expression: sql_expression.squish,
|
|
176
|
-
aggregate: /(array_agg|avg|bit_and|bit_or|bool_and|bool_or|count|every|json_agg|jsonb_agg|json_object_agg|jsonb_object_agg|max|min|string_agg|sum|xmlagg)\((.*)\)/.match?(sql_expression)
|
|
176
|
+
aggregate: /(array_agg|avg|bit_and|bit_or|bool_and|bool_or|boolor_agg|booland_agg|count|every|json_agg|jsonb_agg|json_object_agg|jsonb_object_agg|max|min|string_agg|sum|xmlagg)\((.*)\)/.match?(sql_expression)
|
|
177
177
|
) if sql_alias
|
|
178
178
|
end
|
|
179
179
|
column_maps.compact
|
|
@@ -3,50 +3,80 @@ require "query_helper/invalid_query_error"
|
|
|
3
3
|
class QueryHelper
|
|
4
4
|
class SqlSort
|
|
5
5
|
|
|
6
|
-
attr_accessor :column_maps, :select_strings
|
|
6
|
+
attr_accessor :column_maps, :select_strings, :sort_tiebreak, :column_sort_order
|
|
7
7
|
|
|
8
|
-
def initialize(sort_string: "", column_maps: [])
|
|
8
|
+
def initialize(sort_string: "", sort_tiebreak: "", column_maps: [], column_sort_order: {})
|
|
9
9
|
@sort_string = sort_string
|
|
10
10
|
@column_maps = column_maps
|
|
11
|
+
@sort_tiebreak = sort_tiebreak
|
|
12
|
+
@column_sort_order = column_sort_order
|
|
11
13
|
@select_strings = []
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
def parse_sort_string
|
|
17
|
+
return [] if @sort_string.blank? && @sort_tiebreak.blank? && @column_sort_order.blank?
|
|
18
|
+
|
|
19
|
+
return attributes_sql_expression(@sort_tiebreak) if @sort_string.blank?
|
|
20
|
+
|
|
21
|
+
sql_strings = attributes_sql_expression(@sort_string)
|
|
22
|
+
return sql_strings if @sort_tiebreak.blank?
|
|
23
|
+
|
|
24
|
+
sql_strings + attributes_sql_expression(@sort_tiebreak)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def attributes_sql_expression(sort_attribute)
|
|
15
28
|
sql_strings = []
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
if sort_attribute.present?
|
|
30
|
+
sorts = sort_attribute.split(",")
|
|
31
|
+
sorts.each_with_index do |sort, index|
|
|
32
|
+
sort_alias = sort.split(":")[0]
|
|
33
|
+
direction = sort.split(":")[1]
|
|
34
|
+
modifier = sort.split(":")[2]
|
|
35
|
+
begin
|
|
36
|
+
sql_expression = @column_maps.find{ |m| m.alias_name.casecmp?(sort_alias) }.sql_expression
|
|
37
|
+
rescue NoMethodError => e
|
|
38
|
+
raise InvalidQueryError.new("Sorting not allowed on column '#{sort_alias}'")
|
|
39
|
+
end
|
|
27
40
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
41
|
+
if direction == "desc"
|
|
42
|
+
case ActiveRecord::Base.connection.adapter_name
|
|
43
|
+
when "SQLite" # SQLite is used in the test suite
|
|
44
|
+
direction = "desc"
|
|
45
|
+
else
|
|
46
|
+
direction = "desc nulls last"
|
|
47
|
+
end
|
|
32
48
|
else
|
|
33
|
-
direction = "
|
|
49
|
+
direction = "asc"
|
|
34
50
|
end
|
|
35
|
-
else
|
|
36
|
-
direction = "asc"
|
|
37
|
-
end
|
|
38
51
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
case modifier
|
|
53
|
+
when "lowercase"
|
|
54
|
+
sql_expression = "lower(#{sql_expression})"
|
|
55
|
+
# When select distincts are used, the order by clause must be included in the select clause
|
|
56
|
+
@select_strings << sql_expression
|
|
57
|
+
end
|
|
45
58
|
|
|
46
|
-
|
|
59
|
+
sql_strings << "#{sql_expression} #{direction}"
|
|
60
|
+
end
|
|
47
61
|
end
|
|
62
|
+
sql_strings << parse_custom_sort_string
|
|
63
|
+
sql_strings
|
|
64
|
+
end
|
|
48
65
|
|
|
49
|
-
|
|
66
|
+
# This method is used for sorting enum based column
|
|
67
|
+
def parse_custom_sort_string
|
|
68
|
+
return '' if @column_sort_order.blank? || @column_sort_order[:sort_values].blank?
|
|
69
|
+
|
|
70
|
+
sort_column = @column_sort_order[:column_name]
|
|
71
|
+
sort_values = @column_sort_order[:sort_values]
|
|
72
|
+
direction = @column_sort_order[:direction]
|
|
73
|
+
|
|
74
|
+
sql_expression = '(CASE'
|
|
75
|
+
sort_values.each_with_index do |value, index|
|
|
76
|
+
sql_expression << " WHEN #{sort_column}=#{value} THEN #{index}"
|
|
77
|
+
end
|
|
78
|
+
sql_expression << " END) #{direction}"
|
|
79
|
+
sql_expression
|
|
50
80
|
end
|
|
51
81
|
end
|
|
52
82
|
end
|
data/lib/query_helper/version.rb
CHANGED
data/lib/query_helper.rb
CHANGED
|
@@ -66,6 +66,8 @@ class QueryHelper
|
|
|
66
66
|
search_fields: nil,
|
|
67
67
|
sql_filter: nil,
|
|
68
68
|
sql_sort: nil,
|
|
69
|
+
sort_tiebreak: nil,
|
|
70
|
+
column_sort_order: nil,
|
|
69
71
|
page: nil,
|
|
70
72
|
per_page: nil,
|
|
71
73
|
search_string: nil,
|
|
@@ -80,13 +82,15 @@ class QueryHelper
|
|
|
80
82
|
@as_json_options = as_json_options if as_json_options
|
|
81
83
|
@custom_mappings = custom_mappings if custom_mappings
|
|
82
84
|
@preload = preload if preload
|
|
83
|
-
@search_fields = search_fields if search_fields
|
|
84
|
-
@sql_filter = sql_filter if sql_filter
|
|
85
|
-
@sql_sort = sql_sort if sql_sort
|
|
86
|
-
@
|
|
85
|
+
@search_fields = search_fields if search_fields
|
|
86
|
+
@sql_filter = sql_filter if sql_filter
|
|
87
|
+
@sql_sort = sql_sort if sql_sort
|
|
88
|
+
@sql_sort.sort_tiebreak = sort_tiebreak if sort_tiebreak
|
|
89
|
+
@sql_sort.column_sort_order = column_sort_order if column_sort_order
|
|
90
|
+
@search_string = search_string if search_string
|
|
87
91
|
@page = determine_page(page: page, per_page: per_page) if page
|
|
88
92
|
@per_page = determine_per_page(page: page, per_page: per_page) if per_page
|
|
89
|
-
@metadata = metadata if metadata
|
|
93
|
+
@metadata = metadata if metadata
|
|
90
94
|
set_limit_and_offset()
|
|
91
95
|
return self
|
|
92
96
|
end
|
|
@@ -116,21 +120,21 @@ class QueryHelper
|
|
|
116
120
|
@sql_filter.create_filters()
|
|
117
121
|
|
|
118
122
|
having_clauses = @sql_filter.having_clauses
|
|
119
|
-
where_clauses = @sql_filter.where_clauses
|
|
120
|
-
|
|
123
|
+
where_clauses = @sql_filter.where_clauses
|
|
124
|
+
|
|
121
125
|
if @search_string
|
|
122
126
|
search_filter = search_filter(column_maps)
|
|
123
127
|
if search_filter[:placement] == :where
|
|
124
128
|
where_clauses << search_filter[:filter]
|
|
125
|
-
else
|
|
129
|
+
else
|
|
126
130
|
having_clauses << search_filter[:filter]
|
|
127
131
|
end
|
|
128
|
-
end
|
|
132
|
+
end
|
|
129
133
|
|
|
130
134
|
|
|
131
135
|
# merge the filter bind variables into the query bind variables
|
|
132
136
|
@bind_variables.merge!(@sql_filter.bind_variables)
|
|
133
|
-
|
|
137
|
+
|
|
134
138
|
# Execute Sql Query
|
|
135
139
|
manipulator = SqlManipulator.new(
|
|
136
140
|
sql: @query,
|
|
@@ -152,7 +156,7 @@ class QueryHelper
|
|
|
152
156
|
return query if @bind_variables.length == 0
|
|
153
157
|
begin
|
|
154
158
|
return @model.sanitize_sql_array([query, @bind_variables])
|
|
155
|
-
rescue NoMethodError
|
|
159
|
+
rescue NoMethodError
|
|
156
160
|
# sanitize_sql_array is a protected method before Rails v5.2.3
|
|
157
161
|
return @model.send(:sanitize_sql_array, [query, @bind_variables])
|
|
158
162
|
end
|
|
@@ -181,8 +185,8 @@ class QueryHelper
|
|
|
181
185
|
|
|
182
186
|
def pagination_results(count=@count)
|
|
183
187
|
# Set pagination params if they aren't provided
|
|
184
|
-
results_per_page = @per_page || count
|
|
185
|
-
results_page = @page || 1
|
|
188
|
+
results_per_page = @per_page || count
|
|
189
|
+
results_page = @page || 1
|
|
186
190
|
|
|
187
191
|
total_pages = (count.to_i/(results_per_page.nonzero? || 1).to_f).ceil
|
|
188
192
|
next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
|
|
@@ -205,16 +209,16 @@ class QueryHelper
|
|
|
205
209
|
private
|
|
206
210
|
|
|
207
211
|
def determine_page(page:, per_page:)
|
|
208
|
-
return page.to_i if page
|
|
209
|
-
return 1 if !page && per_page
|
|
212
|
+
return page.to_i if page
|
|
213
|
+
return 1 if !page && per_page
|
|
210
214
|
return nil
|
|
211
|
-
end
|
|
215
|
+
end
|
|
212
216
|
|
|
213
217
|
def determine_per_page(page:, per_page:)
|
|
214
|
-
return per_page.to_i if per_page
|
|
215
|
-
return 100 if !per_page && page
|
|
218
|
+
return per_page.to_i if per_page
|
|
219
|
+
return 100 if !per_page && page
|
|
216
220
|
return nil
|
|
217
|
-
end
|
|
221
|
+
end
|
|
218
222
|
|
|
219
223
|
def set_limit_and_offset
|
|
220
224
|
if @page && @per_page
|
|
@@ -226,7 +230,7 @@ class QueryHelper
|
|
|
226
230
|
@bind_variables[:limit] = limit
|
|
227
231
|
@bind_variables[:offset] = offset
|
|
228
232
|
end
|
|
229
|
-
end
|
|
233
|
+
end
|
|
230
234
|
|
|
231
235
|
def paginated_results
|
|
232
236
|
{ pagination: pagination_results(),
|
|
@@ -273,16 +277,16 @@ class QueryHelper
|
|
|
273
277
|
def search_filter(column_maps)
|
|
274
278
|
raise ArgumentError.new("search_fields not defined") unless @search_fields.length > 0
|
|
275
279
|
placement = :where
|
|
276
|
-
maps = column_maps.select do |cm|
|
|
280
|
+
maps = column_maps.select do |cm|
|
|
277
281
|
placement = :having if cm.aggregate
|
|
278
282
|
@search_fields.include? cm.alias_name
|
|
279
|
-
end
|
|
283
|
+
end
|
|
280
284
|
bind_variable = ('a'..'z').to_a.shuffle[0,20].join.to_sym
|
|
281
285
|
@bind_variables[bind_variable] = "%#{@search_string}%"
|
|
282
|
-
filter = "#{maps.map{|m| "#{m.sql_expression}::varchar"}.join(" || ")} ilike :#{bind_variable}"
|
|
286
|
+
filter = "#{maps.map{|m| "coalesce(#{m.sql_expression}::varchar, '')"}.join(" || ")} ilike :#{bind_variable}"
|
|
283
287
|
return {
|
|
284
288
|
filter: filter,
|
|
285
289
|
placement: placement
|
|
286
290
|
}
|
|
287
|
-
end
|
|
291
|
+
end
|
|
288
292
|
end
|
data/query_helper.gemspec
CHANGED
|
@@ -6,12 +6,12 @@ require "query_helper/version"
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "query_helper"
|
|
8
8
|
spec.version = QueryHelper::VERSION
|
|
9
|
-
spec.authors = ["
|
|
10
|
-
spec.email = ["
|
|
9
|
+
spec.authors = ["Patterninc"]
|
|
10
|
+
spec.email = ["tech@pattern.com"]
|
|
11
11
|
|
|
12
12
|
spec.summary = %q{Ruby Gem to help with pagination and data formatting at Pattern, Inc.}
|
|
13
13
|
spec.description = %q{Ruby gem developed to help with pagination, filtering, sorting, and including associations on both active record queries and custom sql queries}
|
|
14
|
-
spec.homepage = "https://github.com/
|
|
14
|
+
spec.homepage = "https://github.com/patterninc/query_helper"
|
|
15
15
|
spec.license = "MIT"
|
|
16
16
|
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
|
|
|
36
36
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
37
37
|
spec.require_paths = ["lib"]
|
|
38
38
|
|
|
39
|
-
spec.add_development_dependency "bundler", "~>
|
|
39
|
+
spec.add_development_dependency "bundler", "~> 2.2.27"
|
|
40
40
|
spec.add_development_dependency "rake", "~> 13.0"
|
|
41
41
|
spec.add_development_dependency "rspec", "~> 3.0"
|
|
42
42
|
spec.add_development_dependency "sqlite3", "~> 1.3.6"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: query_helper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.26
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
8
|
-
autorequire:
|
|
7
|
+
- Patterninc
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-02-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 2.2.27
|
|
20
20
|
type: :development
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
26
|
+
version: 2.2.27
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: rake
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -167,14 +167,14 @@ dependencies:
|
|
|
167
167
|
description: Ruby gem developed to help with pagination, filtering, sorting, and including
|
|
168
168
|
associations on both active record queries and custom sql queries
|
|
169
169
|
email:
|
|
170
|
-
-
|
|
170
|
+
- tech@pattern.com
|
|
171
171
|
executables: []
|
|
172
172
|
extensions: []
|
|
173
173
|
extra_rdoc_files: []
|
|
174
174
|
files:
|
|
175
|
+
- ".github/actions/publish_rubygems.yml"
|
|
175
176
|
- ".gitignore"
|
|
176
177
|
- ".rspec"
|
|
177
|
-
- ".travis.yml"
|
|
178
178
|
- CODE_OF_CONDUCT.md
|
|
179
179
|
- Gemfile
|
|
180
180
|
- Gemfile.lock
|
|
@@ -195,11 +195,11 @@ files:
|
|
|
195
195
|
- lib/query_helper/sql_sort.rb
|
|
196
196
|
- lib/query_helper/version.rb
|
|
197
197
|
- query_helper.gemspec
|
|
198
|
-
homepage: https://github.com/
|
|
198
|
+
homepage: https://github.com/patterninc/query_helper
|
|
199
199
|
licenses:
|
|
200
200
|
- MIT
|
|
201
201
|
metadata: {}
|
|
202
|
-
post_install_message:
|
|
202
|
+
post_install_message:
|
|
203
203
|
rdoc_options: []
|
|
204
204
|
require_paths:
|
|
205
205
|
- lib
|
|
@@ -214,8 +214,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
214
214
|
- !ruby/object:Gem::Version
|
|
215
215
|
version: '0'
|
|
216
216
|
requirements: []
|
|
217
|
-
rubygems_version: 3.
|
|
218
|
-
signing_key:
|
|
217
|
+
rubygems_version: 3.2.26
|
|
218
|
+
signing_key:
|
|
219
219
|
specification_version: 4
|
|
220
220
|
summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.
|
|
221
221
|
test_files: []
|