pattern_query_helper 0.2.7 → 0.2.11

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: 6c3503df41d4640984dac6772f15d439c23b8fd3904514d58fb98b2af7efbc45
4
- data.tar.gz: d40370dd2f2e9c2da4b0993a7ac21d08dbc512b76bd2da031ff1e3ce56605507
3
+ metadata.gz: 110f333cc3a7f82ece7bce90a4eb59d0084d57a90259f17594ae46ef63279d45
4
+ data.tar.gz: ef87d0577d5f011a4a491bec04b892a33bc1ced8df47dd801a2bc4aea3cb9f5d
5
5
  SHA512:
6
- metadata.gz: 30a723aca45d1ceb20f914826367ca92bfc6381ebe621d823163c0cc3eb6615b4398dc1e4147f4dad4b40b41c8c71bb52191ab8909d384b6ff345caf1982f224
7
- data.tar.gz: 8c8c2855b31e831ed3b22c6c1f2709e15f2ee332515becf48e8a31637a82cf1ded7712155444d89e740bde76a0f62f53a765e61b4686930870507c755e63e8a7
6
+ metadata.gz: 5f934ef37549782fb366945f0e3aee027902927d53d1de11c77398bc8aef4cffa1f7f99ff4ac64cde752716e7e466f157f860c3a5d70a384910108850e4691cf
7
+ data.tar.gz: 3ba6c72d6d6d44b25d2384eb8b9db304436b3f93e265be9f44920529a84e0d58c18c72d37610c930e7353877a26b023c4b75b8fa53204118451af50188de8ec8
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  sudo: false
2
+ dist: xenial
2
3
  language: ruby
3
4
  cache: bundler
4
5
  rvm:
5
- - 2.4.1
6
+ - 2.4.5
6
7
  before_install: gem install bundler -v 1.16.6
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in pattern_query_helper.gemspec
6
6
  gemspec
7
+
8
+ gem 'sqlite3', '~> 1.4'
data/Gemfile.lock CHANGED
@@ -1,37 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pattern_query_helper (0.2.6)
5
- activerecord (~> 5.0)
4
+ pattern_query_helper (0.2.11)
5
+ activerecord (>= 5.0)
6
6
  kaminari (~> 1.1.1)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionview (5.2.3)
12
- activesupport (= 5.2.3)
11
+ actionview (6.1.4.1)
12
+ activesupport (= 6.1.4.1)
13
13
  builder (~> 3.1)
14
14
  erubi (~> 1.4)
15
15
  rails-dom-testing (~> 2.0)
16
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
17
- activemodel (5.2.3)
18
- activesupport (= 5.2.3)
19
- activerecord (5.2.3)
20
- activemodel (= 5.2.3)
21
- activesupport (= 5.2.3)
22
- arel (>= 9.0)
23
- activesupport (5.2.3)
16
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
17
+ activemodel (6.1.4.1)
18
+ activesupport (= 6.1.4.1)
19
+ activerecord (6.1.4.1)
20
+ activemodel (= 6.1.4.1)
21
+ activesupport (= 6.1.4.1)
22
+ activesupport (6.1.4.1)
24
23
  concurrent-ruby (~> 1.0, >= 1.0.2)
25
- i18n (>= 0.7, < 2)
26
- minitest (~> 5.1)
27
- tzinfo (~> 1.1)
28
- arel (9.0.0)
29
- builder (3.2.3)
24
+ i18n (>= 1.6, < 2)
25
+ minitest (>= 5.1)
26
+ tzinfo (~> 2.0)
27
+ zeitwerk (~> 2.3)
28
+ builder (3.2.4)
30
29
  byebug (11.0.1)
31
30
  concurrent-ruby (1.1.4)
32
- crass (1.0.4)
31
+ crass (1.0.6)
33
32
  diff-lcs (1.3)
34
- erubi (1.8.0)
33
+ erubi (1.10.0)
35
34
  faker (1.9.3)
36
35
  i18n (>= 0.7)
37
36
  i18n (1.6.0)
@@ -48,18 +47,18 @@ GEM
48
47
  activerecord
49
48
  kaminari-core (= 1.1.1)
50
49
  kaminari-core (1.1.1)
51
- loofah (2.2.3)
50
+ loofah (2.12.0)
52
51
  crass (~> 1.0.2)
53
52
  nokogiri (>= 1.5.9)
54
- mini_portile2 (2.4.0)
55
- minitest (5.11.3)
56
- nokogiri (1.10.3)
57
- mini_portile2 (~> 2.4.0)
53
+ minitest (5.14.4)
54
+ nokogiri (1.12.3-x86_64-darwin)
55
+ racc (~> 1.4)
56
+ racc (1.5.2)
58
57
  rails-dom-testing (2.0.3)
59
58
  activesupport (>= 4.2.0)
60
59
  nokogiri (>= 1.6)
61
- rails-html-sanitizer (1.0.4)
62
- loofah (~> 2.2, >= 2.2.2)
60
+ rails-html-sanitizer (1.4.1)
61
+ loofah (~> 2.3)
63
62
  rake (10.5.0)
64
63
  rspec (3.8.0)
65
64
  rspec-core (~> 3.8.0)
@@ -74,10 +73,10 @@ GEM
74
73
  diff-lcs (>= 1.2.0, < 2.0)
75
74
  rspec-support (~> 3.8.0)
76
75
  rspec-support (3.8.0)
77
- sqlite3 (1.3.13)
78
- thread_safe (0.3.6)
79
- tzinfo (1.2.5)
80
- thread_safe (~> 0.1)
76
+ sqlite3 (1.4.2)
77
+ tzinfo (2.0.4)
78
+ concurrent-ruby (~> 1.0)
79
+ zeitwerk (2.4.2)
81
80
 
82
81
  PLATFORMS
83
82
  ruby
@@ -89,7 +88,7 @@ DEPENDENCIES
89
88
  pattern_query_helper!
90
89
  rake (~> 10.0)
91
90
  rspec (~> 3.0)
92
- sqlite3 (~> 1.3.6)
91
+ sqlite3 (~> 1.4)
93
92
 
94
93
  BUNDLED WITH
95
94
  1.17.3
@@ -1,13 +1,16 @@
1
1
  module PatternQueryHelper
2
2
  class Filtering
3
- def self.create_filters(filters, valid_columns=nil, symbol_prefix="")
3
+ def self.create_filters(filters:, valid_columns_map: nil, symbol_prefix: "", include_where: true)
4
4
  filters ||= {}
5
- filter_string = "true = true"
5
+ all_conditions = []
6
6
  filter_params = {}
7
7
  filter_array = []
8
8
  filters.each do |filter_attribute, criteria|
9
- if valid_columns
10
- raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless valid_columns.include? filter_attribute
9
+ if valid_columns_map
10
+ raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless valid_columns_map[filter_attribute]
11
+ filter_column = valid_columns_map[filter_attribute]
12
+ else
13
+ filter_column = filter_attribute
11
14
  end
12
15
  criteria.each do |operator_code, criterion|
13
16
  filter_symbol = "#{symbol_prefix}#{filter_attribute}_#{operator_code}"
@@ -25,22 +28,22 @@ module PatternQueryHelper
25
28
  when "noteql"
26
29
  operator = "!="
27
30
  when "like"
28
- modified_filter_attribute = "lower(#{filter_attribute})"
31
+ modified_filter_column = "lower(#{filter_column})"
29
32
  operator = "like"
30
33
  criterion.downcase!
31
34
  when "in"
32
35
  operator = "in (:#{filter_symbol})"
33
- # if criterion are anything but numbers, downcase the filter_attribute
36
+ # if criterion are anything but numbers, downcase the filter_column
34
37
  if criterion.scan(/[^\d|,|\s]/).any?
35
- modified_filter_attribute = "lower(#{filter_attribute})"
38
+ modified_filter_column = "lower(#{filter_column})"
36
39
  end
