rad_core_rails 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9e0668296a4c9eaf8a2ee096c59affeb7b72a95174ed1e15716f250ee08a083
4
- data.tar.gz: 91a4ceb121b1439519e451ca319357954bd62782c981752643def0cc7a9f4887
3
+ metadata.gz: 95f6475b2b10bb6d51c5b4ad3feb7ebe08404663230abcae5278e81babe5e189
4
+ data.tar.gz: 90bfdddfeda70290d119eaad3bb4cdd35afc593e04a1d7d7a0c71736567b6710
5
5
  SHA512:
6
- metadata.gz: 9251d1a8bc39a8cefb863f0d6ba26d88e0059bc04cb2bdd14a53ee897a7f53ee85f4400d188433e0b8dba3266bf09caa7c6246c4354aa6df72c32a00d00c88db
7
- data.tar.gz: fbe6961987af27557a37113c2a03977b28fa3df008ed921a663ad32ba586920e935cc6ddb409b06d43155f9ebbbad244fdc181bf908937142f85bb94ba9cd201
6
+ metadata.gz: 5406cb277bf3591e979e14adb5aef5f92370483cd8dce83fb1cd78c333f0c57ebd9a9de3a586272c649738ee44fe5a32a2d7f08b3ab54b545c914366033e1bb9
7
+ data.tar.gz: 20950b080d702d04bde47999be305945643acdf50d078f9d0fe05a50108656ad648f453f652d248e4e0980b9df980f9e84cb0ae8b84b5b366597bffb4616f05b
@@ -46,60 +46,55 @@ module RadCoreRails
46
46
  [query.reject(&:blank?).join(' AND '), args]
47
47
  end
48
48
 
49
- # def generate_search_clause(search)
50
- # and_args = []
51
- # or_args = []
52
- # if search.present? && searchable_columns.is_a?(Array)
53
- # # search_term_size = search.split(' ').length
54
- # and_terms = search.split(' ').select { |term| !term.include?('+') }
55
- # or_terms = search.split(' ').select { |term| term.include?('+') }
56
- # and_columns = []
57
- # or_columns = []
58
- # # just_ors = search_term_size == or_term_size
59
- # # operand = just_ors ? 'OR' : 'AND'
60
- # # search_terms = just_ors == true ? search.split(' ') : search.split(' ').select { |term| !term.include?('+') }
61
- # and_terms.each do |term|
62
- # columns = []
63
- # and_terms.each do |col|
64
- # and_columns.push("(LOWER(#{col}) ILIKE ?)")
65
- # and_args.push '%' + term.downcase.strip + '%'
66
- # end
67
- # or_terms.each do |col|
68
- # columns.push("(LOWER(#{col}) ILIKE ?)")
69
- # or_args.push '%' + term[1, term.length].downcase.strip + '%'
70
- # end
71
- # clause = if or_columns.empty?
72
- # '(' + and_columns.join(' OR ') + ')'
73
- # else
74
- # '(' + '(' + and_columns.join(' OR ') + ')' + 'AND' + '(' + or_columns.join(' OR ') + ')' + ')'
75
- # end
76
- # end
77
- # [clause, and_args + or_args]
78
- # else
79
- # ['', []]
80
- # end
81
- # end
82
-
83
- # @searchable_columns, is an array of column names that you can compare to your terms.
84
- #
85
49
  def generate_search_clause(search)
50
+ search_clause = ['', []]
51
+
86
52
  if search.present? && searchable_columns.is_a?(Array)
87
- # holds all of the sql from sanitized_and_terms
88
- and_args = []
89
- # holds all the table columns sql for all the AND terms
90
- and_clause = []
91
- # holds all of the sql from sanitized_or_terms
92
- or_args = []
93
- # holds all the table columns sql for all the OR terms
94
- or_clause = []
95
- # extract and terms, remove casing, and add ILIKE '%' comparisions
53
+ and_args = [] # SQL from sanitized_and_terms
54
+ excluded_args = [] # SQL from sanitized_excluded_terms
55
+ and_clause = [] # table columns SQL for all the AND terms
56
+ excluded_clause = [] # table columns SQL for all the AND NOT terms
57
+ or_args = [] # SQL from sanitized_or_terms
58
+ or_clause = [] # table columns SQL for all the OR terms
59
+
60
+ phrases_to_exclude = search.scan(/-"([^"]*)"/) # e.g. -"Phrase to exclude"
61
+ phrases_or = search.scan(/\+"([^"]*)"/) # e.g. +"Phrase for OR"
62
+
63
+ # remove excluded phrases from search if any
64
+ if phrases_to_exclude.any?
65
+ phrases_to_exclude.each {|phrase| search.gsub!('-"' + "#{phrase.first}" + '"', '')}
66
+ end
67
+
68
+ # remove 'or' phrases from search if any
69
+ if phrases_or.any?
70
+ phrases_or.each {|phrase| search.gsub!('+"' + "#{phrase.first}" + '"', '')}
71
+ end
72
+
73
+ # extract 'and' terms, remove casing, and add ILIKE '%' comparisons
96
74
  sanitized_and_terms = search.split(' ')
97
- .reject { |term| term.include?('+') }
75
+ .reject { |term| term.include?('+') || term.include?('-') }
98
76
  .map { |term| '%' + term.downcase.strip + '%' }
99
- # extract or terms, remove casing, and add ILIKE '%' comparisions
77
+
78
+ # extract 'or' terms, remove casing, and add ILIKE '%' comparisons
100
79
  sanitized_or_terms = search.split(' ')
101
80
  .select { |term| term.include?('+') }
102
81
  .map { |term| '%' + term[1, term.length].downcase.strip + '%' }
82
+
83
+ # add 'or' phrase terms, remove casing, and add ILIKE '%' comparisons
84
+ if phrases_or.any?
85
+ sanitized_or_terms += phrases_or.flatten.map{|phrase| '%' + phrase.downcase.strip + '%'}
86
+ end
87
+
88
+ # extract excluded terms, remove casing, and add NOT ILIKE '%' comparisons
89
+ sanitized_excluded_terms = search.split(' ')
90
+ .select { |term| term.include?('-') }
91
+ .map { |term| '%' + term[1, term.length].downcase.strip + '%' }
92
+
93
+ # add excluded phrase terms, remove casing, and add NOT ILIKE '%' comparisons
94
+ if phrases_to_exclude.any?
95
+ sanitized_excluded_terms += phrases_to_exclude.flatten.map{|phrase| '%' + phrase.downcase.strip + '%'}
96
+ end
97
+
103
98
  # loop through sanitized_and_terms to find all possible columns.
104
99
  sanitized_and_terms.each do |sanitized_term|
105
100
  columns = []
@@ -110,6 +105,16 @@ module RadCoreRails
110
105
  end
111
106
  and_clause.push '(' + columns.join(' OR ') + ')'
112
107
  end
108
+ # loop through sanitized_excluded_terms to find all possible columns.
109
+ sanitized_excluded_terms.each do |sanitized_term|
110
+ columns = []
111
+ # all possible columns where this should be searched
112
+ searchable_columns.each do |col|
113
+ columns.push("(LOWER(#{col}) NOT ILIKE ?)")
114
+ excluded_args.push sanitized_term
115
+ end
116
+ excluded_clause.push '(' + columns.join(' AND ') + ')'
117
+ end
113
118
  # loop through sanitized_or_terms to find all possible columns.
114
119
  sanitized_or_terms.each do |sanitized_term|
115
120
  columns = []
@@ -120,18 +125,48 @@ module RadCoreRails
120
125
  end
121
126
  or_clause.push '(' + columns.join(' OR ') + ')'
122
127
  end
128
+
129
+ if or_clause.empty? && and_clause.empty? && excluded_clause.empty?
130
+ return ['', []]
131
+ end
132
+
133
+ if or_clause.empty? && excluded_clause.empty?
134
+ return ['(' + and_clause.join(' AND ') + ')', and_args]
135
+ end
136
+
137
+ if and_clause.empty? && excluded_clause.empty?
138
+ return ['(' + or_clause.join(' OR ') + ')', or_args]
139
+ end
140
+
123
141
  if or_clause.empty? && and_clause.empty?
124
- ['', []]
125
- elsif or_clause.empty?
126
- ['(' + and_clause.join(' AND ') + ')', and_args]
127
- elsif and_clause.empty?
128
- ['(' + or_clause.join(' OR ') + ')', or_args]
129
- else
130
- ['((' + and_clause.join(' AND ') + ') AND (' + or_clause.join(' OR ') + '))', and_args + or_args]
142
+ return ['(' + excluded_clause.join(' AND ') + ')', excluded_args]
131
143
  end
132
- else
133
- ['', []]
144
+
145
+ if or_clause.empty?
146
+ return ['((' + and_clause.join(' AND ') + ') AND (' + excluded_clause.join(' AND ') + '))', and_args + excluded_args]
147
+ end
148
+
149
+ if and_clause.empty?
150
+ return ['((' + or_clause.join(' OR ') + ') AND (' + excluded_clause.join(' AND ') + '))', or_args + excluded_args]
151
+ end
152
+
153
+ if excluded_clause.empty?
154
+ return ['((' + and_clause.join(' AND ') + ') AND (' + or_clause.join(' OR ') + '))', and_args + or_args]
155
+ end
156
+
157
+ search_clause = [
158
+ '((' +
159
+ and_clause.join(' AND ') +
160
+ ') AND (' +
161
+ or_clause.join(' OR ') +
162
+ ') AND (' +
163
+ excluded_clause.join(' AND ') +
164
+ '))',
165
+ and_args + or_args + excluded_args
166
+ ]
134
167
  end
168
+
169
+ search_clause
135
170
  end
136
171
 
137
172
  # Generate Boolean clause
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RadCoreRails
4
- VERSION = '0.7.4'
4
+ VERSION = '0.7.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rad_core_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Poltavets, James Marrs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-13 00:00:00.000000000 Z
11
+ date: 2021-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport