query_helper 0.2.7 → 0.2.8

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: 34196dda5d9ed84de34c317c6f9096a87e9b403ca37868f5f087ef565886fe8d
4
- data.tar.gz: e036203e497d95e1d37ceddd53ec72a3cb794ac3c6675842dfae642e0d9db9ac
3
+ metadata.gz: 2c63157141a68e8dafa45c307f5be2c90cc88fdea8b4c4777c4f81f89a58e797
4
+ data.tar.gz: 720e0093ccddebf935977aa8180127c99b2bbcd232f343ba75684c32c38d7686
5
5
  SHA512:
6
- metadata.gz: 71590ca9d1f926b868d44f6170390d6a795dcab47f17dae1fbc7aed8e010d85d095e7322c5e1fbfe491ef614a2d9abf2863ce67baf57ab013f8d4faac7db823f
7
- data.tar.gz: 3efc246697287100945bd1c247df76ca0db2c5d04df4c44fb711560de9245c5daabe5d73f8a3c9dc4d6c0edf748f8e29b065ad3d30816ddccde0205dcee9c6c2
6
+ metadata.gz: 2d49fc27361319b65c9b03d07b387561baf0341f3783166b41f51b30166b6a381d8ef2ccdc7dab141fd7db58bf16f68850a6ed6692ad9f055eff6304fce52acb
7
+ data.tar.gz: 87f9578aad0578a016aa573493adf486aeb9793506522685e1d6c388c028e85483d97a29d420f9257d81a4c3fd33577ffb6b76e331c626eaf1f7b22b717a6c99
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.2.7)
4
+ query_helper (0.2.8)
5
5
  activerecord (> 5)
6
6
  activesupport (> 5)
7
7
 
data/README.md CHANGED
@@ -111,6 +111,33 @@ null | is null *or* is not null
111
111
 
112
112
  Note: For the null operator code, toggle *is null* operator with true and *is not null* operator with false
113
113
 
114
+ #### Search
115
+
116
+ QueryHelper supports searching across multiple fields. To implement pass an array of column aliases into the `search_fields` argument when creating or updating a `QueryHelper` object.
117
+
118
+ ```ruby
119
+ @query_helper.update(search_fields: ["column1", "column2"])
120
+ render json: @query_helper.results()
121
+ ```
122
+
123
+ You can then take advantage of the `search_for` url param to do text matching in any of the columns included
124
+
125
+ Request: `http://www.example.com/resources?search_for=foo`
126
+
127
+ Results:
128
+ ```json
129
+ [
130
+ {
131
+ "column1": "foobar",
132
+ "column2": "bar"
133
+ },
134
+ {
135
+ "column1": "bar",
136
+ "column2": "barfoo"
137
+ }
138
+ ]
139
+ ```
140
+
114
141
  #### Associations
115
142
 
116
143
  Include ActiveRecord associations in the payload. The association must be defined in the model.
data/lib/query_helper.rb CHANGED
@@ -29,7 +29,9 @@ class QueryHelper
29
29
  as_json_options: nil, # a list of as_json options you'd like run before returning the payload
30
30
  custom_mappings: {}, # custom keyword => sql_expression mappings
31
31
  api_payload: false, # Return the paginated payload or simply return the result array
32
- preload: [] # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
32
+ preload: [], # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
33
+ search_fields: [],
34
+ search_string: nil
33
35
  )
34
36
  @query = query.class < ActiveRecord::Relation ? query.to_sql : query
35
37
  @model = query.class < ActiveRecord::Relation ? query.base_class : model
@@ -44,6 +46,8 @@ class QueryHelper
44
46
  @custom_mappings = custom_mappings
45
47
  @api_payload = api_payload
46
48
  @preload = preload
49
+ @search_fields = search_fields
50
+ @search_string = search_string
47
51
 
48
52
  if @page && @per_page
49
53
  # Determine limit and offset
@@ -64,7 +68,8 @@ class QueryHelper
64
68
  as_json_options: nil,
65
69
  single_record: nil,
66
70
  custom_mappings: nil,
67
- preload: []
71
+ preload: [],
72
+ search_fields: nil
68
73
  )
69
74
  @query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
70
75
  @model = query.class < ActiveRecord::Relation ? query.base_class : model if model || query
@@ -75,6 +80,7 @@ class QueryHelper
75
80
  @as_json_options = as_json_options if as_json_options
76
81
  @custom_mappings = custom_mappings if custom_mappings
77
82
  @preload = preload if preload
83
+ @search_fields = search_fields if search_fields
78
84
  return self
79
85
  end
80
86
 
@@ -102,14 +108,27 @@ class QueryHelper
102
108
  # create the filters from the column maps
103
109
  @sql_filter.create_filters()
104
110
 
111
+ having_clauses = @sql_filter.having_clauses
112
+ where_clauses = @sql_filter.where_clauses
113
+
114
+ if @search_string
115
+ search_filter = search_filter(column_maps)
116
+ if search_filter[:placement] == :where
117
+ where_clauses << search_filter[:filter]
118
+ else
119
+ having_clauses << search_filter[:filter]
120
+ end
121
+ end
122
+
123
+
105
124
  # merge the filter bind variables into the query bind variables
106
125
  @bind_variables.merge!(@sql_filter.bind_variables)
107
126
 
108
127
  # Execute Sql Query
109
128
  manipulator = SqlManipulator.new(
110
129
  sql: @query,
111
- where_clauses: @sql_filter.where_clauses,
112
- having_clauses: @sql_filter.having_clauses,
130
+ where_clauses: where_clauses,
131
+ having_clauses: having_clauses,
113
132
  order_by_clauses: @sql_sort.parse_sort_string,
114
133
  include_limit_clause: @page && @per_page ? true : false,
115
134
  additional_select_clauses: @sql_sort.select_strings
@@ -230,4 +249,20 @@ class QueryHelper
230
249
  model: @model
231
250
  )
232
251
  end
252
+
253
+ def search_filter(column_maps)
254
+ raise ArgumentError.new("search_fields not defined") unless @search_fields.length > 0
255
+ placement = :where
256
+ maps = column_maps.select do |cm|
257
+ placement = :having if cm.aggregate
258
+ @search_fields.include? cm.alias_name
259
+ end
260
+ bind_variable = ('a'..'z').to_a.shuffle[0,20].join.to_sym
261
+ @bind_variables[bind_variable] = "%#{@search_string}%"
262
+ filter = "#{maps.map{|m| "#{m.sql_expression}::varchar"}.join(" || ")} ilike :#{bind_variable}"
263
+ return {
264
+ filter: filter,
265
+ placement: placement
266
+ }
267
+ end
233
268
  end
@@ -33,6 +33,7 @@ class QueryHelper
33
33
  helpers[:sql_filter] = create_query_helper_filter() if params[:filter]
34
34
  helpers[:sql_sort] = create_query_helper_sort() if params[:sort]
35
35
  helpers[:associations] = create_query_helper_associations() if params[:include]
36
+ helpers[:search_string] = params[:search_for] if params[:search_for]
36
37
  helpers
37
38
  end
38
39
  end
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
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.7
4
+ version: 0.2.8
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-03-31 00:00:00.000000000 Z
11
+ date: 2020-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler