query_helper 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78d3243fbf7e2565a4729636a43bce07ce12e891e4c7c154e3d086c6cbb3ff28
4
- data.tar.gz: 54db35985205d12098c3f97c57c3cc25baeea13588fd82cb4c55ff8b040155a0
3
+ metadata.gz: 7b9afd63373b33bf0c2f1dce1bccf08842db0aa811b72cc6390ba235f3232bbd
4
+ data.tar.gz: efe1ea85137f17bb102a4779f5ae669530778df077fcb4f9393ecffe467f1f19
5
5
  SHA512:
6
- metadata.gz: 045e30a87e6bcbdd513f9fe25911897ef94db45fd8931201491c184d36f7f2582aaf465ed0b1ded8f8abc2a00697e7c0fed1bf62f2c019fb427a50dfa8e95a29
7
- data.tar.gz: c66c347806fd79685a4232f9922cd2dba8cb31ae5d0af76e88d5f547bfe5fa51a15d618f8aacefa673f39b6eb0dccd298cf64134860866dd8d937aff439b1309
6
+ metadata.gz: 3d1744f4a0550cf55b1bc31012d492be9ddc4b3c281b75925afba35888dea7b6e5a9db2e94498ba99a30d269f8172567cce3e6e5a67765b8ed7ce7fc65722c1f
7
+ data.tar.gz: 8559aed42b400167060874c43b9cfafc772ffcebb8adc7003d0f4eb63028ef08c337925380cc3fe4b92527dfe732f5d2f3da0bd37de510851bd65a8de4e01096
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.1.2)
4
+ query_helper (0.1.5)
5
5
  activerecord (~> 5.0)
6
6
  activesupport (~> 5.0)
7
7
 
@@ -35,21 +35,21 @@ GEM
35
35
  arel (9.0.0)
36
36
  builder (3.2.3)
37
37
  byebug (11.0.1)
38
- concurrent-ruby (1.1.4)
38
+ concurrent-ruby (1.1.5)
39
39
  crass (1.0.4)
40
40
  diff-lcs (1.3)
41
- erubi (1.8.0)
42
- faker (1.9.3)
41
+ erubi (1.9.0)
42
+ faker (1.9.6)
43
43
  i18n (>= 0.7)
44
44
  i18n (1.6.0)
45
45
  concurrent-ruby (~> 1.0)
46
- loofah (2.2.3)
46
+ loofah (2.3.0)
47
47
  crass (~> 1.0.2)
48
48
  nokogiri (>= 1.5.9)
49
49
  method_source (0.9.2)
50
50
  mini_portile2 (2.4.0)
51
- minitest (5.11.3)
52
- nokogiri (1.10.3)
51
+ minitest (5.12.2)
52
+ nokogiri (1.10.4)
53
53
  mini_portile2 (~> 2.4.0)
54
54
  rack (2.0.7)
55
55
  rack-test (1.1.0)
@@ -57,7 +57,7 @@ GEM
57
57
  rails-dom-testing (2.0.3)
58
58
  activesupport (>= 4.2.0)
59
59
  nokogiri (>= 1.6)
60
- rails-html-sanitizer (1.0.4)
60
+ rails-html-sanitizer (1.2.0)
61
61
  loofah (~> 2.2, >= 2.2.2)
62
62
  railties (5.2.3)
63
63
  actionpack (= 5.2.3)
@@ -70,12 +70,12 @@ GEM
70
70
  rspec-core (~> 3.8.0)
71
71
  rspec-expectations (~> 3.8.0)
72
72
  rspec-mocks (~> 3.8.0)
73
- rspec-core (3.8.0)
73
+ rspec-core (3.8.2)
74
74
  rspec-support (~> 3.8.0)
75
- rspec-expectations (3.8.2)
75
+ rspec-expectations (3.8.4)
76
76
  diff-lcs (>= 1.2.0, < 2.0)
77
77
  rspec-support (~> 3.8.0)
78
- rspec-mocks (3.8.0)
78
+ rspec-mocks (3.8.1)
79
79
  diff-lcs (>= 1.2.0, < 2.0)
80
80
  rspec-support (~> 3.8.0)
81
81
  rspec-rails (3.8.2)
@@ -86,7 +86,7 @@ GEM
86
86
  rspec-expectations (~> 3.8.0)
87
87
  rspec-mocks (~> 3.8.0)
88
88
  rspec-support (~> 3.8.0)
89
- rspec-support (3.8.0)
89
+ rspec-support (3.8.2)
90
90
  sqlite3 (1.3.13)
91
91
  thor (0.20.3)
92
92
  thread_safe (0.3.6)
data/README.md CHANGED
@@ -170,6 +170,44 @@ The QueryHelper gem will return the following payload
170
170
  }
171
171
  ```
172
172
 
173
+ ## Using in Models or Services
174
+
175
+ If your complex queries are defined in a model or service, you can still use QueryHelper to automatically paginate, filter, and sort api calls that reference the given model/service.
176
+
177
+ ### Example
178
+
179
+ #### Model
180
+
181
+ ```ruby
182
+ class MyModel < ApplicationRecord
183
+
184
+ def complex_sql_function(query_helper=QueryHelper.new)
185
+ query = "select * from resource"
186
+ query_helper.update(
187
+ model: Resource,
188
+ query: query,
189
+ )
190
+ end
191
+ end
192
+ ```
193
+
194
+ When calling this model from outside a controller, you will get the full result set without the api wrapping. (i.e. `MyModel.first.complex_sql_function` will return an array.
195
+
196
+ #### Controller
197
+
198
+ ```ruby
199
+ class MyModelsController < ApplicationController
200
+
201
+ def get_complex_query
202
+ @object = MyModel.find(params[:id])
203
+ response = @object.complex_sql_function(@query_helper)
204
+ render json: response
205
+ end
206
+ end
207
+ ```
208
+
209
+ When you pass in the `@query_helper` object from the controller, QueryHelper will paginate, sort, and filter as expected.
210
+
173
211
  ## Advanced Options
174
212
 
175
213
  ### Associations
@@ -192,6 +230,44 @@ You can pass in additional as_json options to be included in the payload.
192
230
  )
193
231
  ```
194
232
 
233
+ ### Preload
234
+
235
+ This is handy if you are loading other associations or methods with the as_json config and need to preload associations to avoid n+1 queries.
236
+
237
+ ```ruby
238
+ @query_helper.update(
239
+ preload: [:association1, :association2]
240
+ )
241
+ ```
242
+ or
243
+ ```ruby
244
+ @query_helper.update(
245
+ preload: [association: [:child_association]]
246
+ )
247
+ ```
248
+
249
+ ### Custom Sort and Filter mappings
250
+
251
+ QueryHelper will automatically determine which sql aliases to run filtering and sorting on. In cases where this doesn't work, you can provide your own custom mappings so QueryHelper knows how to correctly sort and filter. One common example of this is when you run a `select * from resource1` but pass `resource2` in as the model.
252
+
253
+ ```ruby
254
+ @query_helper.update(
255
+ custom_mappings: {
256
+ "alias" => "complex_sql_function"
257
+ }
258
+ )
259
+ ```
260
+
261
+ To indicate that a custom mapping refers to an aggregate function use the following:
262
+
263
+ ```ruby
264
+ @query_helper.update(
265
+ custom_mappings: {
266
+ "alias" => { sql_expression: "MAX(resouce.age)", aggregate: true }
267
+ }
268
+ )
269
+ ```
270
+
195
271
  ### Single Record Queries
196
272
  If you only want to return a single result, but still want to be able to use some of the other functionality of QueryHelper, you can set `single_record` to true in the QueryHelper object.
197
273
 
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/query_helper.rb CHANGED
@@ -74,13 +74,14 @@ class QueryHelper
74
74
  @as_json_options = as_json_options if as_json_options
75
75
  @custom_mappings = custom_mappings if custom_mappings
76
76
  @preload = preload if preload
77
+ return self
77
78
  end
78
79
 
79
80
  def add_filter(operator_code:, criterion:, comparate:)
80
81
  @sql_filter.filter_values["comparate"] = { operator_code => criterion }
81
82
  end
82
83
 
83
- def execute_query
84
+ def build_query
84
85
  # Correctly set the query and model based on query type
85
86
  determine_query_type()
86
87
 
@@ -105,8 +106,17 @@ class QueryHelper
105
106
  include_limit_clause: @page && @per_page ? true : false,
106
107
  additional_select_clauses: @sql_sort.select_strings
107
108
  )
108
- @executed_query = manipulator.build()
109
- @results = @model.find_by_sql([@executed_query, @bind_variables]) # Execute Sql Query
109
+ manipulator.build()
110
+ end
111
+
112
+ def view_query
113
+ query = build_query()
114
+ @model.sanitize_sql_array([query, @bind_variables])
115
+ end
116
+
117
+ def execute_query
118
+ query = build_query()
119
+ @results = @model.find_by_sql([query, @bind_variables]) # Execute Sql Query
110
120
  @results = @results.first if @single_record # Return a single result if requested
111
121
 
112
122
  determine_count()
@@ -143,7 +153,11 @@ class QueryHelper
143
153
 
144
154
  def determine_count
145
155
  # Determine total result count (unpaginated)
146
- @count = @page && @per_page && @results.length > 0 ? @results.first["_query_full_count"] : @results.length
156
+ if @single_record
157
+ @count = 1
158
+ else
159
+ @count = @page && @per_page && @results.length > 0 ? @results.first["_query_full_count"] : @results.length
160
+ end
147
161
  end
148
162
 
149
163
  def load_associations
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.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan McDaniel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-28 00:00:00.000000000 Z
11
+ date: 2019-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler