pattern_query_helper 0.2.5 → 0.2.6

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
- SHA1:
3
- metadata.gz: 7ba28c50b8182b282a395344c179d5a6329d5fcf
4
- data.tar.gz: 6e49fce6551458c99ff19865155f26c9791cc5d4
2
+ SHA256:
3
+ metadata.gz: f9478c942fdda25782ac53d2adc8aeea980719414857098383a5c5b6cfbecf6a
4
+ data.tar.gz: d5cf42e0b74a810e5111bb42e78595c7fca64ecd87657b7af6a23baa7765cc53
5
5
  SHA512:
6
- metadata.gz: 524be27175d2e7184876227ebc9e0e2066c5913e79deca6227e61057fb4036bef66549f337652d6187013c64e54d30e66134624cf83185882a9625598b5310f4
7
- data.tar.gz: fd6db4e9904794269808cbc9de802b49f49987a72f8ee3cfa83542ae78e9eaeffdf71febaf9b2c4af1448ff3399e9bc139e672d6e62d59e92e6dff28120f9fed
6
+ metadata.gz: 59ea5d4b72d30ab2761f1f72fc43546ac20c2f48cb3e5924c95e81522a535a526be3f074226e6fc911b36b7dd1f902c5a897e2fa0c1166748635e30424f3ec74
7
+ data.tar.gz: b90f41129c7aea623290ffe882e981327b6604a821fc1536ff3eac67350ac05ece354c1a82605df0886612b4ff6795b4ee7752d3550ca4442ae2b804ad21ae7b
data/README.md CHANGED
@@ -26,22 +26,24 @@ Or install it yourself as:
26
26
 
27
27
  To run an active record query execute
28
28
  ```ruby
29
- PatternQueryHelper.run_sql_query(active_record_call, query_helpers, single_record)
29
+ PatternQueryHelper.run_active_record_query(active_record_call, query_helpers, valid_columns, single_record)
30
30
  ```
31
31
  active_record_call: Valid active record syntax (i.e. ```Object.where(state: 'Active')```)
32
32
  query_helpers: See docs below
33
+ valid_columns: Default is []. Pass in an array of columns you want to allow sorting and filtering on.
33
34
  single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
34
35
 
35
36
  ### Custom SQL Queries
36
37
 
37
38
  To run a custom sql query execute
38
39
  ```ruby
39
- PatternQueryHelper.run_sql_query(model, query, query_params, query_helpers, single_record)
40
+ PatternQueryHelper.run_sql_query(model, query, query_params, query_helpers, valid_columns, single_record)
40
41
  ```
41
42
  model: A valid ActiveRecord model
42
43
  query: A string containing your custom SQL query
43
44
  query_params: a symbolized hash of binds to be included in your SQL query
44
45
  query_helpers: See docs below
46
+ valid_columns: Default is []. Pass in an array of columns you want to allow sorting and filtering on.
45
47
  single_record: Default is false. Pass in true to format payload as a single object instead of a list of objects
46
48
 
47
49
  ## Query Helpers
@@ -86,6 +88,12 @@ Filtering is controlled by the `filter` object in the query_helpers hash
86
88
  },
87
89
  "column_2" => {
88
90
  "eql" => "my_string"
91
+ },
92
+ "column_3" => {
93
+ "like" => "my_string%"
94
+ },
95
+ "column_4" => {
96
+ "in" => "item1,item2,item3"
89
97
  }
90
98
  }
