default_where 0.0.2 → 0.0.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
  SHA1:
3
- metadata.gz: 76676565331ffd1fd8c5a7a70fa82c99d0510bc9
4
- data.tar.gz: 302ec514e8c0457073945d86a93d9c8a1778950e
3
+ metadata.gz: 7bab77731f3e0efe12e228a3056d8d30278d9e07
4
+ data.tar.gz: 25da3de8c6506403490c7171aef25dc931ac82e6
5
5
  SHA512:
6
- metadata.gz: c5c6264304b2f92c847dc9324fc503d363e3bc5f621331c09eebf16b9bbb01502bf42057cd5a4bef3f422928cf37b2b8bbcde1bd4e3fd2d9acbfb7fa6d8ad904
7
- data.tar.gz: 3f78b9b76db2b62dfd471b419dbd256a567a69b687c6025f84b2546f2b9b74d1df62a756cfc5dd2319b0150c5c7085b77afc40e83b06c1e60d90ed3a5a63ddc2
6
+ metadata.gz: 2b9579575f9f65a863a6c3243fb705009a4728e1297aff129ce9c5165495805c9b637e3f059a494f10649e51a45a27a5ae895c030c8fc3f692c64c0ac2efc7f1
7
+ data.tar.gz: 569ad85a1a09f9d9fe3bb8619f11fcac5e9a182286a36b3c02b97b31c248d19a6d21b08bab78221d2806c76f91a4d1df01d882e7764cbebdb88559de02d7bf67
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ ## DefaultWhere
2
+
3
+ This Library set default params process for where query in ActiveRecord
4
+
5
+ ## Usage
6
+
7
+ - Before use default_where
8
+
9
+ ```ruby
10
+ # Parameters: {"role_id"=>"1", "age"=>"20", "teacher_id"=> "2"}
11
+ User.includes(:student).where(role_id: params[:role_id], age: params[:age], student: {teacher_id: params[:teacher_id]})
12
+
13
+ ```
14
+
15
+ - after Use default_where
16
+
17
+ params must use string key
18
+
19
+ ```ruby
20
+ User.default_where(params, student: :teacher_id)
21
+
22
+ # It will generate the query scope to above
23
+ ```
24
+
25
+ ## Features
26
+ - remove blank where query, need not write where query like this
27
+
28
+ ```ruby
29
+ # if not remove blank query params
30
+ users = Users.where(role_id: params[:role_id])
31
+ users = users.where(age: params[:age]) if params[:age]
32
+ ```
33
+
34
+
@@ -1,52 +1,38 @@
1
- module DefaultWhere::Equal
1
+ module DefaultWhere
2
+ module Equal
2
3
 
3
- def equal_scope(params, options)
4
- @where_string = ''
5
- @where_hash = {}
4
+ def equal_scope(params, options)
5
+ @where_string = ''
6
+ @where_hash = {}
6
7
 
7
- params = equal_params(params, options)
8
- equal_process(params) # 处理相等条件的参数
8
+ params = equal_params(params, options)
9
+ equal_process(params)
9
10
 
10
- @where_string.sub!(/^\sand/, '') if @where_string.start_with?(' and')
11
+ @where_string.sub!(/^\sand/, '') if @where_string.start_with?(' and')
11
12
 
12
- condition = [@where_string, @where_hash]
13
+ condition = [@where_string, @where_hash]
13
14
 
14
- where(condition)
15
- end
16
-
17
- private
18
- # 处理等于参数
19
- def equal_process(params)
20
- params.each do |key, value|
21
- origin_key = key.split('.').last
22
-
23
- @where_string << " and #{key} = :#{origin_key}"
24
- @where_hash.merge! origin_key.to_sym => value
25
- end
26
- end
27
-
28
- # 带表格的参数
29
- def equal_params(params, options)
30
- options.each do |assoc, column|
31
- assoc_model = reflections[assoc.to_sym]
32
- value = params.delete(column.to_s)
33
- if assoc_model && value
34
- params["#{assoc_model.table_name}.#{column}"] = value
35
- elsif assoc_model.blank?
36
- raise 'Wrong Association Name'
37
- end
15
+ where(condition)
38
16
  end
39
17
 
40
- # since 1.9 is using lazy iteration
41
- params.to_a.each do |key, _|
42
- if column_names.include?(key)
43
- params["#{table_name}.#{key}"] = params.delete(key.to_s)
44
- elsif !key.include?('.')
45
- params.delete(key.to_s)
18
+ private
19
+ # process equal params
20
+ def equal_process(params)
21
+ params.each do |key, value|
22
+ origin_key = key.split('.').last
23
+
24
+ @where_string << " and #{key} = :#{origin_key}"
25
+ if value.to_i.to_s == value
26
+ @where_hash.merge! origin_key.to_sym => value.to_i
27
+ elsif 'true' == value
28
+ @where_hash.merge! origin_key.to_sym => true
29
+ elsif 'false' == value
30
+ @where_hash.merge! origin_key.to_sym => false
31
+ else
32
+ @where_hash.merge! origin_key.to_sym => value
33
+ end
46
34
  end
47
35
  end
48
36
 
49
- params
50
37
  end
51
-
52
38
  end
@@ -0,0 +1,25 @@
1
+ module DefaultWhere
2
+ module Order
3
+
4
+ def order_scope(params)
5
+ @order_array = []
6
+
7
+ order_process(params)
8
+
9
+ order(@order_array)
10
+ end
11
+
12
+ def order_process(params)
13
+
14
+ params.select{ |key, _| key.end_with?('_asc') }.each do |k, _|
15
+ @order_array << k.sub(/_asc$/, ' asc')
16
+ end
17
+
18
+ params.select{ |key, _| key.end_with?('_desc') }.each do |k, _|
19
+ @order_array << k.sub(/_desc$/, ' desc')
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -1,69 +1,58 @@
1
- module DefaultWhere::Range
1
+ module DefaultWhere
2
+ module Range
3
+ PATTERN = {
4
+ '>' => /_gt$/,
5
+ '>=' => /_gte$/,
6
+ '<' => /_lt$/,
7
+ '<=' => /_lte$/
8
+ }
9
+ SPATTERN = {
10
+ '_gt' => '>',
11
+ '_gte' => '>=',
12
+ '_lt' => '<',
13
+ '_lte' => '<='
14
+ }
2
15
 
3
- PATTERN = {
4
- '>' => /_gt$/,
5
- '>=' => /_gte$/,
6
- '<' => /_lt$/,
7
- '<=' => /_lte$/
8
- }
9
16
 
10
- def range_scope(params, options)
11
- @range_string = ''
12
- @range_hash = {}
17
+ def range_scope(params)
18
+ @range_string = ''
19
+ @range_hash = {}
13
20
 
14
- range_process(params, options) # 处理范围查询的参数
21
+ range_process(params)
15
22
 
16
- @range_string.sub!(/^\sand/, '') if @range_string.start_with?(' and')
23
+ @range_string.sub!(/^\sand/, '') if @range_string.start_with?(' and')
17
24
 
18
- condition = [@range_string, @range_hash]
25
+ condition = [@range_string, @range_hash]
19
26
 
20
- where(condition)
21
- end
22
-
23
- # 处理范围参数
24
- def range_process_compare(params, compare)
25
- params.each do |origin_key, value|
26
- key = origin_key.sub(PATTERN[compare], '')
27
-
28
- @range_string << " and #{key} #{compare} :#{origin_key}"
29
- @range_hash.merge! origin_key.to_sym => value
27
+ where(condition)
30
28
  end
31
29
 
32
- params
33
- end
30
+ # process with table
31
+ # gt: greater than
32
+ # gte: greater than or equal to
33
+ # lt: less than
34
+ # lte: less than or equal to
35
+ def range_process(params)
36
+ SPATTERN.each do |k, v|
37
+ gt_options = params.select { |key, _| key.end_with?(k) }
38
+ range_process_compare(gt_options, v)
39
+ end
40
+ end
34
41
 
