query_helper 0.2.21 → 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 +1 -1
- data/README.md +32 -2
- 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 +48 -28
- data/lib/query_helper/version.rb +1 -1
- data/lib/query_helper.rb +2 -0
- data/query_helper.gemspec +3 -3
- metadata +7 -7
- 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
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
|
|
@@ -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], sort_tiebreak: params[:sort_tiebreak])
|
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] || params[:sort_tiebreak]
|
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,17 +3,18 @@ require "query_helper/invalid_query_error"
|
|
3
3
|
class QueryHelper
|
4
4
|
class SqlSort
|
5
5
|
|
6
|
-
attr_accessor :column_maps, :select_strings, :sort_tiebreak
|
6
|
+
attr_accessor :column_maps, :select_strings, :sort_tiebreak, :column_sort_order
|
7
7
|
|
8
|
-
def initialize(sort_string: "", sort_tiebreak: "", 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
11
|
@sort_tiebreak = sort_tiebreak
|
12
|
+
@column_sort_order = column_sort_order
|
12
13
|
@select_strings = []
|
13
14
|
end
|
14
15
|
|
15
16
|
def parse_sort_string
|
16
|
-
return [] if @sort_string.blank? && @sort_tiebreak.blank?
|
17
|
+
return [] if @sort_string.blank? && @sort_tiebreak.blank? && @column_sort_order.blank?
|
17
18
|
|
18
19
|
return attributes_sql_expression(@sort_tiebreak) if @sort_string.blank?
|
19
20
|
|
@@ -25,38 +26,57 @@ class QueryHelper
|
|
25
26
|
|
26
27
|
def attributes_sql_expression(sort_attribute)
|
27
28
|
sql_strings = []
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
43
48
|
else
|
44
|
-
direction = "
|
49
|
+
direction = "asc"
|
45
50
|
end
|
46
|
-
else
|
47
|
-
direction = "asc"
|
48
|
-
end
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
56
58
|
|
57
|
-
|
59
|
+
sql_strings << "#{sql_expression} #{direction}"
|
60
|
+
end
|
58
61
|
end
|
62
|
+
sql_strings << parse_custom_sort_string
|
59
63
|
sql_strings
|
60
64
|
end
|
65
|
+
|
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
|
80
|
+
end
|
61
81
|
end
|
62
82
|
end
|
data/lib/query_helper/version.rb
CHANGED
data/lib/query_helper.rb
CHANGED
@@ -67,6 +67,7 @@ class QueryHelper
|
|
67
67
|
sql_filter: nil,
|
68
68
|
sql_sort: nil,
|
69
69
|
sort_tiebreak: nil,
|
70
|
+
column_sort_order: nil,
|
70
71
|
page: nil,
|
71
72
|
per_page: nil,
|
72
73
|
search_string: nil,
|
@@ -85,6 +86,7 @@ class QueryHelper
|
|
85
86
|
@sql_filter = sql_filter if sql_filter
|
86
87
|
@sql_sort = sql_sort if sql_sort
|
87
88
|
@sql_sort.sort_tiebreak = sort_tiebreak if sort_tiebreak
|
89
|
+
@sql_sort.column_sort_order = column_sort_order if column_sort_order
|
88
90
|
@search_string = search_string if search_string
|
89
91
|
@page = determine_page(page: page, per_page: per_page) if page
|
90
92
|
@per_page = determine_per_page(page: page, per_page: per_page) if per_page
|
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'
|
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
|
-
-
|
7
|
+
- Patterninc
|
8
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
|
@@ -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,7 +195,7 @@ 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: {}
|
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
214
|
- !ruby/object:Gem::Version
|
215
215
|
version: '0'
|
216
216
|
requirements: []
|
217
|
-
rubygems_version: 3.
|
217
|
+
rubygems_version: 3.2.26
|
218
218
|
signing_key:
|
219
219
|
specification_version: 4
|
220
220
|
summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.
|