91
99
  ```
@@ -99,6 +107,7 @@ The following operator codes are valid
99
107
  “lt”: <
100
108
  “eql”: =
101
109
  “noteql”: !=
110
+ "like": like
102
111
  “in”: in
103
112
  “notin” not in
104
113
  “null”: “is null” or “is not null” (pass in true or false as the value)
@@ -2,11 +2,7 @@ module PatternQueryHelper
2
2
  class Associations
3
3
  def self.process_association_params(associations)
4
4
  associations ||= []
5
- if associations.class == String
6
- [associations.to_sym]
7
- else
8
- associations
9
- end
5
+ associations.class == String ? [associations.to_sym] : associations
10
6
  end
11
7
 
12
8
  def self.load_associations(payload, associations, as_json_options)
@@ -1,16 +1,13 @@
1
1
  module PatternQueryHelper
2
2
  class Filtering
3
- def self.create_filters(filters, column_map=nil, symbol_prefix="")
3
+ def self.create_filters(filters, valid_columns=nil, symbol_prefix="")
4
4
  filters ||= {}
5
5
  filter_string = "true = true"
6
6
  filter_params = {}
7
7
  filter_array = []
8
8
  filters.each do |filter_attribute, criteria|
9
- if column_map
10
- raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless column_map[filter_attribute]
11
- filter_column = column_map[filter_attribute]
12
- else
13
- filter_column = filter_attribute
9
+ if valid_columns
10
+ raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless valid_columns.include? filter_attribute
14
11
  end
15
12
  criteria.each do |operator_code, criterion|
16
13
  filter_symbol = "#{symbol_prefix}#{filter_attribute}_#{operator_code}"
@@ -27,29 +24,25 @@ module PatternQueryHelper
27
24
  operator = "="
28
25
  when "noteql"
29
26
  operator = "!="
27
+ when "like"
28
+ operator = "like"
30
29
  when "in"
31
- values = criterion.split(",").map { |s| s.to_i }
32
- values = values.to_s.gsub("[","(").gsub("]",")")
33
- operator = "in #{values}"
34
- filter_symbol = ""
30
+ operator = "in (:#{filter_symbol})"
31
+ criterion = criterion.split(",")
32
+ filter_symbol_already_embedded = true
35
33
  when "notin"
36
- values = criterion.split(",").map { |s| s.to_i }
37
- values = values.to_s.gsub("[","(").gsub("]",")")
38
- operator = "not in #{values}"
39
- filter_symbol = ""
34
+ operator = "not in (:#{filter_symbol})"
35
+ criterion = criterion.split(",")
36
+ filter_symbol_already_embedded = true
40
37
  when "null"
41
- if criterion = true || "true"
42
- operator = "is null"
43
- elsif criterion = false || "false"
44
- operator = "is not null"
45
- end
38
+ operator = criterion.to_s == "true" ? "is null" : "is not null"
46
39
  filter_symbol = ""
47
40
  else
48
41
  raise ArgumentError.new("Invalid operator code '#{operator_code}' on '#{filter_attribute}' filter")
49
42
  end
50
- filter_symbol_embed = ":#{filter_symbol}" unless filter_symbol.blank?
51
- filter_string = "#{filter_string} and #{filter_column} #{operator} #{filter_symbol_embed}"
52
- filter_params["#{filter_symbol}"] = criterion
43
+ filter_string = "#{filter_string} and #{filter_attribute} #{operator}"
44
+ filter_string << " :#{filter_symbol}" unless filter_symbol_already_embedded or filter_symbol.blank?
45
+ filter_params["#{filter_symbol}"] = criterion unless filter_symbol.blank?
53
46
  filter_array << {
54
47
  column: filter_attribute,
55
48
  operator: operator,
@@ -4,16 +4,8 @@ require 'kaminari'
4
4
  module PatternQueryHelper
5
5
  class Pagination
6
6
  def self.parse_pagination_params(page, per_page)
7
- if page
8
- page = page.to_i
9
- else
10
- page = 1
11
- end
12
- if per_page
13
- per_page = per_page.to_i
14
- else
15
- per_page = 20
16
- end
7
+ page = page ? page.to_i : 1
8
+ per_page = per_page ? per_page.to_i : 20
17
9
  raise RangeError.new("page must be greater than 0") unless page > 0
18
10
  raise RangeError.new("per_page must be greater than 0") unless per_page > 0
19
11
 
@@ -1,3 +1,3 @@
1
1
  module PatternQueryHelper
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -89,11 +89,7 @@ module PatternQueryHelper
89
89
  end
90
90
 
91
91
  def self.parse_helpers(query_helpers, valid_columns)
92
- valid_columns_map = {}
93
- valid_columns.each do |c|
94
- valid_columns_map["#{c}"] = c
95
- end
96
- filtering = PatternQueryHelper::Filtering.create_filters(query_helpers[:filter], valid_columns_map)
92
+ filtering = PatternQueryHelper::Filtering.create_filters(query_helpers[:filter], valid_columns)
97
93
  sorting = PatternQueryHelper::Sorting.parse_sorting_params(query_helpers[:sort], valid_columns)
98
94
  associations = PatternQueryHelper::Associations.process_association_params(query_helpers[:include])
99
95
  pagination = PatternQueryHelper::Pagination.parse_pagination_params(query_helpers[:page], query_helpers[:per_page])
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.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan McDaniel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-18 00:00:00.000000000 Z
11
+ date: 2019-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  requirements: []
171
171
  rubyforge_project:
172
- rubygems_version: 2.6.14
172
+ rubygems_version: 2.7.6
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.