default_where 2.2.3 → 2.3.0

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: 9860242afa48f1b00939bf840ab9c905891a664498082c3596853b856a6014df
4
- data.tar.gz: df25ea89826f20d10291780ae4e9f90770a1627bbef83a7f9cf82bc3447a7549
3
+ metadata.gz: b496f6c6778aac3b2e8ef9d0a47f8d7e8557f2080aeaa0dc15605f80266c3760
4
+ data.tar.gz: 0320e5570248726e1a4716f852ba951480fccb386b298db0d1118181fb1180eb
5
5
  SHA512:
6
- metadata.gz: 3a4542d9ccc386102203d62ae0aadcdadc4c688f09365b96d7a620ade26050bd872f7f399f12d5e5e0cb6c3f40aa0e4feb19c844872e8d4819f927c85240862a
7
- data.tar.gz: 37dab845591f27c34813b29d0cf1b4139568de7670b6b56ae0faefcc32c9d8be09e71f0117060df667a760f0b59c2db6984d5ac2d286ff0cf5fac4e32a92cf53
6
+ metadata.gz: 0fec4d669be1436bdf7cb217bdb0cee4a30fdd4e7dd4e8b201a9c444da07bc3a5ef100a0329a7c4c3c679af045aa08e48ef2137105b24cc15adcc44f69af7ddf
7
+ data.tar.gz: 2633a6a9cab857a51fc321efce967f7fe4c7fbcc835abed292066a2c90367ad4f6a2746e76bdf4a8418f62f18707851c944ecff1a93a7dec73987d98e37a8fdc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015-Present Mingyuan Qin <mingyuan0715@foxmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## DefaultWhere
2
2
 
3
3
 
4
- This Library set default params process for where query in ActiveRecord
4
+ `default_where` set default params process for where query in ActiveRecord
5
5
 
6
6
  It's a wise decision to use `default_where` with [default_form](https://github.com/qinmingyuan/default_form) to replace `ransack`
7
7
 
@@ -14,6 +14,8 @@ It's a wise decision to use `default_where` with [default_form](https://github.c
14
14
  * 范围:key-gte: value,
15
15
  * 排序:key-asc: 1, key-desc: 2
16
16
  * 排除:key-not: value
17
+ #### 对于postgresql 数据库
18
+ * 包含任一值:key-any: value
17
19
 
18
20
  ### Normal equal params
19
21
 
@@ -71,14 +73,25 @@ users = users.where(age: params[:age]) if params[:age]
71
73
  ```ruby
72
74
  User.default_where(params)
73
75
 
74
- # also can control which blank value can use
75
- User.default_where(params, { allow: [nil] })
76
+ ```
77
+
78
+ ### allow
79
+ you can control which blank value can use
80
+
81
+ ```ruby
82
+ {
83
+ name: nil,
84
+ allow: { name: nil }
85
+ }
76
86
  ```
77
87
 
78
88
  ### OR
79
89
  ```ruby
80
90
  params = {
81
- 'users.email-not-or-name': 'dhh'
91
+ or: {
92
+ 'users.email-not': 'qin',
93
+ 'name': 'qin'
94
+ }
82
95
  }
83
96
 
84
97
  ```
@@ -97,7 +110,13 @@ User.where(name: params[:name].strip)
97
110
  User.default_where(params)
98
111
 
99
112
  # also can control whether use strip
100
- User.default_where(params, { strip: false })
113
+
114
+ {
115
+ name: ' qin',
116
+ strip: {
117
+ name: false
118
+ }
119
+ }
101
120
  ```
102
121
 
103
122
  ### Order params
@@ -114,10 +133,30 @@ User.order(age: :asc, last_login_at: :asc)
114
133
  User.default_where(params)
115
134
  ```
116
135
 
136
+ ## For Postgresql
137
+ * support JSONB filter,just use like this: `column_name/json_key`, just notice jsonb all value is string type;
138
+ ```ruby
139
+ # before
140
+ Order.where("extra->>'maintain_id' = :key", key: id.to_s)
141
+ # after
142
+ Order.default_where('extra/maintain_id': id.to_s)
143
+ ```
144
+
117
145
  ## A sample with all params above
118
146
  * Params
119
147
  ```ruby
120
- { name: 'dhh', 'role.id': 2, 'age-lte': 2, 'age-asc': '1', 'last_login_at-asc': '2' }
148
+ {
149
+ name: 'dhh',
150
+ 'role.id': 2,
151
+ 'age-lte': 2,
152
+ 'age-asc': '1',
153
+ 'last_login_at-asc': '2',
154
+ or: {
155
+ name: 'dhh',
156
+ email: 'dhh'
157
+ },
158
+ allow: { name: nil }
159
+ }
121
160
  ```
122
161
  * Before use `default_where`
123
162
  ```ruby
@@ -127,3 +166,6 @@ User.includes(:role).where(name: params[:name], 'roles.id': params[:'role.id']).
127
166
  ```ruby
128
167
  User.default_where(params)
129
168
  ```
169
+
170
+ ## 许可证
171
+ 遵循 MIT 协议
data/lib/default_where.rb CHANGED
@@ -1,93 +1,69 @@
1
- require 'default_where/not'
2
- require 'default_where/range'
3
- require 'default_where/like'
1
+ # frozen_string_literal: true
2
+
3
+ require 'default_where/scope'
4
4
  require 'default_where/order'
5
- require 'default_where/or'
5
+ require 'default_where/params'
6
+ require 'default_where/group'
6
7
 
7
8
  module DefaultWhere
8
- include DefaultWhere::Not
9
- include DefaultWhere::Range
10
- include DefaultWhere::Order
11
- include DefaultWhere::Like
12
- #include DefaultWhere::Or
9
+ include Scope
10
+ include Order
11
+ include Params
12
+ include Group
13
13
 
14
- REJECT = ['', nil]
14
+ REJECT = ['', nil].freeze
15
15
  STRIP = true
16
16
 
17
- def default_where(params = {}, options = {})
17
+ def default_where(params = {})
18
18
  return all if params.blank?
19
19
 
20
20
  params = params.to_h
21
- params, refs, tables = params_with_table(params, options)
21
+ options = {}
22
+ [:strip, :allow, :reject].each do |key|
23
+ options[key] = params.delete(key) if params[key].respond_to?(:to_hash)
24
+ end
25
+ or_params = {}
26
+ or_params = params.delete(:or) if params[:or].respond_to?(:to_hash)
22
27
 
23
- #or_params = filter_or(params)
24
- range_params = filter_range(params)
25
- order_params = filter_order(params)
26
- not_params = filter_not(params)
27
- like_params = filter_like(params)
28
+ and_params, and_refs, and_tables = default_where_params(params, options)
29
+ order_params = default_where_order_filter(and_params)
30
+ and_params.except!(*order_params.keys)
28
31
 
29
- equal_params = params.except!(*range_params.keys, *order_params.keys, *not_params.keys, *like_params.keys)
32
+ or_params, or_refs, or_tables = default_where_params(or_params, options)
33
+ refs = and_refs + or_refs
34
+ tables = and_tables + or_tables
30
35
 
31
- includes(refs).where(equal_params).references(tables)
32
- .not_scope(not_params)
33
- .like_scope(like_params)
34
- .range_scope(range_params)
35
- .order_scope(order_params)
36
+ includes(refs).default_where_and(and_params).default_where_or(or_params).default_where_order(order_params).references(tables)
36
37
  end
37
38
 
38
- def params_with_table(params = {}, options = {})
39
- if options.has_key?(:reject)
40
- default_reject = Array(options[:reject])
41
- elsif options.has_key?(:allow)
42
- default_reject = REJECT - Array(options[:allow])
43
- else
44
- default_reject = REJECT
45
- end
39
+ def default_where_and(params = {})
40
+ return current_scope if params.blank?
46
41
 
47
- unless options.has_key? :strip
48
- options[:strip] = STRIP
42
+ equal_params = {}
43
+ params.each do |key, _|
44
+ equal_params[key] = params.delete(key) unless key.match? /[-\/]/
49
45
  end
46
+ where_string, where_hash = default_where_scope(params)
47
+ where_string = where_string.join ' AND '
50
48
 
51
- refs = []
52
- tables = []
53
- final_params = {}
54
-
55
- params.each do |key, value|
56
- value = value.strip if value.is_a?(String) && options[:strip]
57
- next if default_reject.include?(value)
58
- key = key.to_s
59
-
60
- if key =~ /\./
61
- _table, _column = key.split('.')
62
- _real_column = _column.split('-').first
63
- _ref = reflections[_table]
64
- if _ref && _ref.klass.column_names.include?(_real_column)
65
- _table = _ref.table_name
66
- elsif connection.data_sources.include?(_table) && connection.column_exists?(_table, _real_column)
67
- _refs = reflections.select { |_, v| v.table_name == _table && !v.polymorphic? }
68
- if _refs.size == 1
69
- _ref = _refs.first
70
- else
71
- raise "#{key} makes confused, please use reflection name!"
72
- end
73
- else
74
- next
75
- end
76
- refs << _ref.name unless refs.include?(_ref.name)
77
- tables << _table unless tables.include?(_table)
78
- final_params["#{_table}.#{_column}"] = value
79
- else
80
- _real_column = key.split('-').first
81
- next unless column_names.include?(_real_column)
82
- final_params[key] = value
83
- end
84
- end
49
+ where(equal_params).where(where_string, where_hash)
50
+ end
85
51
 
86
- [final_params, refs, tables]
52
+ def default_where_or(params = {})
53
+ return current_scope if params.blank?
54
+
55
+ where_string, where_hash = default_where_scope(params)
56
+ where_string = where_string.join ' OR '
57
+
58
+ where(where_string, where_hash)
59
+ end
60
+
61
+ def logger
62
+ ::ActiveRecord::Base.logger
87
63
  end
88
64
 
89
65
  end
90
66
 
91
67
  ActiveSupport.on_load :active_record do
92
68
  extend DefaultWhere
93
- end
69
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefaultWhere
4
+ module Group
5
+
6
+ # {
7
+ # select: 'sum(total_amount)',
8
+ # select: {
9
+ # a: 'sum(total_amount)',
10
+ # b: 'sum()'
11
+ # }
12
+ # }
13
+ # group: 'date(created_at)',
14
+ def default_group(*group, select:)
15
+ if select.respond_to?(:to_hash)
16
+ selected = select.map do |k, v|
17
+ "#{v} AS #{k}"
18
+ end
19
+ else
20
+ selected = Array(select)
21
+ end
22
+
23
+ unscoped.select(*selected, *group).group(*select.values)
24
+ end
25
+
26
+ end
27
+ end
@@ -1,17 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DefaultWhere
2
4
  module Order
5
+ PATTERN = {
6
+ '-asc': :asc,
7
+ '-desc': :desc
8
+ }
3
9
 
4
- def order_scope(params)
5
- order_array = []
10
+ def default_where_order(params)
11
+ order_hash = {}
6
12
 
7
13
  params.sort_by{ |_, v| v.to_i }.each do |i|
8
- order_array << i[0].sub(/-(asc|desc)$/, '-asc' => ' ASC', '-desc' => ' DESC')
14
+ k, v = i[0].split('-')
15
+ order_hash[k] = v
9
16
  end
10
17
 
11
- order(order_array)
18
+ order(order_hash)
12
19
  end
13
20
 
14
- def filter_order(params)
21
+ def default_where_order_filter(params)
15
22
  params.select do |k, v|
16
23
  k.end_with?('-asc', '-desc') && String(v) =~ /^[1-9]$/
17
24
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefaultWhere
4
+ module Params
5
+
6
+ def default_where_params(params = {}, options = {})
7
+ refs = []
8
+ tables = []
9
+ final_params = {}
10
+
11
+ params.each do |key, value|
12
+ # strip, if assign key to false, will not works
13
+ strip = options.fetch(:strip, {}).fetch(key, STRIP)
14
+ value = value.strip if value.is_a?(String) && strip
15
+
16
+ # reject
17
+ if options.key?(:reject)
18
+ reject = options.fetch(:reject, {}).fetch(key, REJECT)
19
+ if reject == nil
20
+ reject = [nil]
21
+ elsif reject == []
22
+ reject = [[]]
23
+ else
24
+ reject = Array(reject)
25
+ end
26
+ else
27
+ allow = options.fetch(:allow, {}).fetch(key, [])
28
+ if allow == nil
29
+ allow = [nil]
30
+ else
31
+ allow = Array(allow)
32
+ end
33
+ reject = REJECT - allow
34
+ end
35
+ next if reject.include?(value)
36
+
37
+ items = key.to_s.split('.')
38
+ column = items[-1]
39
+ real_column = column.split(/[-\/]/)[0]
40
+
41
+ if items.size == 1
42
+ next unless column_names.include?(real_column)
43
+ table = nil
44
+ else
45
+ prefix = items[0]
46
+ ref = reflections[prefix]
47
+
48
+ # 检查 prefix 是否为关联关系的名称
49
+ if ref && !ref.polymorphic?
50
+ table = ref.table_name
51
+ # 检查 prefix 是否为表名,且表中存在 real_column 字段
52
+ elsif connection.data_sources.include?(prefix) && connection.column_exists?(prefix, real_column)
53
+ possible_refs = reflections.select { |_, v| v.table_name == prefix }
54
+ if possible_refs.size < 1
55
+ ref = nil
56
+ elsif possible_refs.size == 1
57
+ ref = possible_refs[0]
58
+ else
59
+ raise "#{key} makes confused, please use reflection name!"
60
+ end
61
+
62
+ table = prefix
63
+ else
64
+ next
65
+ end
66
+
67
+ if ref && !ref.klass.column_names.include?(real_column)
68
+ next
69
+ end
70
+
71
+ if ref && !refs.include?(ref.name)
72
+ refs << ref.name
73
+ end
74
+
75
+ unless tables.include?(table)
76
+ tables << table
77
+ end
78
+ end
79
+
80
+ if table
81
+ final_params["#{table}.#{column}"] = value
82
+ else
83
+ final_params[column] = value
84
+ end
85
+ end
86
+
87
+ [final_params, refs, tables]
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefaultWhere
4
+ module Scope
5
+ PATTERN = {
6
+ gt: '>',
7
+ gte: '>=',
8
+ lt: '<',
9
+ lte: '<=',
10
+ not: '!=',
11
+ like: 'LIKE',
12
+ rl: 'LIKE',
13
+ ll: 'LIKE',
14
+ :'' => '='
15
+ }.freeze
16
+
17
+ def default_where_scope(params)
18
+ where_string = []
19
+ where_hash = {}
20
+
21
+ params.each do |key, value|
22
+ real_key, sign_str = key.split('-')
23
+ agent_key = key.gsub(/[-.\/]/, '_')
24
+
25
+ if value.nil? || value == []
26
+ if sign_str == 'not'
27
+ where_string << "#{real_key} IS NOT NULL"
28
+ elsif sign_str.nil?
29
+ where_string << "#{real_key} IS NULL"
30
+ else
31
+ raise "#{key}'s value can not be nil"
32
+ end
33
+ elsif sign_str == 'any' # 支持 postgres array 查询
34
+ where_string << ":#{agent_key} = ANY(#{real_key})"
35
+ where_hash.merge! agent_key.to_sym => value
36
+ elsif real_key.match?(/.\/./) # 支持 postgres json 查询
37
+ real_key, i18n_key = key.split('/')
38
+ where_string << "#{real_key}->>'#{i18n_key}' = :#{agent_key}"
39
+ where_hash.merge! agent_key.to_sym => value
40
+ else
41
+ case sign_str
42
+ when 'll'
43
+ real_value = "#{value}%"
44
+ when 'rl'
45
+ real_value = "%#{value}"
46
+ when 'like'
47
+ real_value = "%#{value}%"
48
+ else
49
+ real_value = value
50
+ end
51
+
52
+ where_string << "#{table_name}.#{real_key} #{PATTERN[sign_str.to_s.to_sym]} :#{agent_key}"
53
+ where_hash.merge! agent_key.to_sym => real_value
54
+ end
55
+ end
56
+
57
+ [where_string, where_hash]
58
+ end
59
+
60
+ end
61
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DefaultWhere
2
- VERSION = '2.2.3'
4
+ VERSION = '2.3.0'
3
5
  end
data/test/helper.rb CHANGED
@@ -1,14 +1,3 @@
1
- # $DEBUG = true
2
-
3
- require 'rubygems'
4
- require 'bundler/setup'
5
- begin
6
- Bundler.setup(:default, :test, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
1
  require 'active_record'
13
2
  require 'minitest/autorun'
14
3
  require 'factory_bot'
@@ -33,4 +22,4 @@ def teardown_db
33
22
  ActiveRecord::Base.connection.truncate(table)
34
23
  end
35
24
  end
36
- teardown_db
25
+ teardown_db
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: default_where
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-26 00:00:00.000000000 Z
11
+ date: 2021-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '4.0'
20
20
  - - "<="
21
21
  - !ruby/object:Gem::Version
22
- version: '6.0'
22
+ version: '7.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,65 +29,50 @@ dependencies:
29
29
  version: '4.0'
30
30
  - - "<="
31
31
  - !ruby/object:Gem::Version
32
- version: '6.0'
32
+ version: '7.0'
33
33
  - !ruby/object:Gem::Dependency
34
- name: rdoc
34
+ name: sdoc
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: '1.0'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0'
46
+ version: '1.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: '12.3'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '0'
61
- - !ruby/object:Gem::Dependency
62
- name: factory_bot_rails
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '0'
75
- description: Description of QueryScope.
60
+ version: '12.3'
61
+ description: Default where
76
62
  email:
77
63
  - mingyuan0715@foxmail.com
78
64
  executables: []
79
65
  extensions: []
80
66
  extra_rdoc_files: []
81
67
  files:
68
+ - LICENSE
82
69
  - README.md
83
70
  - Rakefile
84
- - lib/default_or.rb
85
71
  - lib/default_where.rb
86
- - lib/default_where/like.rb
87
- - lib/default_where/not.rb
88
- - lib/default_where/or.rb
72
+ - lib/default_where/group.rb
89
73
  - lib/default_where/order.rb
90
- - lib/default_where/range.rb
74
+ - lib/default_where/params.rb
75
+ - lib/default_where/scope.rb
91
76
  - lib/default_where/version.rb
92
77
  - test/config/database.yml
93
78
  - test/config/database.yml.example
@@ -98,7 +83,7 @@ files:
98
83
  - test/models/user.rb
99
84
  homepage: https://github.com/qinmingyuan/default_where
100
85
  licenses:
101
- - LGPL-3.0
86
+ - MIT
102
87
  metadata: {}
103
88
  post_install_message:
104
89
  rdoc_options: []
@@ -115,16 +100,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
100
  - !ruby/object:Gem::Version
116
101
  version: '0'
117
102
  requirements: []
118
- rubyforge_project:
119
- rubygems_version: 2.7.6
103
+ rubygems_version: 3.2.22
120
104
  signing_key:
121
105
  specification_version: 4
122
106
  summary: default process params for where
123
107
  test_files:
124
- - test/default_where_test.rb
125
- - test/migrations/20180508082145_test_init.rb
126
- - test/config/database.yml.example
127
108
  - test/config/database.yml
109
+ - test/config/database.yml.example
110
+ - test/default_where_test.rb
111
+ - test/factories/users.rb
128
112
  - test/helper.rb
113
+ - test/migrations/20180508082145_test_init.rb
129
114
  - test/models/user.rb
130
- - test/factories/users.rb
data/lib/default_or.rb DELETED
@@ -1,20 +0,0 @@
1
- module DefaultWhere::DefaultOr
2
-
3
- def default_or(hash)
4
- return all if hash.blank?
5
-
6
- keys = hash.keys
7
- query = where(keys[0] => hash[keys[0]])
8
-
9
- keys[1..-1].each do |key|
10
- query = query.or(where(key => hash[key]))
11
- end
12
-
13
- query
14
- end
15
-
16
- end
17
-
18
- ActiveSupport.on_load :active_record do
19
- extend DefaultWhere::DefaultOr
20
- end
@@ -1,47 +0,0 @@
1
- module DefaultWhere
2
- module Like
3
- PATTERN = ['-like', '-rl', '-ll']
4
-
5
- def like_scope(params)
6
- where_string = []
7
- where_hash = {}
8
-
9
- params.each do |key, value|
10
- real_key = key.sub(/-like$/, '')
11
- agent_key = key.gsub(/[-.]/, '_')
12
-
13
- if column_names.include?(real_key)
14
- real_key = "#{table_name}.#{real_key}"
15
- end
16
-
17
- where_string << "#{real_key} like :#{agent_key}"
18
-
19
- if key.end_with?('-ll')
20
- like_value = "#{value}%"
21
- elsif key.end_with?('-rl')
22
- like_value = "%#{value}"
23
- else
24
- like_value = "%#{value}%"
25
- end
26
-
27
- where_hash.merge! agent_key.to_sym => like_value
28
- end
29
-
30
- where_string = where_string.join ' AND '
31
-
32
- if where_string.present?
33
- condition = [where_string, where_hash]
34
- where(condition)
35
- else
36
- all
37
- end
38
- end
39
-
40
- def filter_like(params)
41
- params.select do |k, _|
42
- k.end_with?(*PATTERN)
43
- end
44
- end
45
-
46
- end
47
- end
@@ -1,32 +0,0 @@
1
- module DefaultWhere
2
- module Not
3
-
4
- def not_scope(params)
5
- where_hash = {}
6
-
7
- params.each do |key, value|
8
- real_key = key.sub(/-not$/, '')
9
-
10
- if column_names.include?(real_key)
11
- real_key = "#{table_name}.#{real_key}"
12
- end
13
-
14
- where_hash.merge! real_key => value
15
- end
16
-
17
- if where_hash.present?
18
- where.not(where_hash)
19
- else
20
- all
21
- end
22
- end
23
-
24
- def filter_not(params)
25
- params.select do |k, _|
26
- k.end_with?('-not')
27
- end
28
- end
29
-
30
- end
31
- end
32
-
@@ -1,32 +0,0 @@
1
- module DefaultWhere
2
- module Or
3
-
4
- def or_scope(params)
5
- where_hash = {}
6
-
7
- params.each do |key, value|
8
- real_key = key.split('-or-')
9
-
10
- if column_names.include?(real_key)
11
- real_key = "#{table_name}.#{real_key}"
12
- end
13
-
14
- where_hash.merge! real_key => value
15
- end
16
-
17
- if where_hash.present?
18
- where.not(where_hash)
19
- else
20
- all
21
- end
22
- end
23
-
24
- def filter_or(params)
25
- params.select do |k, _|
26
- k.include?('-or-')
27
- end
28
- end
29
-
30
- end
31
- end
32
-
@@ -1,47 +0,0 @@
1
- module DefaultWhere
2
- module Range
3
-
4
- PATTERN = {
5
- '-gt' => '>',
6
- '-gte' => '>=',
7
- '-lt' => '<',
8
- '-lte' => '<='
9
- }
10
-
11
- def range_scope(params)
12
- where_string = []
13
- where_hash = {}
14
-
15
- params.each do |key, value|
16
- exp = /-(gt|gte|lt|lte)$/
17
- real_key = key.sub(exp, '')
18
- sign_str = key.match(exp).to_s
19
- agent_key = key.gsub(/[-.]/, '_')
20
-
21
- if column_names.include?(real_key)
22
- real_key = "#{table_name}.#{real_key}"
23
- end
24
-
25
- where_string << "#{real_key} #{PATTERN[sign_str]} :#{agent_key}"
26
-
27
- where_hash.merge! agent_key.to_sym => value
28
- end
29
-
30
- where_string = where_string.join ' AND '
31
-
32
- if where_string.present?
33
- condition = [where_string, where_hash]
34
- where(condition)
35
- else
36
- all
37
- end
38
- end
39
-
40
- def filter_range(params)
41
- params.select do |k, _|
42
- k.end_with?(*PATTERN.keys)
43
- end
44
- end
45
-
46
- end
47
- end