37
40
  criterion = criterion.downcase.split(",")
38
41
  filter_symbol_already_embedded = true
39
42
  when "notin"
40
43
  operator = "not in (:#{filter_symbol})"
41
- # if criterion are anything but numbers, downcase the filter_attribute
44
+ # if criterion are anything but numbers, downcase the filter_column
42
45
  if criterion.scan(/[^\d|,|\s]/).any?
43
- modified_filter_attribute = "lower(#{filter_attribute})"
46
+ modified_filter_column = "lower(#{filter_column})"
44
47
  end
45
48
  criterion = criterion.downcase.split(",")
46
49
  filter_symbol_already_embedded = true
@@ -50,9 +53,10 @@ module PatternQueryHelper
50
53
  else
51
54
  raise ArgumentError.new("Invalid operator code '#{operator_code}' on '#{filter_attribute}' filter")
52
55
  end
53
- filter_column = modified_filter_attribute || filter_attribute
54
- filter_string = "#{filter_string} and #{filter_column} #{operator}"
55
- filter_string << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
56
+ filter_column = modified_filter_column || filter_column
57
+ condition = "#{filter_column} #{operator}"
58
+ condition << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
59
+ all_conditions << condition
56
60
  filter_params["#{filter_symbol}"] = criterion unless filter_symbol.blank?
57
61
  filter_array << {
58
62
  column: filter_attribute,
@@ -63,6 +67,12 @@ module PatternQueryHelper
63
67
  end
64
68
  end
65
69
 
70
+ if include_where
71
+ filter_string = "where " + all_conditions.join("\n and ") unless all_conditions.empty?
72
+ else
73
+ filter_string = all_conditions.empty? ? "1 = 1" : all_conditions.join("\n and ")
74
+ end
75
+
66
76
  {
67
77
  filter_string: filter_string,
68
78
  filter_params: filter_params,
@@ -1,14 +1,12 @@
1
1
  module PatternQueryHelper
2
2
  class Sql
3
+ QUERY_COUNT_COLUMN = "_query_full_count".freeze
3
4
  def self.sql_query(config)
4
5
  model = config[:model]
5
- query = config[:query]
6
6
  query_params = config[:query_params] || {}
7
7
  page = config[:page]
8
8
  per_page = config[:per_page]
9
- filter_string = config[:filter_string]
10
9
  filter_params = config[:filter_params] || {}
11
- sort_string = config[:sort_string]
12
10
 
13
11
  if page && per_page
14
12
  query_params[:limit] = per_page
@@ -16,46 +14,44 @@ module PatternQueryHelper
16
14
  limit = "limit :limit offset :offset"
17
15
  end
18
16
 
17
+ full_count_join = "cross join (select count(*) as #{QUERY_COUNT_COLUMN} from filtered_query) as filtered_query_count" if page || per_page
19
18
  query_params = query_params.merge(filter_params).symbolize_keys
20
- sort_string = "order by #{sort_string}" if !sort_string.blank?
21
- filter_string = "where #{filter_string}" if !filter_string.blank?
22
19
 
23
20
  sql = %(
24
- with query as (#{query})
25
- select *
26
- from query
27
- #{filter_string}
28
- #{sort_string}
29
- #{limit}
30
- )
21
+ with filtered_query as (#{filtered_query(config)})
22
+ select *
23
+ from filtered_query
24
+ #{full_count_join}
25
+ #{limit}
26
+ )
31
27
 
32
28
  model.find_by_sql([sql, query_params])
33
29
  end
34
30
 
35
- def self.sql_query_count(config)
36
- model = config[:model]
31
+ def self.sql_query_count(sql_query_results)
32
+ sql_query_results.empty? ? 0 : sql_query_results.first[QUERY_COUNT_COLUMN]
33
+ end
34
+
35
+ def self.single_record_query(config)
36
+ results = sql_query(config)
37
+ results.first
38
+ end
39
+
40
+ private
41
+
42
+ def self.filtered_query(config)
37
43
  query = config[:query]
38
- query_params = config[:query_params] || {}
39
44
  filter_string = config[:filter_string]
40
- filter_params = config[:filter_params] || {}
41
-
42
- query_params = query_params.merge(filter_params).symbolize_keys
43
- filter_string = "where #{filter_string}" if !filter_string.blank?
45
+ sort_string = config[:sort_string]
46
+ sort_string = "order by #{sort_string}" if !sort_string.blank?
44
47
 
45
- count_sql = %(
48
+ sql = %(
46
49
  with query as (#{query})
47
- select count(*) as count
50
+ select *
48
51
  from query
49
52
  #{filter_string}
53
+ #{sort_string}
50
54
  )
51
-
52
- model.find_by_sql([count_sql, query_params]).first["count"]
53
- end
54
-
55
- def self.single_record_query(config)
56
- results = sql_query(config)
57
- results.first
58
55
  end
59
-
60
56
  end
61
57
  end
@@ -1,3 +1,3 @@
1
1
  module PatternQueryHelper
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.11"
3
3
  end
@@ -39,7 +39,8 @@ module PatternQueryHelper
39
39
 
40
40
  data = PatternQueryHelper::Sql.sql_query(query_config)
41
41
  data = PatternQueryHelper::Associations.load_associations(data, query_helpers[:associations], query_helpers[:as_json])
42
- count = PatternQueryHelper::Sql.sql_query_count(query_config)
42
+ count = PatternQueryHelper::Sql.sql_query_count(data)
43
+ data.map! { |d| d.except(PatternQueryHelper::Sql::QUERY_COUNT_COLUMN) } if query_config[:page] or query_config[:per_page]
43
44
  pagination = PatternQueryHelper::Pagination.create_pagination_payload(count, query_helpers[:pagination])
44
45
 
45
46
  {
@@ -89,7 +90,11 @@ module PatternQueryHelper
89
90
  end
90
91
 
91
92
  def self.parse_helpers(query_helpers, valid_columns)
92
- filtering = PatternQueryHelper::Filtering.create_filters(query_helpers[:filter], valid_columns)
93
+ valid_columns_map = {}
94
+ valid_columns.each do |c|
95
+ valid_columns_map["#{c}"] = c
96
+ end
97
+ filtering = PatternQueryHelper::Filtering.create_filters(filters: query_helpers[:filter], valid_columns_map: valid_columns_map)
93
98
  sorting = PatternQueryHelper::Sorting.parse_sorting_params(query_helpers[:sort], valid_columns)
94
99
  associations = PatternQueryHelper::Associations.process_association_params(query_helpers[:include])
95
100
  pagination = PatternQueryHelper::Pagination.parse_pagination_params(query_helpers[:page], query_helpers[:per_page])
@@ -43,6 +43,6 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency "faker", "~> 1.9.3"
44
44
  spec.add_development_dependency "byebug"
45
45
 
46
- spec.add_dependency "activerecord", "~> 5.0"
46
+ spec.add_dependency "activerecord", ">= 5.0"
47
47
  spec.add_dependency "kaminari", "~> 1.1.1"
48
48
  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.2.7
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan McDaniel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-17 00:00:00.000000000 Z
11
+ date: 2021-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,14 +98,14 @@ dependencies:
98
98
  name: activerecord
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '5.0'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '5.0'
111
111
  - !ruby/object:Gem::Dependency
@@ -153,7 +153,7 @@ homepage: https://github.com/iserve-products/pattern_query_helper
153
153
  licenses:
154
154
  - MIT
155
155
  metadata: {}
156
- post_install_message:
156
+ post_install_message:
157
157
  rdoc_options: []
158
158
  require_paths:
159
159
  - lib
@@ -168,9 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  - !ruby/object:Gem::Version
169
169
  version: '0'
170
170
  requirements: []
171
- rubyforge_project:
172
- rubygems_version: 2.7.6
173
- signing_key:
171
+ rubygems_version: 3.0.3
172
+ signing_key:
174
173
  specification_version: 4
175
174
  summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.
176
175
  test_files: []