42
+ def range_process_compare(params, compare)
43
+ params.each do |origin_key, value|
44
+ key = origin_key.sub(PATTERN[compare], '')
35
45
 
36
- # 带表格的参数
37
- # 先检查是否有范围查询的参数,生成范围的条件
38
- # gt: 大于
39
- # gte: 大于等于
40
- # lt: 小于
41
- # lte: 小于等于
42
- def range_process(params, options)
43
- options.each do |assoc, column|
44
- assoc_model = reflections[assoc.to_sym]
45
- if assoc_model
46
- params.select{ |k, _| k.start_with?(column.to_s) }.each do |key, _|
47
- params["#{assoc_model.table_name}.#{key}"] = params.delete(key)
46
+ @range_string << " and #{key} #{compare} :#{origin_key}"
47
+ if value.to_i.to_s == value
48
+ @range_hash.merge! origin_key.to_sym => value.to_i
49
+ else
50
+ @range_hash.merge! origin_key.to_sym => value
48
51
  end
49
- else
50
- raise 'Wrong Association Name'
51
52
  end
52
- end
53
53
 
54
- params.each do |key, _|
55
- params["#{table_name}.#{key}"] = params.delete(key.to_s)
54
+ params
56
55
  end
57
56
 
58
- gt_options = params.select { |key, _| key.end_with?('_gt') }
59
- gte_options = params.select { |key, _| key.end_with?('_gte') }
60
- lt_options = params.select { |key, _| key.end_with?('_lt') }
61
- lte_options = params.select { |key, _| key.end_with?('_lte') }
62
-
63
- range_process_compare(gt_options, '>')
64
- range_process_compare(gte_options, '>=')
65
- range_process_compare(lt_options, '<')
66
- range_process_compare(lte_options, '<=')
67
57
  end
68
-
69
58
  end
@@ -1,3 +1,3 @@
1
1
  module DefaultWhere
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/default_where.rb CHANGED
@@ -1,43 +1,84 @@
1
+ require 'default_where/equal'
2
+ require 'default_where/range'
3
+ require 'default_where/order'
4
+
1
5
  module DefaultWhere
2
- require 'default_where/equal'
3
- require 'default_where/range'
4
6
  include DefaultWhere::Equal
5
7
  include DefaultWhere::Range
8
+ include DefaultWhere::Order
6
9
 
7
- # 参数格式
8
- # options 格式:
10
+ # options:
9
11
  # <association_name> => <column_name>
10
12
  # student: 'teacher_id'
11
- def query_scope(params, options = {}, include = true)
12
- params ||= {}
13
+ def default_where(params = {}, options = {})
14
+ return all if params.blank?
15
+
16
+ params = params_with_table(params, options)
13
17
 
14
- params = filter_params(params)
15
18
  range_params = filter_range(params)
16
- equal_params = params.except!(range_params.keys)
19
+ params = params.except!(range_params.keys)
20
+
21
+ order_params = filter_order(params)
22
+ equal_params = params.except!(order_params.keys)
23
+
24
+ include = options.present? && !self.values.keys.include?(:includes)
25
+
26
+ if include
27
+ includes(options.keys)
28
+ end
17
29
 
18
- include = include && options.present?
30
+ if range_params.present?
31
+ range_scope(range_params, options)
32
+ end
19
33
 
20
- if include && range_params.present?
21
- includes(options.keys).range_scope(range_params, options).equal_scope(equal_params, options)
22
- elsif include && range_params.blank?
23
- includes(options.keys).equal_scope(equal_params, options)
24
- elsif !include && range_params.present?
25
- range_scope(range_params, options).equal_scope(equal_params, options)
26
- elsif !include && range_params.blank?
34
+ if equal_params.present?
27
35
  equal_scope(equal_params, options)
28
36
  end
29
- end
30
37
 
31
- private
32
- # remove blank values
33
- def filter_params(params)
34
- params.select { |_, v| v.present? }
38
+ if order_params
39
+ order_scope(order_params, options)
40
+ end
35
41
  end
36
42
 
