pattern_query_helper 0.1.10 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b868a9d24784bf1635be621ee9ab43542f176774
4
- data.tar.gz: e1595a029ff272c8c7fc8dfe872615b672ab40dc
3
+ metadata.gz: 65460f1546e868997cf4f1b0d049ee2d16f68ff4
4
+ data.tar.gz: 347f43fb24fad0aed12bfd4c5e3c901edc5b5b87
5
5
  SHA512:
6
- metadata.gz: 267714428ec883cd37a8879ae59ca3442fbd20296012575d3b5cb84796535f6a22f74d14fa93ab56ff0bf1c97dff1bfb04d32f7c9eeba2c30285d2bb7955cd79
7
- data.tar.gz: 0f84d630e8e6c7174b80135cb6a5e27a22f519d18131ca148545278f38e64abb97f1faa6a0a7b9a0e80593480863e118fe1d578aaf133299510703eeae42e0a5
6
+ metadata.gz: 8db937be84711841bd756d37f43021ad0e73541bcc9f16401641d03f1fbb0429c505c5e6668d0a4e7eb80128246c69734aab59b3b6f977e5811f2404eda7a78b
7
+ data.tar.gz: f198e72b5325b64221fc4105da3259f8e6ed2127b5fd625549f0b13fed206bccb850cbfa049d9aceb6eb9b32723bb4a5125a2d8b7a88b416c5188f4001e20f67
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pattern_query_helper (0.1.10)
4
+ pattern_query_helper (0.2.0)
5
5
  activerecord (~> 5.0)
6
6
  kaminari
7
7
 
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # PatternQueryHelper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pattern_query_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- ## Usage
21
+ ## Use
22
+
23
+ ### Active Record Queries
24
24
 
25
- TODO: Write usage instructions here
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
- ## Development
33
+ ### Custom SQL Queries
28
34
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
 
@@ -1,3 +1,3 @@
1
1
  module PatternQueryHelper
2
- VERSION = "0.1.10"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.10
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-01 00:00:00.000000000 Z
11
+ date: 2019-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler