default_where 2.2.2 → 2.2.3

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: 4925afe89ab55b52bb5945813486b4aeefaa3f29e9a2ed93ca0ca3982076df39
4
- data.tar.gz: 9b564d353c2b3e7f578e3773aac664294ccd4d08d092d6b71ecc2aa92d247d31
3
+ metadata.gz: 9860242afa48f1b00939bf840ab9c905891a664498082c3596853b856a6014df
4
+ data.tar.gz: df25ea89826f20d10291780ae4e9f90770a1627bbef83a7f9cf82bc3447a7549
5
5
  SHA512:
6
- metadata.gz: ae9e5b07c2b5b8fb269370c98d2a8c3d772d263f646a7fa2b229e1b338436c7a55a1ba6448ff44d39381171d42d0e615b1dd204eabbaa0cd634c350615f293e2
7
- data.tar.gz: eaa3827a022d87f95a5ecbea489bb87e07b5e6a051981ee21cc94dd130fc115574a8f3e212bb0352619a038bdf8ab4997e0d177ce4ad511115ea22dfee483980
6
+ metadata.gz: 3a4542d9ccc386102203d62ae0aadcdadc4c688f09365b96d7a620ade26050bd872f7f399f12d5e5e0cb6c3f40aa0e4feb19c844872e8d4819f927c85240862a
7
+ data.tar.gz: 37dab845591f27c34813b29d0cf1b4139568de7670b6b56ae0faefcc32c9d8be09e71f0117060df667a760f0b59c2db6984d5ac2d286ff0cf5fac4e32a92cf53
data/README.md CHANGED
@@ -1,10 +1,19 @@
1
1
  ## DefaultWhere
2
2
 
3
+
3
4
  This Library set default params process for where query in ActiveRecord
4
5
 
5
6
  It's a wise decision to use `default_where` with [default_form](https://github.com/qinmingyuan/default_form) to replace `ransack`
6
7
 
7
- ## Features and Usage
8
+ ## 使用说明
9
+
10
+
11
+ ### 语法概览
12
+
13
+ * 等于:key: value
14
+ * 范围:key-gte: value,
15
+ * 排序:key-asc: 1, key-desc: 2
16
+ * 排除:key-not: value
8
17
 
9
18
  ### Normal equal params
10
19
 
@@ -117,4 +126,4 @@ User.includes(:role).where(name: params[:name], 'roles.id': params[:'role.id']).
117
126
  * After use `default_where`
118
127
  ```ruby
119
128
  User.default_where(params)
120
- ```
129
+ ```
@@ -6,7 +6,7 @@ module DefaultWhere
6
6
  where_string = []
7
7
  where_hash = {}
8
8
 
9
- params.select{ |k, _| k.end_with?(*PATTERN) }.each do |key, value|
9
+ params.each do |key, value|
10
10
  real_key = key.sub(/-like$/, '')
11
11
  agent_key = key.gsub(/[-.]/, '_')
12
12
 
@@ -17,11 +17,11 @@ module DefaultWhere
17
17
  where_string << "#{real_key} like :#{agent_key}"
18
18
 
19
19
  if key.end_with?('-ll')
20
- like_value = value.to_s + '%'
20
+ like_value = "#{value}%"
21
21
  elsif key.end_with?('-rl')
22
- like_value = '%' + value.to_s
22
+ like_value = "%#{value}"
23
23
  else
24
- like_value = '%' + value.to_s + '%'
24
+ like_value = "%#{value}%"
25
25
  end
26
26
 
27
27
  where_hash.merge! agent_key.to_sym => like_value
@@ -4,12 +4,8 @@ module DefaultWhere
4
4
  def order_scope(params)
5
5
  order_array = []
6
6
 
7
- params.select{ |key, _| key.end_with?('-asc') }.each do |k, _|
8
- order_array << k.sub(/-asc$/, ' ASC')
9
- end
10
-
11
- params.select{ |key, _| key.end_with?('-desc') }.each do |k, _|
12
- order_array << k.sub(/-desc$/, ' DESC')
7
+ params.sort_by{ |_, v| v.to_i }.each do |i|
8
+ order_array << i[0].sub(/-(asc|desc)$/, '-asc' => ' ASC', '-desc' => ' DESC')
13
9
  end
14
10
 
15
11
  order(order_array)
@@ -17,7 +13,7 @@ module DefaultWhere
17
13
 
18
14
  def filter_order(params)
19
15
  params.select do |k, v|
20
- k.end_with?('-asc', '-desc') && v =~ /^[1-9]$/
16
+ k.end_with?('-asc', '-desc') && String(v) =~ /^[1-9]$/
21
17
  end
22
18
  end
23
19
 
@@ -12,22 +12,19 @@ module DefaultWhere
12
12
  where_string = []
13
13
  where_hash = {}
14
14
 
15
- PATTERN.each do |char, sign|
16
- options = params.select{ |key, _| key.end_with?(char) }
17
-
18
- options.each do |key, value|
19
- exp = Regexp.new(char + '$')
20
- real_key = key.sub(exp, '')
21
- agent_key = key.gsub(/[-.]/, '_')
22
-
23
- if column_names.include?(real_key)
24
- real_key = "#{table_name}.#{real_key}"
25
- end
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
26
24
 
27
- where_string << "#{real_key} #{sign} :#{agent_key}"
25
+ where_string << "#{real_key} #{PATTERN[sign_str]} :#{agent_key}"
28
26
 
29
- where_hash.merge! agent_key.to_sym => value
30
- end
27
+ where_hash.merge! agent_key.to_sym => value
31
28
  end
32
29
 
33
30
  where_string = where_string.join ' AND '
@@ -1,3 +1,3 @@
1
1
  module DefaultWhere
2
- VERSION = '2.2.2'
3
- end
2
+ VERSION = '2.2.3'
3
+ end
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.2
4
+ version: 2.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-19 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.7.7
119
+ rubygems_version: 2.7.6
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: default process params for where