default_where 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -7
- data/lib/default_where.rb +34 -30
- data/lib/default_where/like.rb +33 -0
- data/lib/default_where/order.rb +3 -3
- data/lib/default_where/range.rb +3 -3
- data/lib/default_where/version.rb +1 -1
- metadata +4 -4
- data/lib/tasks/query_scope_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab39ef3d31eaf2792f44350e22cdc97ea35d3e3
|
4
|
+
data.tar.gz: fd311a5d219d7cf5b5ee6ecf5f3240fd9b79408c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae8a24b39eb7fa0a412139fb9e0be1506e9753c5c10969dbe169a4fe50c35c30b6973cfd1e9649f146ad2ad706d8351d0bfa479dc4a21d30e39d34e093c81f41
|
7
|
+
data.tar.gz: 46bc20372f0a1f118fe25ec723ab29d2b64f9c9e4568bbdd82ac7e46568848abcebcc8aada55e763a40e9cd72832bc3e8e7600bb9f0ee0e77607ef2ecf4c4829
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ This Library set default params process for where query in ActiveRecord
|
|
9
9
|
- Params:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
# rails 4 and
|
12
|
+
# rails 4 and later, default_where does nothing
|
13
13
|
params = { role_id: 1, age: 20 }
|
14
14
|
User.default_where(params)
|
15
15
|
```
|
@@ -20,6 +20,9 @@ User.default_where(params)
|
|
20
20
|
```ruby
|
21
21
|
User.belongs_to :role
|
22
22
|
params = { name: 'dhh', 'role.id': 2 }
|
23
|
+
|
24
|
+
# you can use any table name or reference name
|
25
|
+
params = { name: 'dhh', 'roles.id': 2 }
|
23
26
|
```
|
24
27
|
- Before use `default_where`
|
25
28
|
```ruby
|
@@ -58,12 +61,15 @@ users = users.where(age: params[:age]) if params[:age]
|
|
58
61
|
- After use `default_where`
|
59
62
|
```ruby
|
60
63
|
User.default_where(params)
|
64
|
+
|
65
|
+
# also can control which blank value can use
|
66
|
+
User.default_where(params, { allow: [nil] })
|
61
67
|
```
|
62
68
|
|
63
69
|
### Order params
|
64
70
|
- Params
|
65
71
|
```ruby
|
66
|
-
params = {
|
72
|
+
params = { 'age-asc': '1', 'last_login_at-asc': '2' }
|
67
73
|
```
|
68
74
|
- Before use `default_where`
|
69
75
|
```ruby
|
@@ -77,7 +83,7 @@ User.default_where(params)
|
|
77
83
|
## A sample with all params above
|
78
84
|
- Params
|
79
85
|
```ruby
|
80
|
-
{ name: 'dhh', 'role.id': 2, 'age-lte': 2,
|
86
|
+
{ name: 'dhh', 'role.id': 2, 'age-lte': 2, 'age-asc': '1', 'last_login_at-asc': '2' }
|
81
87
|
```
|
82
88
|
- Before use `default_where`
|
83
89
|
```ruby
|
@@ -86,7 +92,4 @@ User.includes(:role).where(name: params[:name], 'roles.id': params[:'role.id']).
|
|
86
92
|
- After use `default_where`
|
87
93
|
```ruby
|
88
94
|
User.default_where(params)
|
89
|
-
```
|
90
|
-
|
91
|
-
It will generate the query scope to above
|
92
|
-
params must use string key
|
95
|
+
```
|
data/lib/default_where.rb
CHANGED
@@ -1,42 +1,43 @@
|
|
1
1
|
require 'default_where/not'
|
2
2
|
require 'default_where/range'
|
3
|
+
require 'default_where/like'
|
3
4
|
require 'default_where/order'
|
4
5
|
|
5
6
|
module DefaultWhere
|
6
7
|
include DefaultWhere::Not
|
7
8
|
include DefaultWhere::Range
|
8
9
|
include DefaultWhere::Order
|
10
|
+
include DefaultWhere::Like
|
11
|
+
|
12
|
+
REJECT = ['', ' ', nil]
|
13
|
+
|
9
14
|
|
10
15
|
def default_where(params = {}, options = {})
|
11
16
|
return all if params.blank?
|
12
17
|
|
13
|
-
params, tables = params_with_table(params, options)
|
18
|
+
params, refs, tables = params_with_table(params, options)
|
14
19
|
|
15
20
|
range_params = filter_range(params)
|
16
|
-
params = params.except!(*range_params.keys)
|
17
|
-
|
18
21
|
order_params = filter_order(params)
|
19
|
-
params = params.except!(*order_params.keys)
|
20
|
-
|
21
22
|
not_params = filter_not(params)
|
22
|
-
|
23
|
+
like_params = filter_like(params)
|
24
|
+
|
25
|
+
equal_params = params.except!(*range_params.keys, *order_params.keys, *not_params.keys, *like_params.keys)
|
23
26
|
|
24
|
-
includes(
|
27
|
+
includes(refs).where(equal_params).references(tables)
|
25
28
|
.not_scope(not_params)
|
29
|
+
.like_scope(like_params)
|
26
30
|
.range_scope(range_params)
|
27
31
|
.order_scope(order_params)
|
28
32
|
end
|
29
33
|
|
30
34
|
def params_with_table(params = {}, options = {})
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
default_reject.
|
35
|
-
|
36
|
-
|
37
|
-
# todo secure bug
|
38
|
-
if params.respond_to?(:permitted?) && !params.permitted?
|
39
|
-
params.permit!
|
35
|
+
if options[:reject]
|
36
|
+
default_reject = [options[:reject]].flatten
|
37
|
+
elsif options[:allow]
|
38
|
+
default_reject = REJECT - [options[:allow]].flatten
|
39
|
+
else
|
40
|
+
default_reject = REJECT
|
40
41
|
end
|
41
42
|
|
42
43
|
params = params.to_h
|
@@ -44,33 +45,36 @@ module DefaultWhere
|
|
44
45
|
params.reject! { |_, value| default_reject.include?(value) }
|
45
46
|
|
46
47
|
refs = []
|
48
|
+
tables = []
|
49
|
+
final_params = {}
|
47
50
|
|
48
|
-
|
49
|
-
params.to_a.each do |key, _|
|
50
|
-
|
51
|
+
params.each do |key, _|
|
51
52
|
if key =~ /\./
|
52
|
-
|
53
|
+
table, col = key.split('.')
|
54
|
+
as_model = reflections[table]
|
53
55
|
f_col, _ = col.split('-')
|
54
|
-
as_model = reflections[as]
|
55
56
|
|
56
57
|
if as_model && as_model.klass.column_names.include?(f_col)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
final_params["#{as_model.table_name}.#{col}"] = params[key]
|
59
|
+
tables << as_model.table_name
|
60
|
+
refs << table.to_sym
|
61
|
+
elsif connection.data_sources.include? table
|
62
|
+
final_params["#{table}.#{col}"] = params[key]
|
63
|
+
tables << table
|
64
|
+
keys = reflections.select { |_, v| v.table_name == table }.keys
|
65
|
+
if keys && keys.size == 1
|
66
|
+
refs << keys.first.to_sym
|
67
|
+
end
|
61
68
|
end
|
62
69
|
else
|
63
70
|
f_key, _ = key.split('-')
|
64
71
|
if column_names.include?(f_key)
|
65
|
-
|
66
|
-
else
|
67
|
-
params.delete(key)
|
72
|
+
final_params["#{table_name}.#{key}"] = params[key]
|
68
73
|
end
|
69
74
|
end
|
70
|
-
|
71
75
|
end
|
72
76
|
|
73
|
-
[
|
77
|
+
[final_params, refs, tables]
|
74
78
|
end
|
75
79
|
|
76
80
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module DefaultWhere
|
2
|
+
module Like
|
3
|
+
|
4
|
+
def like_scope(params)
|
5
|
+
where_string = []
|
6
|
+
where_hash = {}
|
7
|
+
|
8
|
+
params.select{ |k, _| k.end_with?('-like') }.each do |key, value|
|
9
|
+
real_key = key.sub(/-like$/, '')
|
10
|
+
agent_key = key.gsub(/[-\.]/, '_')
|
11
|
+
|
12
|
+
where_string << "#{real_key} like :#{agent_key}"
|
13
|
+
where_hash.merge! agent_key.to_sym => '%' + value.to_s + '%'
|
14
|
+
end
|
15
|
+
|
16
|
+
where_string = where_string.join ' AND '
|
17
|
+
|
18
|
+
if where_string.present?
|
19
|
+
condition = [where_string, where_hash]
|
20
|
+
where(condition)
|
21
|
+
else
|
22
|
+
all
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def filter_like(params)
|
27
|
+
params.select do |k, _|
|
28
|
+
k.end_with?('-like')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/default_where/order.rb
CHANGED
@@ -5,11 +5,11 @@ module DefaultWhere
|
|
5
5
|
order_array = []
|
6
6
|
|
7
7
|
params.select{ |key, _| key.end_with?('-asc') }.each do |k, _|
|
8
|
-
order_array << k.sub(/-asc$/, '
|
8
|
+
order_array << k.sub(/-asc$/, ' ASC')
|
9
9
|
end
|
10
10
|
|
11
11
|
params.select{ |key, _| key.end_with?('-desc') }.each do |k, _|
|
12
|
-
order_array << k.sub(/-desc$/, '
|
12
|
+
order_array << k.sub(/-desc$/, ' DESC')
|
13
13
|
end
|
14
14
|
|
15
15
|
order(order_array)
|
@@ -17,7 +17,7 @@ module DefaultWhere
|
|
17
17
|
|
18
18
|
def filter_order(params)
|
19
19
|
params.select do |k, v|
|
20
|
-
k
|
20
|
+
k.end_with?('-asc', '-desc') && v =~ /^[1-9]$/
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
data/lib/default_where/range.rb
CHANGED
@@ -9,7 +9,7 @@ module DefaultWhere
|
|
9
9
|
}
|
10
10
|
|
11
11
|
def range_scope(params)
|
12
|
-
where_string =
|
12
|
+
where_string = []
|
13
13
|
where_hash = {}
|
14
14
|
|
15
15
|
PATTERN.each do |char, sign|
|
@@ -20,13 +20,13 @@ module DefaultWhere
|
|
20
20
|
real_key = key.sub(exp, '')
|
21
21
|
agent_key = key.gsub(/[-\.]/, '_')
|
22
22
|
|
23
|
-
where_string << "
|
23
|
+
where_string << "#{real_key} #{sign} :#{agent_key}"
|
24
24
|
|
25
25
|
where_hash.merge! agent_key.to_sym => value
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
where_string
|
29
|
+
where_string = where_string.join ' AND '
|
30
30
|
|
31
31
|
if where_string.present?
|
32
32
|
condition = [where_string, where_hash]
|
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.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- qinmingyuan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -35,11 +35,11 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- Rakefile
|
37
37
|
- lib/default_where.rb
|
38
|
+
- lib/default_where/like.rb
|
38
39
|
- lib/default_where/not.rb
|
39
40
|
- lib/default_where/order.rb
|
40
41
|
- lib/default_where/range.rb
|
41
42
|
- lib/default_where/version.rb
|
42
|
-
- lib/tasks/query_scope_tasks.rake
|
43
43
|
- test/default_where_test.rb
|
44
44
|
- test/test_helper.rb
|
45
45
|
homepage: https://github.com/qinmingyuan/default_where
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
64
|
rubyforge_project:
|
65
|
-
rubygems_version: 2.6.
|
65
|
+
rubygems_version: 2.6.11
|
66
66
|
signing_key:
|
67
67
|
specification_version: 4
|
68
68
|
summary: default process params for where
|