query_helper 0.2.8 → 0.2.13

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: 2c63157141a68e8dafa45c307f5be2c90cc88fdea8b4c4777c4f81f89a58e797
4
- data.tar.gz: 720e0093ccddebf935977aa8180127c99b2bbcd232f343ba75684c32c38d7686
3
+ metadata.gz: b0971fc10b2b990185b05e538e41dcb898033baa6e9f3f59c22a5c024fead8bb
4
+ data.tar.gz: 55e383eadc934c0ff9f1cb44236033b5c93b6d7b302210fc10eaff1c8e4d614f
5
5
  SHA512:
6
- metadata.gz: 2d49fc27361319b65c9b03d07b387561baf0341f3783166b41f51b30166b6a381d8ef2ccdc7dab141fd7db58bf16f68850a6ed6692ad9f055eff6304fce52acb
7
- data.tar.gz: 87f9578aad0578a016aa573493adf486aeb9793506522685e1d6c388c028e85483d97a29d420f9257d81a4c3fd33577ffb6b76e331c626eaf1f7b22b717a6c99
6
+ metadata.gz: 8f5c67987554c03af1a0cf2d6a72bc3e225463c33fc5e8b53fbc5689fe05888bcb364f702d771b47f23c1cf356b8c0a01d0c1c39e56afd9931e55c50cdc0d6a7
7
+ data.tar.gz: 8a65504c1ba6e8f7f5088e6dcdbeb132b438781c63efec29eb79f527d0fb18919af1f4570f10be3e0453bea014892c3dd8372388c2f6e8b8a9b536197c3f683a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.2.8)
4
+ query_helper (0.2.13)
5
5
  activerecord (> 5)
6
6
  activesupport (> 5)
7
7
 
@@ -13,7 +13,7 @@ require "query_helper/invalid_query_error"
13
13
 
14
14
  class QueryHelper
15
15
 
16
- attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload
16
+ attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload, :search_field, :search_string, :metadata
17
17
  attr_reader :query
18
18
 
19
19
  def initialize(
@@ -31,7 +31,8 @@ class QueryHelper
31
31
  api_payload: false, # Return the paginated payload or simply return the result array
32
32
  preload: [], # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
33
33
  search_fields: [],
34
- search_string: nil
34
+ search_string: nil,
35
+ metadata: {}
35
36
  )
36
37
  @query = query.class < ActiveRecord::Relation ? query.to_sql : query
37
38
  @model = query.class < ActiveRecord::Relation ? query.base_class : model
@@ -40,6 +41,7 @@ class QueryHelper
40
41
  @sql_sort = sql_sort
41
42
  @page = determine_page(page: page, per_page: per_page)
42
43
  @per_page = determine_per_page(page: page, per_page: per_page)
44
+ set_limit_and_offset()
43
45
  @single_record = single_record
44
46
  @associations = associations
45
47
  @as_json_options = as_json_options
@@ -48,15 +50,7 @@ class QueryHelper
48
50
  @preload = preload
49
51
  @search_fields = search_fields
50
52
  @search_string = search_string
51
-
52
- if @page && @per_page
53
- # Determine limit and offset
54
- limit = @per_page
55
- offset = (@page - 1) * @per_page
56
-
57
- # Merge limit/offset variables into bind_variables
58
- @bind_variables.merge!({limit: limit, offset: offset})
59
- end
53
+ @metadata = metadata
60
54
  end
61
55
 
62
56
  def update(
@@ -69,7 +63,13 @@ class QueryHelper
69
63
  single_record: nil,
70
64
  custom_mappings: nil,
71
65
  preload: [],
72
- search_fields: nil
66
+ search_fields: nil,
67
+ sql_filter: nil,
68
+ sql_sort: nil,
69
+ page: nil,
70
+ per_page: nil,
71
+ search_string: nil,
72
+ metadata: nil
73
73
  )
74
74
  @query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
75
75
  @model = query.class < ActiveRecord::Relation ? query.base_class : model if model || query
@@ -81,6 +81,13 @@ class QueryHelper
81
81
  @custom_mappings = custom_mappings if custom_mappings
82
82
  @preload = preload if preload
83
83
  @search_fields = search_fields if search_fields
84
+ @sql_filter = sql_filter if sql_filter
85
+ @sql_sort = sql_sort if sql_sort
86
+ @search_string = search_string if search_string
87
+ @page = determine_page(page: page, per_page: per_page) if page
88
+ @per_page = determine_per_page(page: page, per_page: per_page) if per_page
89
+ @metadata = metadata if metadata
90
+ set_limit_and_offset()
84
91
  return self
85
92
  end
86
93
 
@@ -110,7 +117,7 @@ class QueryHelper
110
117
 
111
118
  having_clauses = @sql_filter.having_clauses
112
119
  where_clauses = @sql_filter.where_clauses
113
-
120
+
114
121
  if @search_string
115
122
  search_filter = search_filter(column_maps)
116
123
  if search_filter[:placement] == :where
@@ -123,7 +130,7 @@ class QueryHelper
123
130
 
124
131
  # merge the filter bind variables into the query bind variables
125
132
  @bind_variables.merge!(@sql_filter.bind_variables)
126
-
133
+
127
134
  # Execute Sql Query
128
135
  manipulator = SqlManipulator.new(
129
136
  sql: @query,
@@ -186,9 +193,22 @@ class QueryHelper
186
193
  return nil
187
194
  end
188
195
 
196
+ def set_limit_and_offset
197
+ if @page && @per_page
198
+ # Determine limit and offset
199
+ limit = @per_page
200
+ offset = (@page - 1) * @per_page
201
+
202
+ # Merge limit/offset variables into bind_variables
203
+ @bind_variables[:limit] = limit
204
+ @bind_variables[:offset] = offset
205
+ end
206
+ end
207
+
189
208
  def paginated_results
190
209
  { pagination: pagination_results(),
191
- data: @results }
210
+ data: @results,
211
+ metadata: @metadata }
192
212
  end
193
213
 
194
214
  def determine_count
@@ -221,22 +241,22 @@ class QueryHelper
221
241
 
222
242
  def pagination_results
223
243
  # Set pagination params if they aren't provided
224
- @per_page = @count unless @per_page
225
- @page = 1 unless @page
244
+ results_per_page = @per_page || @count
245
+ results_page = @page || 1
226
246
 
227
- total_pages = (@count/(@per_page.nonzero? || 1).to_f).ceil
228
- next_page = @page + 1 if @page.between?(1, total_pages - 1)
229
- previous_page = @page - 1 if @page.between?(2, total_pages)
230
- first_page = @page == 1
231
- last_page = @page == total_pages
232
- out_of_range = !@page.between?(1,total_pages)
247
+ total_pages = (@count/(results_per_page.nonzero? || 1).to_f).ceil
248
+ next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
249
+ previous_page = results_page - 1 if results_page.between?(2, total_pages)
250
+ first_page = results_page == 1
251
+ last_page = results_page == total_pages
252
+ out_of_range = !results_page.between?(1,total_pages)
233
253
 
234
254
  { count: @count,
235
- current_page: @page,
255
+ current_page: results_page,
236
256
  next_page: next_page,
237
257
  previous_page: previous_page,
238
258
  total_pages: total_pages,
239
- per_page: @per_page,
259
+ per_page: results_per_page,
240
260
  first_page: first_page,
241
261
  last_page: last_page,
242
262
  out_of_range: out_of_range }
@@ -9,10 +9,22 @@ class QueryHelper
9
9
  @query_helper
10
10
  end
11
11
 
12
+ def query_helper_with_no_pagination
13
+ QueryHelper.new(**query_helper_params_no_pagination)
14
+ end
15
+
12
16
  def create_query_helper
13
17
  @query_helper = QueryHelper.new(**query_helper_params, api_payload: true)
14
18
  end
15
19
 
20
+ def create_query_helper_with_no_pagination
21
+ @query_helper = query_helper_with_no_pagination()
22
+ end
23
+
24
+ def reload_query_params(query_helper=@query_helper)
25
+ query_helper.update(**query_helper_params)
26
+ end
27
+
16
28
  def create_query_helper_filter
17
29
  filter_values = params[:filter].permit!.to_h
18
30
  QueryHelper::SqlFilter.new(filter_values: filter_values)
@@ -27,9 +39,14 @@ class QueryHelper
27
39
  end
28
40
 
29
41
  def query_helper_params
30
- helpers = {}
42
+ helpers = query_helper_params_no_pagination
31
43
  helpers[:page] = params[:page] if params[:page]
32
44
  helpers[:per_page] = params[:per_page] if params[:per_page]
45
+ helpers
46
+ end
47
+
48
+ def query_helper_params_no_pagination
49
+ helpers = {}
33
50
  helpers[:sql_filter] = create_query_helper_filter() if params[:filter]
34
51
  helpers[:sql_sort] = create_query_helper_sort() if params[:sort]
35
52
  helpers[:associations] = create_query_helper_associations() if params[:include]
@@ -55,7 +55,7 @@ class QueryHelper
55
55
 
56
56
  def insert_order_by_and_limit_clause
57
57
  @sql.slice!(@parser.limit_clause) if @parser.limit_included? # remove existing limit clause
58
- @sql.slice!(@parser.order_by_clause) if @parser.order_by_included? # remove existing order by clause
58
+ @sql.slice!(@parser.order_by_clause) if @parser.order_by_included? && @order_by_clauses.length > 0 # remove existing order by clause
59
59
  @sql += " order by #{@order_by_clauses.join(", ")} " if @order_by_clauses.length > 0
60
60
  @sql += " limit :limit offset :offset " if @include_limit_clause
61
61
  end
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.13"
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.2.8
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan McDaniel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-22 00:00:00.000000000 Z
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler