pattern_query_helper 0.1.10 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +187 -8
- data/lib/pattern_query_helper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65460f1546e868997cf4f1b0d049ee2d16f68ff4
|
4
|
+
data.tar.gz: 347f43fb24fad0aed12bfd4c5e3c901edc5b5b87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8db937be84711841bd756d37f43021ad0e73541bcc9f16401641d03f1fbb0429c505c5e6668d0a4e7eb80128246c69734aab59b3b6f977e5811f2404eda7a78b
|
7
|
+
data.tar.gz: f198e72b5325b64221fc4105da3259f8e6ed2127b5fd625549f0b13fed206bccb850cbfa049d9aceb6eb9b32723bb4a5125a2d8b7a88b416c5188f4001e20f67
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# PatternQueryHelper
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Ruby Gem developed and used at Pattern to paginate, sort, filter, and include associations on sql and active record queries.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -20,15 +18,196 @@ Or install it yourself as:
|
|
20
18
|
|
21
19
|
$ gem install pattern_query_helper
|
22
20
|
|
23
|
-
##
|
21
|
+
## Use
|
22
|
+
|
23
|
+
### Active Record Queries
|
24
24
|
|
25
|
-
|
25
|
+
To run an active record query execute
|
26
|
+
```ruby
|
27
|
+
PatternQueryHelper.run_sql_query(active_record_call, query_helpers, single_record)
|
28
|
+
```
|
29
|
+
active_record_call: Valid active record syntax (i.e. ```Object.where(state: 'Active')```)
|
30
|
+
query_helpers: See docs below
|
31
|
+
single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
|
26
32
|
|
27
|
-
|
33
|
+
### Custom SQL Queries
|
28
34
|
|
29
|
-
|
35
|
+
To run a custom sql query execute
|
36
|
+
```ruby
|
37
|
+
PatternQueryHelper.run_sql_query(model, query, query_params, query_helpers, single_record)
|
38
|
+
```
|
39
|
+
model: A valid ActiveRecord model
|
40
|
+
query: A string containing your custom SQL query
|
41
|
+
query_params: a symbolized hash of binds to be included in your SQL query
|
42
|
+
query_helpers: See docs below
|
43
|
+
single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
|
44
|
+
|
45
|
+
## Query Helpers
|
46
|
+
query_helpers is a symbolized hash passed in with information about pagination, associations, filtering and sorting.
|
47
|
+
|
48
|
+
### Pagination
|
49
|
+
There are two pagination keys you can pass in as part of the query_helpers objects
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
{
|
53
|
+
page: 1,
|
54
|
+
per_page: 20
|
55
|
+
}
|
56
|
+
```
|
30
57
|
|
31
|
-
|
58
|
+
If at least one of these keys is present, paginated results will be returned.
|
59
|
+
|
60
|
+
### Sorting
|
61
|
+
Sorting is controlled by the `sort` key in the query_helpers object
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
{
|
65
|
+
sort: "column_name:sort_direction"
|
66
|
+
}
|
67
|
+
```
|
68
|
+
Sort direction can be either asc or desc. If you wish to lowercase string before sorting include the following:
|
69
|
+
```ruby
|
70
|
+
{
|
71
|
+
sort: "name:desc:lowercase"
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
### Filtering
|
76
|
+
Filtering is controlled by the `filter` object in the query_helpers hash
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
{
|
80
|
+
filter: {
|
81
|
+
"column_1" => {
|
82
|
+
"gte" => 20,
|
83
|
+
"lt" => 40
|
84
|
+
},
|
85
|
+
"column_2" => {
|
86
|
+
"eql" => "my_string"
|
87
|
+
}
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
The following operator codes are valid
|
92
|
+
|
93
|
+
```
|
94
|
+
“gte”: >=
|
95
|
+
“lte”: <=
|
96
|
+
“gt”: >
|
97
|
+
“lt”: <
|
98
|
+
“eql”: =
|
99
|
+
“noteql”: !=
|
100
|
+
“in”: in
|
101
|
+
“notin” not in
|
102
|
+
“null”: “is null” or “is not null” (pass in true or false as the value)
|
103
|
+
```
|
104
|
+
|
105
|
+
### Associations
|
106
|
+
|
107
|
+
To include associated objects in the payload, pass in the following as part of the query_helpers hash:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
{
|
111
|
+
include: ['associated_object_1', 'associated_object_2']
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
### Example
|
116
|
+
|
117
|
+
The following is an example of a query_helpers object that can be passed into the sql and active record methods
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
query_helpers = {
|
121
|
+
page: 1,
|
122
|
+
per_page: 20,
|
123
|
+
sort: "name:desc"
|
124
|
+
include: ["child"]
|
125
|
+
filter: {
|
126
|
+
"id" => {
|
127
|
+
"gte" => 20,
|
128
|
+
"lt" => 40
|
129
|
+
}
|
130
|
+
}
|
131
|
+
```
|
132
|
+
|
133
|
+
## Payload Formats
|
134
|
+
|
135
|
+
The PatternQueryHelper gem will return results in one of three formats
|
136
|
+
|
137
|
+
### Paginated List Payload
|
138
|
+
```json
|
139
|
+
{
|
140
|
+
"pagination": {
|
141
|
+
"count": 18,
|
142
|
+
"current_page": 1,
|
143
|
+
"next_page": 2,
|
144
|
+
"previous_page": null,
|
145
|
+
"total_pages": 6,
|
146
|
+
"per_page": 3,
|
147
|
+
"first_page": true,
|
148
|
+
"last_page": false,
|
149
|
+
"out_of_range": false
|
150
|
+
},
|
151
|
+
"data": [
|
152
|
+
{
|
153
|
+
"id": 1,
|
154
|
+
"attribute_1": "string_attribute",
|
155
|
+
"attribute_2": 12345,
|
156
|
+
"attribute_3": 0.3423212
|
157
|
+
},
|
158
|
+
{
|
159
|
+
"id": 2,
|
160
|
+
"attribute_1": "string_attribute",
|
161
|
+
"attribute_2": 12345,
|
162
|
+
"attribute_3": 0.3423212
|
163
|
+
},
|
164
|
+
{
|
165
|
+
"id": 3,
|
166
|
+
"attribute_1": "string_attribute",
|
167
|
+
"attribute_2": 12345,
|
168
|
+
"attribute_3": 0.3423212
|
169
|
+
},
|
170
|
+
]
|
171
|
+
}
|
172
|
+
```
|
173
|
+
|
174
|
+
### List Payload
|
175
|
+
```json
|
176
|
+
{
|
177
|
+
"data": [
|
178
|
+
{
|
179
|
+
"id": 1,
|
180
|
+
"attribute_1": "string_attribute",
|
181
|
+
"attribute_2": 12345,
|
182
|
+
"attribute_3": 0.3423212
|
183
|
+
},
|
184
|
+
{
|
185
|
+
"id": 2,
|
186
|
+
"attribute_1": "string_attribute",
|
187
|
+
"attribute_2": 12345,
|
188
|
+
"attribute_3": 0.3423212
|
189
|
+
},
|
190
|
+
{
|
191
|
+
"id": 3,
|
192
|
+
"attribute_1": "string_attribute",
|
193
|
+
"attribute_2": 12345,
|
194
|
+
"attribute_3": 0.3423212
|
195
|
+
},
|
196
|
+
]
|
197
|
+
}
|
198
|
+
```
|
199
|
+
|
200
|
+
### Single Record Payload
|
201
|
+
```json
|
202
|
+
{
|
203
|
+
"data": {
|
204
|
+
"id": 1,
|
205
|
+
"attribute_1": "string_attribute",
|
206
|
+
"attribute_2": 12345,
|
207
|
+
"attribute_3": 0.3423212
|
208
|
+
}
|
209
|
+
}
|
210
|
+
```
|
32
211
|
|
33
212
|
## Contributing
|
34
213
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pattern_query_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-03-
|
11
|
+
date: 2019-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|