37
43
  def filter_range(params)
38
- params.select do |key, _|
39
- key.end_with?('gt', 'gte', 'lt', 'lte')
44
+ params.select do |k, _|
45
+ k.end_with?('_gt', '_gte', '_lt', '_lte')
46
+ end
47
+ end
48
+
49
+ def filter_order(params)
50
+ params.select do |k, _|
51
+ k.end_with?('_asc', '_desc')
52
+ end
53
+ end
54
+
55
+ # params with table
56
+ def params_with_table(params, options)
57
+ params.stringify_keys!
58
+ params.compact!
59
+
60
+ options.each do |assoc, column|
61
+ assoc_model = reflections[assoc.to_s]
62
+
63
+ if assoc_model
64
+ params.select{ |k, _| k.start_with?(column.to_s) }.each do |k, _|
65
+ params["#{assoc_model.table_name}.#{k}"] = params.delete(k)
66
+ end
67
+ else
68
+ raise 'Wrong Association Name'
69
+ end
40
70
  end
71
+
72
+ # since 1.9 is using lazy iteration
73
+ params.to_a.each do |key, _|
74
+ if column_names.include?(key)
75
+ params["#{table_name}.#{key}"] = params.delete(key.to_s)
76
+ elsif !key.include?('.')
77
+ params.delete(key.to_s)
78
+ end
79
+ end
80
+
81
+ params
41
82
  end
42
83
 
43
84
  end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ class DefaultWhereTest < ActiveSupport::TestCase
4
+
5
+ test "truth" do
6
+ assert_kind_of Module, DefaultWhere
7
+ end
8
+
9
+ test "" do
10
+ params = { id: 1, uid: 2, name: 3 }
11
+ options = { signs: 'name' }
12
+
13
+
14
+ end
15
+
16
+ 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: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-11 00:00:00.000000000 Z
11
+ date: 2015-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
19
+ version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.2'
26
+ version: 4.0.0
27
27
  description: Description of QueryScope.
28
28
  email:
29
29
  - mingyuan0715@foxmail.com
@@ -32,12 +32,15 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - MIT-LICENSE
35
+ - README.md
35
36
  - Rakefile
36
37
  - lib/default_where.rb
37
38
  - lib/default_where/equal.rb
39
+ - lib/default_where/order.rb
38
40
  - lib/default_where/range.rb
39
41
  - lib/default_where/version.rb
40
42
  - lib/tasks/query_scope_tasks.rake
43
+ - test/default_where_test.rb
41
44
  - test/dummy/README.rdoc
42
45
  - test/dummy/Rakefile
43
46
  - test/dummy/app/assets/javascripts/application.js
@@ -71,7 +74,6 @@ files:
71
74
  - test/dummy/public/422.html
72
75
  - test/dummy/public/500.html
73
76
  - test/dummy/public/favicon.ico
74
- - test/query_scope_test.rb
75
77
  - test/test_helper.rb
76
78
  homepage: https://github.com/qinmingyuan/default_where
77
79
  licenses:
@@ -93,11 +95,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
95
  version: '0'
94
96
  requirements: []
95
97
  rubyforge_project:
96
- rubygems_version: 2.4.5
98
+ rubygems_version: 2.4.5.1
97
99
  signing_key:
98
100
  specification_version: 4
99
- summary: Summary of QueryScope.
101
+ summary: default process params for where
100
102
  test_files:
103
+ - test/default_where_test.rb
101
104
  - test/dummy/app/assets/javascripts/application.js
102
105
  - test/dummy/app/assets/stylesheets/application.css
103
106
  - test/dummy/app/controllers/application_controller.rb
@@ -131,5 +134,4 @@ test_files:
131
134
  - test/dummy/public/favicon.ico
132
135
  - test/dummy/Rakefile
133
136
  - test/dummy/README.rdoc
134
- - test/query_scope_test.rb
135
137
  - test/test_helper.rb
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class QueryScopeTest < ActiveSupport::TestCase
4
- test "truth" do
5
- assert_kind_of Module, QueryScope
6
- end
7
- end