query_helper 0.1.1 → 0.1.2

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: 871736bd5aa9e012fb55ce1738a150782de390cefa9275c2d61e5d5bd89cdf84
4
- data.tar.gz: 7c11403e23c7b6247a540c475dae7aa083ffb79cf4f1f0ec944a583f86f8eccd
3
+ metadata.gz: 1988f3c68a54af6511be4ef536fa27df2080fdfa5495b8f5532ad729fdf0f029
4
+ data.tar.gz: c7783fc5b7a275e4a78b9c5ca37be49c85b3e6113c9f657e72ce16101a12f364
5
5
  SHA512:
6
- metadata.gz: 2105e8fee2c52885ef26b333eebe43cf017e030a6ee9420fc573f044c2d619586a9bdcf8569856861ce4599a24607284d1d80823d664c47f8465378b86bceffb
7
- data.tar.gz: 0e2a4dc0b3a310f81e1713a78d61707d8d02a82b6300d7676ee6ec442f8d89302e74cf3bb81a1e13857cdf65d6d5246a19fbc13d16308ca35c4e47aa13dfb771
6
+ metadata.gz: f247dc18f26acd1937c1d963beb6a6e10bb9a96b4bdec974a9831279cacb65e3d12cc3f42cb01ab97135fc164d0e263349793ccfb51784ab72700a3b6d890abf
7
+ data.tar.gz: 4573625d4a526036fae518209836ad7be1aec3b49914cfad3e457182aeab810597903b260e74422b8563f2ed04a4623a9c11907aa791014abb5ffefe5dc41037
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.1.1)
4
+ query_helper (0.1.2)
5
5
  activerecord (~> 5.0)
6
6
  activesupport (~> 5.0)
7
7
 
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, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload
16
+ attr_accessor :model, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload
17
17
 
18
18
  def initialize(
19
19
  model: nil, # the model to run the query against
@@ -27,7 +27,8 @@ class QueryHelper
27
27
  associations: [], # a list of activerecord associations you'd like included in the payload
28
28
  as_json_options: nil, # a list of as_json options you'd like run before returning the payload
29
29
  custom_mappings: {}, # custom keyword => sql_expression mappings
30
- api_payload: false # Return the paginated payload or simply return the result array
30
+ api_payload: false, # Return the paginated payload or simply return the result array
31
+ preload: [] # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
31
32
  )
32
33
  @model = model
33
34
  @query = query
@@ -41,6 +42,7 @@ class QueryHelper
41
42
  @as_json_options = as_json_options
42
43
  @custom_mappings = custom_mappings
43
44
  @api_payload = api_payload
45
+ @preload = preload
44
46
 
45
47
  if @page && @per_page
46
48
  # Determine limit and offset
@@ -60,7 +62,8 @@ class QueryHelper
60
62
  associations: [],
61
63
  as_json_options: nil,
62
64
  single_record: nil,
63
- custom_mappings: nil
65
+ custom_mappings: nil,
66
+ preload: []
64
67
  )
65
68
  @model = model if model
66
69
  @query = query if query
@@ -70,6 +73,7 @@ class QueryHelper
70
73
  @single_record = single_record if single_record
71
74
  @as_json_options = as_json_options if as_json_options
72
75
  @custom_mappings = custom_mappings if custom_mappings
76
+ @preload = preload if preload
73
77
  end
74
78
 
75
79
  def add_filter(operator_code:, criterion:, comparate:)
@@ -106,6 +110,7 @@ class QueryHelper
106
110
  @results = @results.first if @single_record # Return a single result if requested
107
111
 
108
112
  determine_count()
113
+ preload_associations()
109
114
  load_associations()
110
115
  clean_results()
111
116
  end
@@ -116,8 +121,6 @@ class QueryHelper
116
121
  return @results
117
122
  end
118
123
 
119
-
120
-
121
124
  private
122
125
 
123
126
  def paginated_results
@@ -151,6 +154,13 @@ class QueryHelper
151
154
  )
152
155
  end
153
156
 
157
+ def preload_associations
158
+ Associations.preload_associations(
159
+ payload: @results,
160
+ preload: @preload
161
+ )
162
+ end
163
+
154
164
  def clean_results
155
165
  @results.map!{ |r| r.except("_query_full_count") } if @page && @per_page
156
166
  end
@@ -185,5 +195,4 @@ class QueryHelper
185
195
  model: @model
186
196
  )
187
197
  end
188
-
189
198
  end
@@ -12,6 +12,10 @@ class QueryHelper
12
12
  payload.as_json(as_json_options)
13
13
  end
14
14
 
15
+ def self.preload_associations(payload:, preload: [])
16
+ ActiveRecord::Associations::Preloader.new.preload(payload, preload)
17
+ end
18
+
15
19
  def self.json_associations(associations)
16
20
  associations ||= []
17
21
  associations = associations.is_a?(Array) ? associations : [associations]
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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.1
4
+ version: 0.1.2
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-05 00:00:00.000000000 Z
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler