query_helper 0.2.15 → 0.2.20

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: a322338c9c9fd1675506690061efb3e40715f8efb438a6a552335cb7948a3e86
4
- data.tar.gz: 6da1ce413feabd5f72b48fe599b6ce020e6288830af637e9c330a2083a49e91f
3
+ metadata.gz: 20ef59e4a5c449b359942acdf2577f6e756aa24df46175ab7c066a878d737a3e
4
+ data.tar.gz: 155d7aedaeb4b2da6ae1984217307dbb7144e19565cc28d1fdd920879adb4c6b
5
5
  SHA512:
6
- metadata.gz: b0257379a11c98ed103f6cdd9751d341b815bb817789ee0b8054438c920813c94d2b25c4c5d26a75de53734e8ccf78b6a7077516011b3fd021a561a12681fda5
7
- data.tar.gz: f75e4d2e80c083dc579829b46443b1dc6661a8fd57b648ed1d2ebedda14c016cf5d78a82ba388998e320d42d7ac065d2f7644b0c266076ea33a6afe595400800
6
+ metadata.gz: 1ee0d63818c17954054138587d5d6a49a175119bb37432e4c99952f638b8f39c4b81130e14eaf9e51e6715cb3fcd4527b23382c2caef7721f05dbe874cab07f6
7
+ data.tar.gz: 2295b50557aa7d74c9d713c4cfd4732f479fca0c026f101e9969562c7c8f68fe762324a4215c40cc722abfa0129e062ef995a504b3f060f6842ce27013cf9185
data/Gemfile.lock CHANGED
@@ -1,56 +1,58 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.2.15)
4
+ query_helper (0.2.20)
5
5
  activerecord (> 5)
6
6
  activesupport (> 5)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionpack (6.0.2.2)
12
- actionview (= 6.0.2.2)
13
- activesupport (= 6.0.2.2)
11
+ actionpack (6.0.3.5)
12
+ actionview (= 6.0.3.5)
13
+ activesupport (= 6.0.3.5)
14
14
  rack (~> 2.0, >= 2.0.8)
15
15
  rack-test (>= 0.6.3)
16
16
  rails-dom-testing (~> 2.0)
17
17
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
- actionview (6.0.2.2)
19
- activesupport (= 6.0.2.2)
18
+ actionview (6.0.3.5)
19
+ activesupport (= 6.0.3.5)
20
20
  builder (~> 3.1)
21
21
  erubi (~> 1.4)
22
22
  rails-dom-testing (~> 2.0)
23
23
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
- activemodel (6.0.2.2)
25
- activesupport (= 6.0.2.2)
26
- activerecord (6.0.2.2)
27
- activemodel (= 6.0.2.2)
28
- activesupport (= 6.0.2.2)
29
- activesupport (6.0.2.2)
24
+ activemodel (6.0.3.5)
25
+ activesupport (= 6.0.3.5)
26
+ activerecord (6.0.3.5)
27
+ activemodel (= 6.0.3.5)
28
+ activesupport (= 6.0.3.5)
29
+ activesupport (6.0.3.5)
30
30
  concurrent-ruby (~> 1.0, >= 1.0.2)
31
31
  i18n (>= 0.7, < 2)
32
32
  minitest (~> 5.1)
33
33
  tzinfo (~> 1.1)
34
- zeitwerk (~> 2.2)
34
+ zeitwerk (~> 2.2, >= 2.2.2)
35
35
  builder (3.2.4)
36
- byebug (11.1.1)
36
+ byebug (11.1.3)
37
37
  concurrent-ruby (1.1.6)
38
38
  crass (1.0.6)
39
- diff-lcs (1.3)
39
+ diff-lcs (1.4.4)
40
40
  erubi (1.9.0)
41
41
  faker (1.9.6)
42
42
  i18n (>= 0.7)
43
- i18n (1.8.2)
43
+ i18n (1.8.5)
44
44
  concurrent-ruby (~> 1.0)
45
- loofah (2.4.0)
45
+ loofah (2.6.0)
46
46
  crass (~> 1.0.2)
47
47
  nokogiri (>= 1.5.9)
48
48
  method_source (1.0.0)
49
- mini_portile2 (2.4.0)
50
- minitest (5.14.0)
51
- nokogiri (1.10.9)
52
- mini_portile2 (~> 2.4.0)
53
- rack (2.2.2)
49
+ mini_portile2 (2.5.0)
50
+ minitest (5.14.1)
51
+ nokogiri (1.11.1)
52
+ mini_portile2 (~> 2.5.0)
53
+ racc (~> 1.4)
54
+ racc (1.5.2)
55
+ rack (2.2.3)
54
56
  rack-test (1.1.0)
55
57
  rack (>= 1.0, < 3)
56
58
  rails-dom-testing (2.0.3)
@@ -58,9 +60,9 @@ GEM
58
60
  nokogiri (>= 1.6)
59
61
  rails-html-sanitizer (1.3.0)
60
62
  loofah (~> 2.3)
61
- railties (6.0.2.2)
62
- actionpack (= 6.0.2.2)
63
- activesupport (= 6.0.2.2)
63
+ railties (6.0.3.5)
64
+ actionpack (= 6.0.3.5)
65
+ activesupport (= 6.0.3.5)
64
66
  method_source
65
67
  rake (>= 0.8.7)
66
68
  thor (>= 0.20.3, < 2.0)
@@ -69,15 +71,15 @@ GEM
69
71
  rspec-core (~> 3.9.0)
70
72
  rspec-expectations (~> 3.9.0)
71
73
  rspec-mocks (~> 3.9.0)
72
- rspec-core (3.9.1)
73
- rspec-support (~> 3.9.1)
74
- rspec-expectations (3.9.1)
74
+ rspec-core (3.9.2)
75
+ rspec-support (~> 3.9.3)
76
+ rspec-expectations (3.9.2)
75
77
  diff-lcs (>= 1.2.0, < 2.0)
76
78
  rspec-support (~> 3.9.0)
77
79
  rspec-mocks (3.9.1)
78
80
  diff-lcs (>= 1.2.0, < 2.0)
79
81
  rspec-support (~> 3.9.0)
80
- rspec-rails (4.0.0)
82
+ rspec-rails (4.0.1)
81
83
  actionpack (>= 4.2)
82
84
  activesupport (>= 4.2)
83
85
  railties (>= 4.2)
@@ -85,13 +87,13 @@ GEM
85
87
  rspec-expectations (~> 3.9)
86
88
  rspec-mocks (~> 3.9)
87
89
  rspec-support (~> 3.9)
88
- rspec-support (3.9.2)
90
+ rspec-support (3.9.3)
89
91
  sqlite3 (1.3.13)
90
92
  thor (1.0.1)
91
93
  thread_safe (0.3.6)
92
- tzinfo (1.2.6)
94
+ tzinfo (1.2.7)
93
95
  thread_safe (~> 0.1)
94
- zeitwerk (2.3.0)
96
+ zeitwerk (2.4.0)
95
97
 
96
98
  PLATFORMS
97
99
  ruby
@@ -99,7 +101,7 @@ PLATFORMS
99
101
  DEPENDENCIES
100
102
  actionpack
101
103
  activesupport
102
- bundler (~> 1.17)
104
+ bundler (~> 2.2.6)
103
105
  byebug
104
106
  faker (~> 1.9.3)
105
107
  query_helper!
@@ -109,4 +111,4 @@ DEPENDENCIES
109
111
  sqlite3 (~> 1.3.6)
110
112
 
111
113
  BUNDLED WITH
112
- 1.17.3
114
+ 2.2.6
data/lib/query_helper.rb CHANGED
@@ -66,6 +66,7 @@ class QueryHelper
66
66
  search_fields: nil,
67
67
  sql_filter: nil,
68
68
  sql_sort: nil,
69
+ sort_tiebreak: nil,
69
70
  page: nil,
70
71
  per_page: nil,
71
72
  search_string: nil,
@@ -80,13 +81,14 @@ class QueryHelper
80
81
  @as_json_options = as_json_options if as_json_options
81
82
  @custom_mappings = custom_mappings if custom_mappings
82
83
  @preload = preload if preload
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
84
+ @search_fields = search_fields if search_fields
85
+ @sql_filter = sql_filter if sql_filter
86
+ @sql_sort = sql_sort if sql_sort
87
+ @sql_sort.sort_tiebreak = sort_tiebreak if sort_tiebreak
88
+ @search_string = search_string if search_string
87
89
  @page = determine_page(page: page, per_page: per_page) if page
88
90
  @per_page = determine_per_page(page: page, per_page: per_page) if per_page
89
- @metadata = metadata if metadata
91
+ @metadata = metadata if metadata
90
92
  set_limit_and_offset()
91
93
  return self
92
94
  end
@@ -116,21 +118,21 @@ class QueryHelper
116
118
  @sql_filter.create_filters()
117
119
 
118
120
  having_clauses = @sql_filter.having_clauses
119
- where_clauses = @sql_filter.where_clauses
120
-
121
+ where_clauses = @sql_filter.where_clauses
122
+
121
123
  if @search_string
122
124
  search_filter = search_filter(column_maps)
123
125
  if search_filter[:placement] == :where
124
126
  where_clauses << search_filter[:filter]
125
- else
127
+ else
126
128
  having_clauses << search_filter[:filter]
127
129
  end
128
- end
130
+ end
129
131
 
130
132
 
131
133
  # merge the filter bind variables into the query bind variables
132
134
  @bind_variables.merge!(@sql_filter.bind_variables)
133
-
135
+
134
136
  # Execute Sql Query
135
137
  manipulator = SqlManipulator.new(
136
138
  sql: @query,
@@ -152,7 +154,7 @@ class QueryHelper
152
154
  return query if @bind_variables.length == 0
153
155
  begin
154
156
  return @model.sanitize_sql_array([query, @bind_variables])
155
- rescue NoMethodError
157
+ rescue NoMethodError
156
158
  # sanitize_sql_array is a protected method before Rails v5.2.3
157
159
  return @model.send(:sanitize_sql_array, [query, @bind_variables])
158
160
  end
@@ -181,14 +183,14 @@ class QueryHelper
181
183
 
182
184
  def pagination_results(count=@count)
183
185
  # Set pagination params if they aren't provided
184
- results_per_page = @per_page || count
185
- results_page = @page || 1
186
+ results_per_page = @per_page || count
187
+ results_page = @page || 1
186
188
 
187
- total_pages = (count/(results_per_page.nonzero? || 1).to_f).ceil
189
+ total_pages = (count.to_i/(results_per_page.nonzero? || 1).to_f).ceil
188
190
  next_page = results_page + 1 if results_page.between?(1, total_pages - 1)
189
191
  previous_page = results_page - 1 if results_page.between?(2, total_pages)
190
192
  first_page = results_page == 1
191
- last_page = results_page == total_pages
193
+ last_page = results_page >= total_pages
192
194
  out_of_range = !results_page.between?(1,total_pages)
193
195
 
194
196
  { count: count,
@@ -205,16 +207,16 @@ class QueryHelper
205
207
  private
206
208
 
207
209
  def determine_page(page:, per_page:)
208
- return page.to_i if page
209
- return 1 if !page && per_page
210
+ return page.to_i if page
211
+ return 1 if !page && per_page
210
212
  return nil
211
- end
213
+ end
212
214
 
213
215
  def determine_per_page(page:, per_page:)
214
- return per_page.to_i if per_page
215
- return 100 if !per_page && page
216
+ return per_page.to_i if per_page
217
+ return 100 if !per_page && page
216
218
  return nil
217
- end
219
+ end
218
220
 
219
221
  def set_limit_and_offset
220
222
  if @page && @per_page
@@ -226,7 +228,7 @@ class QueryHelper
226
228
  @bind_variables[:limit] = limit
227
229
  @bind_variables[:offset] = offset
228
230
  end
229
- end
231
+ end
230
232
 
231
233
  def paginated_results
232
234
  { pagination: pagination_results(),
@@ -273,16 +275,16 @@ class QueryHelper
273
275
  def search_filter(column_maps)
274
276
  raise ArgumentError.new("search_fields not defined") unless @search_fields.length > 0
275
277
  placement = :where
276
- maps = column_maps.select do |cm|
278
+ maps = column_maps.select do |cm|
277
279
  placement = :having if cm.aggregate
278
280
  @search_fields.include? cm.alias_name
279
- end
281
+ end
280
282
  bind_variable = ('a'..'z').to_a.shuffle[0,20].join.to_sym
281
283
  @bind_variables[bind_variable] = "%#{@search_string}%"
282
- filter = "#{maps.map{|m| "#{m.sql_expression}::varchar"}.join(" || ")} ilike :#{bind_variable}"
284
+ filter = "#{maps.map{|m| "coalesce(#{m.sql_expression}::varchar, '')"}.join(" || ")} ilike :#{bind_variable}"
283
285
  return {
284
286
  filter: filter,
285
287
  placement: placement
286
288
  }
287
- end
289
+ end
288
290
  end
@@ -13,7 +13,7 @@ class QueryHelper
13
13
 
14
14
  model.attribute_names.each do |attribute|
15
15
  if maps.select{|x| x.alias_name == attribute}.empty?
16
- maps << ColumnMap.new(alias_name: attribute, sql_expression: "#{model.to_s.underscore.pluralize}.#{attribute}")
16
+ maps << ColumnMap.new(alias_name: attribute, sql_expression: "#{model.table_name.pluralize}.#{attribute}")
17
17
  end
18
18
  end
19
19
 
@@ -31,7 +31,7 @@ class QueryHelper
31
31
  end
32
32
 
33
33
  def create_query_helper_sort
34
- QueryHelper::SqlSort.new(sort_string: params[:sort])
34
+ QueryHelper::SqlSort.new(sort_string: params[:sort], sort_tiebreak: params[:sort_tiebreak])
35
35
  end
36
36
 
37
37
  def create_query_helper_associations
@@ -48,7 +48,7 @@ class QueryHelper
48
48
  def query_helper_params_no_pagination
49
49
  helpers = {}
50
50
  helpers[:sql_filter] = create_query_helper_filter() if params[:filter]
51
- helpers[:sql_sort] = create_query_helper_sort() if params[:sort]
51
+ helpers[:sql_sort] = create_query_helper_sort() if params[:sort] || params[:sort_tiebreak]
52
52
  helpers[:associations] = create_query_helper_associations() if params[:include]
53
53
  helpers[:search_string] = params[:search_for] if params[:search_for]
54
54
  helpers
@@ -3,22 +3,33 @@ require "query_helper/invalid_query_error"
3
3
  class QueryHelper
4
4
  class SqlSort
5
5
 
6
- attr_accessor :column_maps, :select_strings
6
+ attr_accessor :column_maps, :select_strings, :sort_tiebreak
7
7
 
8
- def initialize(sort_string: "", column_maps: [])
8
+ def initialize(sort_string: "", sort_tiebreak: "", column_maps: [])
9
9
  @sort_string = sort_string
10
10
  @column_maps = column_maps
11
+ @sort_tiebreak = sort_tiebreak
11
12
  @select_strings = []
12
13
  end
13
14
 
14
15
  def parse_sort_string
16
+ return [] if @sort_string.blank? && @sort_tiebreak.blank?
17
+
18
+ return attributes_sql_expression(@sort_tiebreak) if @sort_string.blank?
19
+
20
+ sql_strings = attributes_sql_expression(@sort_string)
21
+ return sql_strings if @sort_tiebreak.blank?
22
+
23
+ sql_strings + attributes_sql_expression(@sort_tiebreak)
24
+ end
25
+
26
+ def attributes_sql_expression(sort_attribute)
15
27
  sql_strings = []
16
- sorts = @sort_string.split(",")
28
+ sorts = sort_attribute.split(",")
17
29
  sorts.each_with_index do |sort, index|
18
30
  sort_alias = sort.split(":")[0]
19
31
  direction = sort.split(":")[1]
20
32
  modifier = sort.split(":")[2]
21
-
22
33
  begin
23
34
  sql_expression = @column_maps.find{ |m| m.alias_name == sort_alias }.sql_expression
24
35
  rescue NoMethodError => e
@@ -45,8 +56,7 @@ class QueryHelper
45
56
 
46
57
  sql_strings << "#{sql_expression} #{direction}"
47
58
  end
48
-
49
- return sql_strings
59
+ sql_strings
50
60
  end
51
61
  end
52
62
  end
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.2.15"
2
+ VERSION = "0.2.20"
3
3
  end
data/query_helper.gemspec CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
37
  spec.require_paths = ["lib"]
38
38
 
39
- spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "bundler", "~> 2.2.6"
40
40
  spec.add_development_dependency "rake", "~> 13.0"
41
41
  spec.add_development_dependency "rspec", "~> 3.0"
42
42
  spec.add_development_dependency "sqlite3", "~> 1.3.6"
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.15
4
+ version: 0.2.20
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-07-22 00:00:00.000000000 Z
11
+ date: 2021-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.2.6
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: 2.2.6
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  - !ruby/object:Gem::Version
215
215
  version: '0'
216
216
  requirements: []
217
- rubygems_version: 3.0.3
217
+ rubygems_version: 3.1.4
218
218
  signing_key:
219
219
  specification_version: 4
220
220
  summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.