query_helper 0.2.11 → 0.2.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +30 -30
- data/lib/query_helper.rb +33 -28
- data/lib/query_helper/sql_manipulator.rb +1 -1
- data/lib/query_helper/version.rb +1 -1
- 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: ec3e2ffc3d82e55e6e2898c06071e3dc08111df35b70c0b51f430d6f0b85c8b4
|
4
|
+
data.tar.gz: 13e9d510e2c5cee31a8ac64b089d9822402742d2a31f3e34bf51dd2db0db1ae7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 121e0ec7dfab835a7aa77d368d82bfc373d8b13c8a7483c4950a9f9995c676d25c51bfd29eb9a48b0781f1ab12fabe10334612d766188fd568581f209b509df7
|
7
|
+
data.tar.gz: 6f03e2fa58fc313e657f8d0ff868e9c6bfe668713202e227451369f2b47b99e4d3b0015113ca1f7cd4add40d20bc6fccc9833fd60204784d89d1c22c2c9a01c5
|
data/Gemfile.lock
CHANGED
@@ -1,56 +1,56 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
query_helper (0.2.
|
4
|
+
query_helper (0.2.16)
|
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.0.
|
12
|
-
actionview (= 6.0.
|
13
|
-
activesupport (= 6.0.
|
11
|
+
actionpack (6.0.3.2)
|
12
|
+
actionview (= 6.0.3.2)
|
13
|
+
activesupport (= 6.0.3.2)
|
14
14
|
rack (~> 2.0, >= 2.0.8)
|
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.0.
|
19
|
-
activesupport (= 6.0.
|
18
|
+
actionview (6.0.3.2)
|
19
|
+
activesupport (= 6.0.3.2)
|
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.0.
|
25
|
-
activesupport (= 6.0.
|
26
|
-
activerecord (6.0.
|
27
|
-
activemodel (= 6.0.
|
28
|
-
activesupport (= 6.0.
|
29
|
-
activesupport (6.0.
|
24
|
+
activemodel (6.0.3.2)
|
25
|
+
activesupport (= 6.0.3.2)
|
26
|
+
activerecord (6.0.3.2)
|
27
|
+
activemodel (= 6.0.3.2)
|
28
|
+
activesupport (= 6.0.3.2)
|
29
|
+
activesupport (6.0.3.2)
|
30
30
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
31
|
i18n (>= 0.7, < 2)
|
32
32
|
minitest (~> 5.1)
|
33
33
|
tzinfo (~> 1.1)
|
34
|
-
zeitwerk (~> 2.2)
|
34
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
35
35
|
builder (3.2.4)
|
36
|
-
byebug (11.1.
|
36
|
+
byebug (11.1.3)
|
37
37
|
concurrent-ruby (1.1.6)
|
38
38
|
crass (1.0.6)
|
39
|
-
diff-lcs (1.
|
39
|
+
diff-lcs (1.4.4)
|
40
40
|
erubi (1.9.0)
|
41
41
|
faker (1.9.6)
|
42
42
|
i18n (>= 0.7)
|
43
|
-
i18n (1.8.
|
43
|
+
i18n (1.8.5)
|
44
44
|
concurrent-ruby (~> 1.0)
|
45
|
-
loofah (2.
|
45
|
+
loofah (2.6.0)
|
46
46
|
crass (~> 1.0.2)
|
47
47
|
nokogiri (>= 1.5.9)
|
48
48
|
method_source (1.0.0)
|
49
49
|
mini_portile2 (2.4.0)
|
50
|
-
minitest (5.14.
|
51
|
-
nokogiri (1.10.
|
50
|
+
minitest (5.14.1)
|
51
|
+
nokogiri (1.10.10)
|
52
52
|
mini_portile2 (~> 2.4.0)
|
53
|
-
rack (2.2.
|
53
|
+
rack (2.2.3)
|
54
54
|
rack-test (1.1.0)
|
55
55
|
rack (>= 1.0, < 3)
|
56
56
|
rails-dom-testing (2.0.3)
|
@@ -58,9 +58,9 @@ GEM
|
|
58
58
|
nokogiri (>= 1.6)
|
59
59
|
rails-html-sanitizer (1.3.0)
|
60
60
|
loofah (~> 2.3)
|
61
|
-
railties (6.0.
|
62
|
-
actionpack (= 6.0.
|
63
|
-
activesupport (= 6.0.
|
61
|
+
railties (6.0.3.2)
|
62
|
+
actionpack (= 6.0.3.2)
|
63
|
+
activesupport (= 6.0.3.2)
|
64
64
|
method_source
|
65
65
|
rake (>= 0.8.7)
|
66
66
|
thor (>= 0.20.3, < 2.0)
|
@@ -69,15 +69,15 @@ GEM
|
|
69
69
|
rspec-core (~> 3.9.0)
|
70
70
|
rspec-expectations (~> 3.9.0)
|
71
71
|
rspec-mocks (~> 3.9.0)
|
72
|
-
rspec-core (3.9.
|
73
|
-
rspec-support (~> 3.9.
|
74
|
-
rspec-expectations (3.9.
|
72
|
+
rspec-core (3.9.2)
|
73
|
+
rspec-support (~> 3.9.3)
|
74
|
+
rspec-expectations (3.9.2)
|
75
75
|
diff-lcs (>= 1.2.0, < 2.0)
|
76
76
|
rspec-support (~> 3.9.0)
|
77
77
|
rspec-mocks (3.9.1)
|
78
78
|
diff-lcs (>= 1.2.0, < 2.0)
|
79
79
|
rspec-support (~> 3.9.0)
|
80
|
-
rspec-rails (4.0.
|
80
|
+
rspec-rails (4.0.1)
|
81
81
|
actionpack (>= 4.2)
|
82
82
|
activesupport (>= 4.2)
|
83
83
|
railties (>= 4.2)
|
@@ -85,13 +85,13 @@ GEM
|
|
85
85
|
rspec-expectations (~> 3.9)
|
86
86
|
rspec-mocks (~> 3.9)
|
87
87
|
rspec-support (~> 3.9)
|
88
|
-
rspec-support (3.9.
|
88
|
+
rspec-support (3.9.3)
|
89
89
|
sqlite3 (1.3.13)
|
90
90
|
thor (1.0.1)
|
91
91
|
thread_safe (0.3.6)
|
92
|
-
tzinfo (1.2.
|
92
|
+
tzinfo (1.2.7)
|
93
93
|
thread_safe (~> 0.1)
|
94
|
-
zeitwerk (2.
|
94
|
+
zeitwerk (2.4.0)
|
95
95
|
|
96
96
|
PLATFORMS
|
97
97
|
ruby
|
data/lib/query_helper.rb
CHANGED
@@ -13,7 +13,7 @@ require "query_helper/invalid_query_error"
|
|
13
13
|
|
14
14
|
class QueryHelper
|
15
15
|
|
16
|
-
attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload, :search_field, :search_string
|
16
|
+
attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload, :search_field, :search_string, :metadata
|
17
17
|
attr_reader :query
|
18
18
|
|
19
19
|
def initialize(
|
@@ -31,7 +31,8 @@ class QueryHelper
|
|
31
31
|
api_payload: false, # Return the paginated payload or simply return the result array
|
32
32
|
preload: [], # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
|
33
33
|
search_fields: [],
|
34
|
-
search_string: nil
|
34
|
+
search_string: nil,
|
35
|
+
metadata: {}
|
35
36
|
)
|
36
37
|
@query = query.class < ActiveRecord::Relation ? query.to_sql : query
|
37
38
|
@model = query.class < ActiveRecord::Relation ? query.base_class : model
|
@@ -49,6 +50,7 @@ class QueryHelper
|
|
49
50
|
@preload = preload
|
50
51
|
@search_fields = search_fields
|
51
52
|
@search_string = search_string
|
53
|
+
@metadata = metadata
|
52
54
|
end
|
53
55
|
|
54
56
|
def update(
|
@@ -66,7 +68,8 @@ class QueryHelper
|
|
66
68
|
sql_sort: nil,
|
67
69
|
page: nil,
|
68
70
|
per_page: nil,
|
69
|
-
search_string: nil
|
71
|
+
search_string: nil,
|
72
|
+
metadata: nil
|
70
73
|
)
|
71
74
|
@query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
|
72
75
|
@model = query.class < ActiveRecord::Relation ? query.base_class : model if model || query
|
@@ -83,6 +86,7 @@ class QueryHelper
|
|
83
86
|
@search_string = search_string if search_string
|
84
87
|
@page = determine_page(page: page, per_page: per_page) if page
|
85
88
|
@per_page = determine_per_page(page: page, per_page: per_page) if per_page
|
89
|
+
@metadata = metadata if metadata
|
86
90
|
set_limit_and_offset()
|
87
91
|
return self
|
88
92
|
end
|
@@ -140,7 +144,7 @@ class QueryHelper
|
|
140
144
|
end
|
141
145
|
|
142
146
|
def to_json(args)
|
143
|
-
|
147
|
+
results.to_json
|
144
148
|
end
|
145
149
|
|
146
150
|
def to_sql
|
@@ -175,6 +179,29 @@ class QueryHelper
|
|
175
179
|
return @results
|
176
180
|
end
|
177
181
|
|
182
|
+
def pagination_results(count=@count)
|
183
|
+
# Set pagination params if they aren't provided
|
184
|
+
results_per_page = @per_page || count
|
185
|
+
results_page = @page || 1
|
186
|
+
|
187
|
+
total_pages = (count/(results_per_page.nonzero? || 1).to_f).ceil
|
188
|
+
next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
|
189
|
+
previous_page = results_page - 1 if results_page.between?(2, total_pages)
|
190
|
+
first_page = results_page == 1
|
191
|
+
last_page = results_page == total_pages
|
192
|
+
out_of_range = !results_page.between?(1,total_pages)
|
193
|
+
|
194
|
+
{ count: count,
|
195
|
+
current_page: results_page,
|
196
|
+
next_page: next_page,
|
197
|
+
previous_page: previous_page,
|
198
|
+
total_pages: total_pages,
|
199
|
+
per_page: results_per_page,
|
200
|
+
first_page: first_page,
|
201
|
+
last_page: last_page,
|
202
|
+
out_of_range: out_of_range }
|
203
|
+
end
|
204
|
+
|
178
205
|
private
|
179
206
|
|
180
207
|
def determine_page(page:, per_page:)
|
@@ -203,7 +230,8 @@ class QueryHelper
|
|
203
230
|
|
204
231
|
def paginated_results
|
205
232
|
{ pagination: pagination_results(),
|
206
|
-
data: @results
|
233
|
+
data: @results,
|
234
|
+
metadata: @metadata }
|
207
235
|
end
|
208
236
|
|
209
237
|
def determine_count
|
@@ -234,29 +262,6 @@ class QueryHelper
|
|
234
262
|
@results.map!{ |r| r.except("_query_full_count") } if @page && @per_page && !@single_record
|
235
263
|
end
|
236
264
|
|
237
|
-
def pagination_results
|
238
|
-
# Set pagination params if they aren't provided
|
239
|
-
results_per_page = @per_page || @count
|
240
|
-
results_page = @page || 1
|
241
|
-
|
242
|
-
total_pages = (@count/(results_per_page.nonzero? || 1).to_f).ceil
|
243
|
-
next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
|
244
|
-
previous_page = results_page - 1 if results_page.between?(2, total_pages)
|
245
|
-
first_page = results_page == 1
|
246
|
-
last_page = results_page == total_pages
|
247
|
-
out_of_range = !results_page.between?(1,total_pages)
|
248
|
-
|
249
|
-
{ count: @count,
|
250
|
-
current_page: results_page,
|
251
|
-
next_page: next_page,
|
252
|
-
previous_page: previous_page,
|
253
|
-
total_pages: total_pages,
|
254
|
-
per_page: results_per_page,
|
255
|
-
first_page: first_page,
|
256
|
-
last_page: last_page,
|
257
|
-
out_of_range: out_of_range }
|
258
|
-
end
|
259
|
-
|
260
265
|
def create_column_maps
|
261
266
|
ColumnMap.create_column_mappings(
|
262
267
|
query: @query,
|
@@ -55,7 +55,7 @@ class QueryHelper
|
|
55
55
|
|
56
56
|
def insert_order_by_and_limit_clause
|
57
57
|
@sql.slice!(@parser.limit_clause) if @parser.limit_included? # remove existing limit clause
|
58
|
-
@sql.slice!(@parser.order_by_clause) if @parser.order_by_included? # remove existing order by clause
|
58
|
+
@sql.slice!(@parser.order_by_clause) if @parser.order_by_included? && @order_by_clauses.length > 0 # remove existing order by clause
|
59
59
|
@sql += " order by #{@order_by_clauses.join(", ")} " if @order_by_clauses.length > 0
|
60
60
|
@sql += " limit :limit offset :offset " if @include_limit_clause
|
61
61
|
end
|
data/lib/query_helper/version.rb
CHANGED
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.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan McDaniel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05
|
11
|
+
date: 2020-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|