query_helper 0.2.8 → 0.2.9

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: ad62903d54b1df9280b687c57da64e7a8a94e0919c156a1176f517248c69065e
4
+ data.tar.gz: d6c25ed277261ea00685c57bee58432bbc208d76f6e4b42d9ce3401173e3f35a
5
5
  SHA512:
6
- metadata.gz: 2d49fc27361319b65c9b03d07b387561baf0341f3783166b41f51b30166b6a381d8ef2ccdc7dab141fd7db58bf16f68850a6ed6692ad9f055eff6304fce52acb
7
- data.tar.gz: 87f9578aad0578a016aa573493adf486aeb9793506522685e1d6c388c028e85483d97a29d420f9257d81a4c3fd33577ffb6b76e331c626eaf1f7b22b717a6c99
6
+ metadata.gz: abeb83f9d294ee0a0d0dcc7ed1c702b9f8d2858ce8da6c82b5cd8fc12c45634865cb68cdfc994851362fc4180d38d31b992bda0f35e0ec1b21c0462a29b61999
7
+ data.tar.gz: cddfa33a462fb4391bb736a39405309fbb980ab14e72040e4924fa898c0d95aac69bf4635b6f312df694480827969a715d4f77452aa6303c9f90236f2e0688f7
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.2.8)
4
+ query_helper (0.2.9)
5
5
  activerecord (> 5)
6
6
  activesupport (> 5)
7
7
 
@@ -40,6 +40,7 @@ class QueryHelper
40
40
  @sql_sort = sql_sort
41
41
  @page = determine_page(page: page, per_page: per_page)
42
42
  @per_page = determine_per_page(page: page, per_page: per_page)
43
+ set_limit_and_offset()
43
44
  @single_record = single_record
44
45
  @associations = associations
45
46
  @as_json_options = as_json_options
@@ -48,15 +49,6 @@ class QueryHelper
48
49
  @preload = preload
49
50
  @search_fields = search_fields
50
51
  @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
60
52
  end
61
53
 
62
54
  def update(
@@ -69,7 +61,12 @@ class QueryHelper
69
61
  single_record: nil,
70
62
  custom_mappings: nil,
71
63
  preload: [],
72
- search_fields: nil
64
+ search_fields: nil,
65
+ sql_filter: nil,
66
+ sql_sort: nil,
67
+ page: nil,
68
+ per_page: nil,
69
+ search_string: nil
73
70
  )
74
71
  @query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
75
72
  @model = query.class < ActiveRecord::Relation ? query.base_class : model if model || query
@@ -81,6 +78,12 @@ class QueryHelper
81
78
  @custom_mappings = custom_mappings if custom_mappings
82
79
  @preload = preload if preload
83
80
  @search_fields = search_fields if search_fields
81
+ @sql_filter = sql_filter if sql_filter
82
+ @sql_sort = sql_sort if sql_sort
83
+ @search_string = search_string if search_string
84
+ @page = determine_page(page: page, per_page: per_page) if page
85
+ @per_page = determine_per_page(page: page, per_page: per_page) if per_page
86
+ set_limit_and_offset()
84
87
  return self
85
88
  end
86
89
 
@@ -110,7 +113,7 @@ class QueryHelper
110
113
 
111
114
  having_clauses = @sql_filter.having_clauses
112
115
  where_clauses = @sql_filter.where_clauses
113
-
116
+
114
117
  if @search_string
115
118
  search_filter = search_filter(column_maps)
116
119
  if search_filter[:placement] == :where
@@ -123,7 +126,7 @@ class QueryHelper
123
126
 
124
127
  # merge the filter bind variables into the query bind variables
125
128
  @bind_variables.merge!(@sql_filter.bind_variables)
126
-
129
+
127
130
  # Execute Sql Query
128
131
  manipulator = SqlManipulator.new(
129
132
  sql: @query,
@@ -186,6 +189,18 @@ class QueryHelper
186
189
  return nil
187
190
  end
188
191
 
192
+ def set_limit_and_offset
193
+ if @page && @per_page
194
+ # Determine limit and offset
195
+ limit = @per_page
196
+ offset = (@page - 1) * @per_page
197
+
198
+ # Merge limit/offset variables into bind_variables
199
+ @bind_variables[:limit] = limit
200
+ @bind_variables[:offset] = offset
201
+ end
202
+ end
203
+
189
204
  def paginated_results
190
205
  { pagination: pagination_results(),
191
206
  data: @results }
@@ -221,22 +236,22 @@ class QueryHelper
221
236
 
222
237
  def pagination_results
223
238
  # Set pagination params if they aren't provided
224
- @per_page = @count unless @per_page
225
- @page = 1 unless @page
239
+ results_per_page = @per_page || @count
240
+ results_page = @page || 1
226
241
 
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)
242
+ total_pages = (@count/(results_per_page.nonzero? || 1).to_f).ceil
243
+ next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
244
+ previous_page = results_page - 1 if results_page.between?(2, total_pages)
245
+ first_page = results_page == 1
246
+ last_page = results_page == total_pages
247
+ out_of_range = !results_page.between?(1,total_pages)
233
248
 
234
249
  { count: @count,
235
- current_page: @page,
250
+ current_page: results_page,
236
251
  next_page: next_page,
237
252
  previous_page: previous_page,
238
253
  total_pages: total_pages,
239
- per_page: @per_page,
254
+ per_page: results_per_page,
240
255
  first_page: first_page,
241
256
  last_page: last_page,
242
257
  out_of_range: out_of_range }
@@ -9,10 +9,18 @@ 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 reload_query_params(query_helper=@query_helper)
21
+ query_helper.update(**query_helper_params)
22
+ end
23
+
16
24
  def create_query_helper_filter
17
25
  filter_values = params[:filter].permit!.to_h
18
26
  QueryHelper::SqlFilter.new(filter_values: filter_values)
@@ -27,9 +35,14 @@ class QueryHelper
27
35
  end
28
36
 
29
37
  def query_helper_params
30
- helpers = {}
38
+ helpers = query_helper_params_no_pagination
31
39
  helpers[:page] = params[:page] if params[:page]
32
40
  helpers[:per_page] = params[:per_page] if params[:per_page]
41
+ helpers
42
+ end
43
+
44
+ def query_helper_params_no_pagination
45
+ helpers = {}
33
46
  helpers[:sql_filter] = create_query_helper_filter() if params[:filter]
34
47
  helpers[:sql_sort] = create_query_helper_sort() if params[:sort]
35
48
  helpers[:associations] = create_query_helper_associations() if params[:include]
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
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.9
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-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler