query_helper 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9748c7b95202e187a62f4e889db450dbd6a406aac6f342d6fb0ae5f532e77f98
4
+ data.tar.gz: bc3f05f3093a0730758d05d7fab7485960b4d0dfbd489982e3dbbe996f51fb15
5
+ SHA512:
6
+ metadata.gz: 833e99acbbac34e55b2c99c03977c707792c1e1ed1b86d65a347d2184c01f96894cc5e41c40dab0e81f4b7463511ab63cd1ade1d9f7b36a6b689a9bfcece5df7
7
+ data.tar.gz: 73ccb427fc984585951c099fd311ebe5f83ff20d927f6e196b69e0d5f899b7e55e42a027de87424c3e986b94e767aa3faceab106f8aacd07cae1341f6b78c79e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ dist: xenial
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 1.16.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at eamigo13@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in query_helper.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ query_helper (0.0.0)
5
+ activerecord (~> 5.0)
6
+ activesupport (~> 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (5.2.3)
12
+ actionview (= 5.2.3)
13
+ activesupport (= 5.2.3)
14
+ rack (~> 2.0)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
+ actionview (5.2.3)
19
+ activesupport (= 5.2.3)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
+ activemodel (5.2.3)
25
+ activesupport (= 5.2.3)
26
+ activerecord (5.2.3)
27
+ activemodel (= 5.2.3)
28
+ activesupport (= 5.2.3)
29
+ arel (>= 9.0)
30
+ activesupport (5.2.3)
31
+ concurrent-ruby (~> 1.0, >= 1.0.2)
32
+ i18n (>= 0.7, < 2)
33
+ minitest (~> 5.1)
34
+ tzinfo (~> 1.1)
35
+ arel (9.0.0)
36
+ builder (3.2.3)
37
+ byebug (11.0.1)
38
+ concurrent-ruby (1.1.4)
39
+ crass (1.0.4)
40
+ diff-lcs (1.3)
41
+ erubi (1.8.0)
42
+ faker (1.9.3)
43
+ i18n (>= 0.7)
44
+ i18n (1.6.0)
45
+ concurrent-ruby (~> 1.0)
46
+ loofah (2.2.3)
47
+ crass (~> 1.0.2)
48
+ nokogiri (>= 1.5.9)
49
+ method_source (0.9.2)
50
+ mini_portile2 (2.4.0)
51
+ minitest (5.11.3)
52
+ nokogiri (1.10.3)
53
+ mini_portile2 (~> 2.4.0)
54
+ rack (2.0.7)
55
+ rack-test (1.1.0)
56
+ rack (>= 1.0, < 3)
57
+ rails-dom-testing (2.0.3)
58
+ activesupport (>= 4.2.0)
59
+ nokogiri (>= 1.6)
60
+ rails-html-sanitizer (1.0.4)
61
+ loofah (~> 2.2, >= 2.2.2)
62
+ railties (5.2.3)
63
+ actionpack (= 5.2.3)
64
+ activesupport (= 5.2.3)
65
+ method_source
66
+ rake (>= 0.8.7)
67
+ thor (>= 0.19.0, < 2.0)
68
+ rake (10.5.0)
69
+ rspec (3.8.0)
70
+ rspec-core (~> 3.8.0)
71
+ rspec-expectations (~> 3.8.0)
72
+ rspec-mocks (~> 3.8.0)
73
+ rspec-core (3.8.0)
74
+ rspec-support (~> 3.8.0)
75
+ rspec-expectations (3.8.2)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.8.0)
78
+ rspec-mocks (3.8.0)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.8.0)
81
+ rspec-rails (3.8.2)
82
+ actionpack (>= 3.0)
83
+ activesupport (>= 3.0)
84
+ railties (>= 3.0)
85
+ rspec-core (~> 3.8.0)
86
+ rspec-expectations (~> 3.8.0)
87
+ rspec-mocks (~> 3.8.0)
88
+ rspec-support (~> 3.8.0)
89
+ rspec-support (3.8.0)
90
+ sqlite3 (1.3.13)
91
+ thor (0.20.3)
92
+ thread_safe (0.3.6)
93
+ tzinfo (1.2.5)
94
+ thread_safe (~> 0.1)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ actionpack
101
+ activesupport
102
+ bundler (~> 1.16)
103
+ byebug
104
+ faker (~> 1.9.3)
105
+ query_helper!
106
+ rake (~> 10.0)
107
+ rspec (~> 3.0)
108
+ rspec-rails
109
+ sqlite3 (~> 1.3.6)
110
+
111
+ BUNDLED WITH
112
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Evan McDaniel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,381 @@
1
+ # QueryHelper
2
+ [![TravisCI](https://travis-ci.org/iserve-products/query_helper.svg?branch=master)](https://travis-ci.org/iserve-products/query_helper)
3
+ [![Gem Version](https://badge.fury.io/rb/query_helper.svg)](https://badge.fury.io/rb/query_helper)
4
+
5
+ Ruby Gem developed and used at Pattern to paginate, sort, filter, and include associations on sql and active record queries.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'query_helper'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install query_helper
22
+
23
+ ## Use
24
+
25
+ ### SQL Queries
26
+
27
+ #### Initialize
28
+
29
+ To create a new sql query object run
30
+
31
+ ```ruby
32
+ QueryHelper::Sql.new(
33
+ model:, # required
34
+ query:, # required
35
+ query_params: , # optional
36
+ column_mappings: , # optional
37
+ filters: , # optional
38
+ sorts: , # optional
39
+ page: , # optional
40
+ per_page: , # optional
41
+ single_record: , # optional, default: false
42
+ associations: , # optional
43
+ as_json_options: , # optional
44
+ run: # optional, default: true
45
+ )
46
+ ```
47
+
48
+ The following arguments are accepted when creating a new objects
49
+
50
+ <table>
51
+ <tr>
52
+ <th>Argument</th>
53
+ <th>Description</th>
54
+ <th>Example</th>
55
+ </tr>
56
+ <tr>
57
+ <td>model</td>
58
+ <td>the model to run the query against</td>
59
+ <td>
60
+ <pre lang="ruby">
61
+ Parent
62
+ </pre>
63
+ </td>
64
+ </tr>
65
+ <tr>
66
+ <td>query</td>
67
+ <td>the custom sql string to be executed</td>
68
+ <td>
69
+ <pre lang="ruby">
70
+ 'select * from parents'
71
+ </pre>
72
+ </td>
73
+ </tr>
74
+ <tr>
75
+ <td>query_params</td>
76
+ <td>a hash of bind variables to be embedded into the sql query</td>
77
+ <td>
78
+ <pre lang="ruby">
79
+ {
80
+ age: 20,
81
+ name: 'John'
82
+ }
83
+ </pre>
84
+ </td>
85
+ </tr>
86
+ <tr>
87
+ <td>column_mappings</td>
88
+ <td>A hash that translates aliases to sql expressions</td>
89
+ <td>
90
+ <pre lang="ruby">
91
+ {
92
+ "age" => "parents.age"
93
+ "children_count" => {
94
+ sql_expression: "count(children.id)",
95
+ aggregate: true
96
+ }
97
+ }
98
+ </pre>
99
+ </td>
100
+ </tr>
101
+ <tr>
102
+ <td>filters</td>
103
+ <td>a list of filters in the form of `{"comparate_alias"=>{"operator_code"=>"value"}}`</td>
104
+ <td>
105
+ <pre lang="ruby">
106
+ {
107
+ "age" => { "lt" => 100 },
108
+ "children_count" => { "gt" => 0 }
109
+ }
110
+ </pre>
111
+ </td>
112
+ </tr>
113
+ <tr>
114
+ <td>sorts</td>
115
+ <td>a comma separated string with a list of sort values</td>
116
+ <td>
117
+ <pre lang="ruby">
118
+ "age:desc,name:asc:lowercase"
119
+ </pre>
120
+ </td>
121
+ </tr>
122
+ <tr>
123
+ <td>page</td>
124
+ <td>the page you want returned</td>
125
+ <td>
126
+ <pre lang="ruby">
127
+ 5
128
+ </pre>
129
+ </td>
130
+ </tr>
131
+ <tr>
132
+ <td>per_page</td>
133
+ <td>the number of results per page</td>
134
+ <td>
135
+ <pre lang="ruby">
136
+ 20
137
+ </pre>
138
+ </td>
139
+ </tr>
140
+ <tr>
141
+ <td>single_record</td>
142
+ <td>whether or not you expect the record to return a single result, if toggled, only the first result will be returned</td>
143
+ <td>
144
+ <pre lang="ruby">
145
+ false
146
+ </pre>
147
+ </td>
148
+ </tr>
149
+ <tr>
150
+ <td>associations</td>
151
+ <td>a list of activerecord associations you'd like included in the payload </td>
152
+ <td>
153
+ <pre lang="ruby">
154
+
155
+ </pre>
156
+ </td>
157
+ </tr>
158
+ <tr>
159
+ <td>as_json_options</td>
160
+ <td>a list of as_json options you'd like run before returning the payload</td>
161
+ <td>
162
+ <pre lang="ruby">
163
+
164
+ </pre>
165
+ </td>
166
+ </tr>
167
+ <tr>
168
+ <td>run</td>
169
+ <td>whether or not you'd like to run the query on initilization</td>
170
+ <td>
171
+ <pre lang="ruby">
172
+ false
173
+ </pre>
174
+ </td>
175
+ </tr>
176
+ </table>
177
+
178
+ ### Active Record Queries
179
+
180
+ To run an active record query execute
181
+ ```ruby
182
+ QueryHelper.run_active_record_query(active_record_call, query_helpers, valid_columns, single_record)
183
+ ```
184
+ active_record_call: Valid active record syntax (i.e. ```Object.where(state: 'Active')```)
185
+ query_helpers: See docs below
186
+ valid_columns: Default is []. Pass in an array of columns you want to allow sorting and filtering on.
187
+ single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
188
+
189
+
190
+ model: A valid ActiveRecord model
191
+ query: A string containing your custom SQL query
192
+ query_params: a symbolized hash of binds to be included in your SQL query
193
+ query_helpers: See docs below
194
+ valid_columns: Default is []. Pass in an array of columns you want to allow sorting and filtering on.
195
+ single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
196
+
197
+ ## Query Helpers
198
+ query_helpers is a symbolized hash passed in with information about pagination, associations, filtering and sorting.
199
+
200
+ ### Pagination
201
+ There are two pagination keys you can pass in as part of the query_helpers objects
202
+
203
+ ```ruby
204
+ {
205
+ page: 1,
206
+ per_page: 20
207
+ }
208
+ ```
209
+
210
+ If at least one of these keys is present, paginated results will be returned.
211
+
212
+ ### Sorting
213
+ Sorting is controlled by the `sort` key in the query_helpers object
214
+
215
+ ```ruby
216
+ {
217
+ sort: "column_name:sort_direction"
218
+ }
219
+ ```
220
+ Sort direction can be either asc or desc. If you wish to lowercase string before sorting include the following:
221
+ ```ruby
222
+ {
223
+ sort: "name:desc:lowercase"
224
+ }
225
+ ```
226
+
227
+ ### Filtering
228
+ Filtering is controlled by the `filter` object in the query_helpers hash
229
+
230
+ ```ruby
231
+ {
232
+ filter: {
233
+ "column_1" => {
234
+ "gte" => 20,
235
+ "lt" => 40
236
+ },
237
+ "column_2" => {
238
+ "eql" => "my_string"
239
+ },
240
+ "column_3" => {
241
+ "like" => "my_string%"
242
+ },
243
+ "column_4" => {
244
+ "in" => "item1,item2,item3"
245
+ }
246
+ }
247
+ ```
248
+
249
+ The following operator codes are valid
250
+
251
+ ```
252
+ “gte”: >=
253
+ “lte”: <=
254
+ “gt”: >
255
+ “lt”: <
256
+ “eql”: =
257
+ “noteql”: !=
258
+ "like": like
259
+ “in”: in
260
+ “notin” not in
261
+ “null”: “is null” or “is not null” (pass in true or false as the value)
262
+ ```
263
+
264
+ ### Associations
265
+
266
+ To include associated objects in the payload, pass in the following as part of the query_helpers hash:
267
+
268
+ ```ruby
269
+ {
270
+ include: ['associated_object_1', 'associated_object_2']
271
+ }
272
+ ```
273
+
274
+ ### Example
275
+
276
+ The following is an example of a query_helpers object that can be passed into the sql and active record methods
277
+
278
+ ```ruby
279
+ query_helpers = {
280
+ page: 1,
281
+ per_page: 20,
282
+ sort: "name:desc"
283
+ include: ["child"]
284
+ filter: {
285
+ "id" => {
286
+ "gte" => 20,
287
+ "lt" => 40
288
+ }
289
+ }
290
+ ```
291
+
292
+ ## Payload Formats
293
+
294
+ The QueryHelper gem will return results in one of three formats
295
+
296
+ ### Paginated List Payload
297
+ ```json
298
+ {
299
+ "pagination": {
300
+ "count": 18,
301
+ "current_page": 1,
302
+ "next_page": 2,
303
+ "previous_page": null,
304
+ "total_pages": 6,
305
+ "per_page": 3,
306
+ "first_page": true,
307
+ "last_page": false,
308
+ "out_of_range": false
309
+ },
310
+ "data": [
311
+ {
312
+ "id": 1,
313
+ "attribute_1": "string_attribute",
314
+ "attribute_2": 12345,
315
+ "attribute_3": 0.3423212
316
+ },
317
+ {
318
+ "id": 2,
319
+ "attribute_1": "string_attribute",
320
+ "attribute_2": 12345,
321
+ "attribute_3": 0.3423212
322
+ },
323
+ {
324
+ "id": 3,
325
+ "attribute_1": "string_attribute",
326
+ "attribute_2": 12345,
327
+ "attribute_3": 0.3423212
328
+ },
329
+ ]
330
+ }
331
+ ```
332
+
333
+ ### List Payload
334
+ ```json
335
+ {
336
+ "data": [
337
+ {
338
+ "id": 1,
339
+ "attribute_1": "string_attribute",
340
+ "attribute_2": 12345,
341
+ "attribute_3": 0.3423212
342
+ },
343
+ {
344
+ "id": 2,
345
+ "attribute_1": "string_attribute",
346
+ "attribute_2": 12345,
347
+ "attribute_3": 0.3423212
348
+ },
349
+ {
350
+ "id": 3,
351
+ "attribute_1": "string_attribute",
352
+ "attribute_2": 12345,
353
+ "attribute_3": 0.3423212
354
+ },
355
+ ]
356
+ }
357
+ ```
358
+
359
+ ### Single Record Payload
360
+ ```json
361
+ {
362
+ "data": {
363
+ "id": 1,
364
+ "attribute_1": "string_attribute",
365
+ "attribute_2": 12345,
366
+ "attribute_3": 0.3423212
367
+ }
368
+ }
369
+ ```
370
+
371
+ ## Contributing
372
+
373
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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.
374
+
375
+ ## License
376
+
377
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
378
+
379
+ ## Code of Conduct
380
+
381
+ Everyone interacting in the QueryHelper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/query_helper/blob/master/CODE_OF_CONDUCT.md).